From 10b0580ff48b7d7937836425a3f2c37322ad34a8 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Wed, 3 Aug 2016 16:29:47 -0700 Subject: Ensure trailing newline is written to cron file Records whether existing cron file (or CRONCMD output) has a terminating newline, and ensures a trailing newline is written as necessary EVEN IF NO CHANGE WAS MADE to the target env/job Fixes #2316 --- system/cron.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'system') diff --git a/system/cron.py b/system/cron.py index 654ca410..f13032c8 100644 --- a/system/cron.py +++ b/system/cron.py @@ -224,6 +224,7 @@ class CronTab(object): self.root = (os.getuid() == 0) self.lines = None self.ansible = "#Ansible: " + self.terminated= True if cron_file: if os.path.isabs(cron_file): @@ -242,7 +243,9 @@ class CronTab(object): # read the cronfile try: f = open(self.cron_file, 'r') - self.lines = f.read().splitlines() + read_cron_file = f.read() + self.terminated = read_cron_file.endswith(('\r', '\n')) + self.lines = read_cron_file.splitlines() f.close() except IOError: # cron file does not exist @@ -256,6 +259,8 @@ class CronTab(object): if rc != 0 and rc != 1: # 1 can mean that there are no jobs. raise CronTabError("Unable to read crontab") + self.terminated = out.endswith(('\r', '\n')) + lines = out.splitlines() count = 0 for l in lines: @@ -464,8 +469,8 @@ class CronTab(object): crons.append(cron) result = '\n'.join(crons) - if result and result[-1] not in ['\n', '\r']: - result += '\n' + if result: + result = result.rstrip('\r\n') + '\n' return result def _read_user_execute(self): @@ -660,6 +665,10 @@ def main(): crontab.remove_job(name) changed = True + # no changes to env/job, but existing crontab needs a terminating newline + if not changed and not crontab.terminated: + changed = True + res_args = dict( jobs = crontab.get_jobnames(), envs = crontab.get_envnames(), -- cgit v1.2.1