This commit is contained in:
Konstantin Stepanov 2014-04-22 08:14:47 -07:00
parent 22e8f305be
commit 72b502fd6f

View File

@ -39,11 +39,17 @@ def parse_crontab(filename, withuser=True, monotonic=False):
if monotonic:
period, delay, jobid = parts[0:3]
command = parts[3:]
period = {
'1': 'daily',
'7': 'weekly',
'@annually': 'yearly'
}.get(period, None) or period.lstrip('@')
yield {
'e': ' '.join('"%s=%s"' % kv for kv in environment.iteritems()),
'l': line,
'p': period.lstrip('@'),
'f': filename,
'p': period,
'd': delay,
'j': jobid,
'c': ' '.join(command),
@ -52,12 +58,19 @@ def parse_crontab(filename, withuser=True, monotonic=False):
else:
if line.startswith('@'):
period = parts[0].lstrip('@')
period = parts[0]
period = {
'1': 'daily',
'7': 'weekly',
'@annually': 'yearly'
}.get(period, None) or period.lstrip('@')
user, command = (parts[1], parts[2:]) if withuser else (basename, parts[1:])
yield {
'e': ' '.join('"%s=%s"' % kv for kv in environment.iteritems()),
'l': line,
'f': filename,
'p': period,
'u': user,
'c': ' '.join(command)
@ -70,6 +83,7 @@ def parse_crontab(filename, withuser=True, monotonic=False):
yield {
'e': ' '.join('"%s=%s"' % kv for kv in environment.iteritems()),
'l': line,
'f': filename,
'm': parse_time_unit(minutes, range(0, 60)),
'h': parse_time_unit(hours, range(0, 24)),
'd': parse_time_unit(days, range(0, 32)),
@ -125,16 +139,24 @@ def generate_timer_unit(job, seq):
if job['p'] == 'reboot':
schedule = 'OnBootSec=5m'
else:
schedule = 'OnCalendar=%s' % job['p']
try:
schedule = 'OnCalendar=*-*-1/%s 0:%s:0' % (int(job['p']), job.get('d', 0))
except ValueError:
schedule = 'OnCalendar=%s' % job['p']
accuracy = job.get('d', 1)
else:
dows = ','.join(job['w'])
dows = '' if dows == '*' else dows + ' '
schedule = 'OnCalendar=%s*-%s-%s %s:%s:00' % (dows, ','.join(map(str, job['M'])),
','.join(map(str, job['d'])), ','.join(map(str, job['h'])), ','.join(map(str, job['m'])))
accuracy = 1
with open('%s/%s.timer' % (TARGER_DIR, unit_name), 'w') as f:
f.write('''# Automatically generated by %s
# Source crontab: %s
[Unit]
Description=[Cron] "%s"
@ -145,12 +167,13 @@ RefuseManualStop=true
[Timer]
Unit=%s.service
Persistent=true
AccuracySec=1m
AccuracySec=%sm
%s
''' % (SELF, job['l'], unit_name, schedule))
''' % (SELF, job['f'], job['l'], unit_name, accuracy, schedule))
with open('%s/%s.service' % (TARGER_DIR, unit_name), 'w') as f:
f.write('''# Automatically generated by %s
# Source crontab: %s
[Unit]
Description=[Cron] "%s"
@ -162,7 +185,7 @@ Type=oneshot
User=%s
Environment=%s
ExecStart=/bin/sh -c '%s'
''' % (SELF, job['l'], job['u'], job['e'], job['c']))
''' % (SELF, job['f'], job['l'], job['u'], job['e'], job['c']))
return '%s.timer' % unit_name
@ -182,12 +205,12 @@ for filename in CRONTAB_FILES:
except IOError:
pass
#for filename in ANACRONTAB_FILES:
#try:
#for job in parse_crontab(filename, monotonic=True):
#requirements.append(generate_timer_unit(job, seqs.setdefault(job['u'], count())))
#except IOError:
#pass
for filename in ANACRONTAB_FILES:
try:
for job in parse_crontab(filename, monotonic=True):
requirements.append(generate_timer_unit(job, seqs.setdefault(job['u'], count())))
except IOError:
pass
for filename in USERCRONTAB_FILES:
try: