diff options
author | Sébastien Dailly <sebastien@chimrod.com> | 2013-05-08 11:06:31 +0200 |
---|---|---|
committer | Sébastien Dailly <sebastien@chimrod.com> | 2013-05-08 19:30:28 +0200 |
commit | 5bd3c5ca95731e81ceaabedc04367e832ffbf5bb (patch) | |
tree | a98c63ca4cfbbbc47c62611f7973f36ac8b1b72c /content/resources/fcron2cron.py | |
parent | 22e881fa89e4b3b8f6207d2c2e8713c2ff919400 (diff) |
New article on fcron to cron migration
Diffstat (limited to 'content/resources/fcron2cron.py')
-rw-r--r-- | content/resources/fcron2cron.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/content/resources/fcron2cron.py b/content/resources/fcron2cron.py new file mode 100644 index 0000000..26bd4e1 --- /dev/null +++ b/content/resources/fcron2cron.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +import subprocess +import os + +FCRON_LIST="/var/spool/fcron" + +def transform_variable(variables): + """ Transform a list of variables like + bootrun(true),nice(15),serial(true) + """ + + variable_operators = { + 'mail': lambda x: x == "false" and 'MAILTO=""' or "", + 'mailto': lambda x : 'MAILTO="%s"' %x, + + } + + transforms = "" + + for variable in variables.split(","): + attribute, value = variable.split('(') + transforms += variable_operators.get(attribute, lambda x:"")(value[:-1]) + return transforms + +def transform_directive(line): + operators = { + 'hourly': ('@hourly', 2), + 'midhourly': ('@hourly', 2), + 'daily': ('@daily', 3), + 'middaily': ('@daily', 3), + 'nightly': ('@daily', 3), + 'weekly': ('@weekly', 3), + 'midweekly': ('@weekly', 3), + 'montly': ('@montly', 4), + 'midmonthly': ('@montly', 4), + } + operator = line.split(" ")[0].split(',')[0] + substitute, to_trim = operators.get(operator, ("", 0)) + return "%s\t%s" %(substitute, line.split(None, to_trim)[to_trim]) + +def transform(line): + if line == '': + return '' + transformation = { + '!': lambda x:transform_variable(x[1:]), + '%': lambda x:transform_directive(x[1:]), + '&': lambda x: x.split(None, 1)[1], + } + return transformation.get(line[0], lambda x:x)(line) + +def main(parameters): + configs = os.listdir(FCRON_LIST) + for config in configs: + process = subprocess.Popen(["fcrontab", "-l", config], universal_newlines=True, stdout=subprocess.PIPE) + f = open('%s.crontab' % config, 'w') + for line in process.stdout: + transformation = transform(line.strip().expandtabs(1)) + if transformation is not None: + f.write("%s\n" % transformation) + f.close() + + +if __name__ == "__main__": + main(sys.argv) + |