Record existing cron file as string property, rather than only recording termination
This seems less hackish, and feels more proper for diff generation
This commit is contained in:
parent
f64990df02
commit
f8dad7130d
1 changed files with 11 additions and 9 deletions
|
@ -224,7 +224,7 @@ class CronTab(object):
|
||||||
self.root = (os.getuid() == 0)
|
self.root = (os.getuid() == 0)
|
||||||
self.lines = None
|
self.lines = None
|
||||||
self.ansible = "#Ansible: "
|
self.ansible = "#Ansible: "
|
||||||
self.terminated= True
|
self.existing = ''
|
||||||
|
|
||||||
if cron_file:
|
if cron_file:
|
||||||
if os.path.isabs(cron_file):
|
if os.path.isabs(cron_file):
|
||||||
|
@ -243,9 +243,8 @@ class CronTab(object):
|
||||||
# read the cronfile
|
# read the cronfile
|
||||||
try:
|
try:
|
||||||
f = open(self.cron_file, 'r')
|
f = open(self.cron_file, 'r')
|
||||||
read_cron_file = f.read()
|
self.existing = f.read()
|
||||||
self.terminated = read_cron_file.endswith(('\r', '\n'))
|
self.lines = self.existing.splitlines()
|
||||||
self.lines = read_cron_file.splitlines()
|
|
||||||
f.close()
|
f.close()
|
||||||
except IOError:
|
except IOError:
|
||||||
# cron file does not exist
|
# cron file does not exist
|
||||||
|
@ -259,7 +258,7 @@ class CronTab(object):
|
||||||
if rc != 0 and rc != 1: # 1 can mean that there are no jobs.
|
if rc != 0 and rc != 1: # 1 can mean that there are no jobs.
|
||||||
raise CronTabError("Unable to read crontab")
|
raise CronTabError("Unable to read crontab")
|
||||||
|
|
||||||
self.terminated = out.endswith(('\r', '\n'))
|
self.existing = out
|
||||||
|
|
||||||
lines = out.splitlines()
|
lines = out.splitlines()
|
||||||
count = 0
|
count = 0
|
||||||
|
@ -268,6 +267,9 @@ class CronTab(object):
|
||||||
not re.match( r'# \(/tmp/.*installed on.*\)', l) and
|
not re.match( r'# \(/tmp/.*installed on.*\)', l) and
|
||||||
not re.match( r'# \(.*version.*\)', l)):
|
not re.match( r'# \(.*version.*\)', l)):
|
||||||
self.lines.append(l)
|
self.lines.append(l)
|
||||||
|
else:
|
||||||
|
pattern = re.escape(l) + '[\r\n]?'
|
||||||
|
self.existing = re.sub(pattern, '', self.existing, 1)
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
def is_empty(self):
|
def is_empty(self):
|
||||||
|
@ -460,7 +462,7 @@ class CronTab(object):
|
||||||
|
|
||||||
self.lines = newlines
|
self.lines = newlines
|
||||||
|
|
||||||
def render(self, diff=None):
|
def render(self):
|
||||||
"""
|
"""
|
||||||
Render this crontab as it would be in the crontab.
|
Render this crontab as it would be in the crontab.
|
||||||
"""
|
"""
|
||||||
|
@ -469,7 +471,7 @@ class CronTab(object):
|
||||||
crons.append(cron)
|
crons.append(cron)
|
||||||
|
|
||||||
result = '\n'.join(crons)
|
result = '\n'.join(crons)
|
||||||
if result and not diff:
|
if result:
|
||||||
result = result.rstrip('\r\n') + '\n'
|
result = result.rstrip('\r\n') + '\n'
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -586,7 +588,7 @@ def main():
|
||||||
|
|
||||||
if module._diff:
|
if module._diff:
|
||||||
diff = dict()
|
diff = dict()
|
||||||
diff['before'] = crontab.render(diff=True)
|
diff['before'] = crontab.existing
|
||||||
if crontab.cron_file:
|
if crontab.cron_file:
|
||||||
diff['before_header'] = crontab.cron_file
|
diff['before_header'] = crontab.cron_file
|
||||||
else:
|
else:
|
||||||
|
@ -666,7 +668,7 @@ def main():
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
# no changes to env/job, but existing crontab needs a terminating newline
|
# no changes to env/job, but existing crontab needs a terminating newline
|
||||||
if not changed and not crontab.terminated:
|
if not changed and not crontab.existing.endswith(('\r', '\n')):
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
res_args = dict(
|
res_args = dict(
|
||||||
|
|
Loading…
Reference in a new issue