From 6de0c3408cb8444aa028061862d7aa61b1530df2 Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Sat, 24 Sep 2016 06:24:27 +0000 Subject: [PATCH] AgePolicy: Create the (new) date file automatically Closes: Debian/britney2#13 Signed-off-by: Niels Thykier --- policies/policy.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/policies/policy.py b/policies/policy.py index 1912218..bdb8e59 100644 --- a/policies/policy.py +++ b/policies/policy.py @@ -281,23 +281,37 @@ class AgePolicy(BasePolicy): """Parse the dates file""" dates = self._dates fallback_filename = os.path.join(self.options.testing, 'Dates') + using_new_name = False try: filename = os.path.join(self.options.state_dir, 'age-policy-dates') if not os.path.exists(filename) and os.path.exists(fallback_filename): filename = fallback_filename + else: + using_new_name = True except AttributeError: - filename = fallback_filename + if os.path.exists(fallback_filename): + filename = fallback_filename + else: + raise RuntimeError("Please set STATE_DIR in the britney configuration") - with open(filename, encoding='utf-8') as fd: - for line in fd: - # - l = line.split() - if len(l) != 3: - continue - try: - dates[l[0]] = (l[1], int(l[2])) - except ValueError: - pass + try: + with open(filename, encoding='utf-8') as fd: + for line in fd: + # + l = line.split() + if len(l) != 3: + continue + try: + dates[l[0]] = (l[1], int(l[2])) + except ValueError: + pass + except FileNotFoundError: + if not using_new_name: + # If we using the legacy name, then just give up + raise + self.log("%s does not appear to exist. Creating it" % filename) + with open(filename, mode='wx', encoding='utf-8'): + pass def _read_urgencies_file(self, britney): urgencies = self._urgencies