From 99b92a15bbebc5568f4455f388f945613c4a2759 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Mon, 16 Dec 2019 17:45:34 -0800 Subject: [PATCH] support shortcut schedules --- jc/parsers/crontab.py | 40 +++++++++++++++++++-------- tests/fixtures/centos-7.7/crontab.out | 11 ++++++-- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/jc/parsers/crontab.py b/jc/parsers/crontab.py index f66d2f5f..26c70b79 100644 --- a/jc/parsers/crontab.py +++ b/jc/parsers/crontab.py @@ -22,7 +22,7 @@ import jc.parsers.universal class info(): version = '1.0' - description = '/etc/crontab file parser' + description = 'crontab file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' # details = 'enter any other details here' @@ -45,6 +45,7 @@ def process(proc_data): [ { + "occurrence" string, "minute": [ string ], @@ -66,12 +67,15 @@ def process(proc_data): """ # put itmes in lists - for entry in proc_data['schedule']: - entry['minute'] = entry['minute'].split(',') - entry['hour'] = entry['hour'].split(',') - entry['day_of_month'] = entry['day_of_month'].split(',') - entry['month'] = entry['month'].split(',') - entry['day_of_week'] = entry['day_of_week'].split(',') + try: + for entry in proc_data['schedule']: + entry['minute'] = entry['minute'].split(',') + entry['hour'] = entry['hour'].split(',') + entry['day_of_month'] = entry['day_of_month'].split(',') + entry['month'] = entry['month'].split(',') + entry['day_of_week'] = entry['day_of_week'].split(',') + except (KeyError): + pass return proc_data @@ -109,23 +113,37 @@ def parse(data, raw=False, quiet=False): for i, line in reversed(list(enumerate(cleandata))): if line.find('=') != -1: var_line = cleandata.pop(i) - var_name = var_line.split('=', maxsplit=1)[0] - var_value = var_line.split('=', maxsplit=1)[1] + var_name = var_line.split('=', maxsplit=1)[0].strip() + var_value = var_line.split('=', maxsplit=1)[1].strip() cron_var.append({'name': var_name, 'value': var_value}) raw_output['variables'] = cron_var - # TODO: support @shortcuts + # Pop any shortcut lines + shortcut_list = [] + for i, line in reversed(list(enumerate(cleandata))): + if line.strip().startswith('@'): + shortcut_line = cleandata.pop(i) + occurrence = shortcut_line.split(maxsplit=1)[0].strip().lstrip('@') + cmd = shortcut_line.split(maxsplit=1)[1].strip() + shortcut_list.append({'occurrence': occurrence, + 'command': cmd}) + + print(shortcut_list) # Add header row for parsing - cleandata[0] = 'minute hour day_of_month month day_of_week username command' + cleandata[0] = 'minute hour day_of_month month day_of_week command' if len(cleandata) > 1: cron_list = jc.parsers.universal.simple_table_parse(cleandata) raw_output['schedule'] = cron_list + # Add shortcut entries back in + for item in shortcut_list: + raw_output['schedule'].append(item) + if raw: return raw_output else: diff --git a/tests/fixtures/centos-7.7/crontab.out b/tests/fixtures/centos-7.7/crontab.out index 7f1f0b4a..6a323af2 100644 --- a/tests/fixtures/centos-7.7/crontab.out +++ b/tests/fixtures/centos-7.7/crontab.out @@ -24,10 +24,10 @@ MAILTO=root * * * * * /var/www/devdaily.com/bin/check-apache.sh # generate links to new blog posts twice a day -5 10,22 * * * /var/www/devdaily.com/bin/mk-new-links.php +5 10-11,22 * * * /var/www/devdaily.com/bin/mk-new-links.php # run the backup scripts at 4:30am -30 4 * * * /var/www/devdaily.com/bin/create-all-backups.sh +30 4/2 * * * /var/www/devdaily.com/bin/create-all-backups.sh # re-generate the blog "categories" list (four times a day) 5 0,4,10,16 * * * /var/www/devdaily.com/bin/create-cat-list.sh @@ -35,9 +35,14 @@ MAILTO=root # reset the contact form just after midnight 5 0 * * * /var/www/devdaily.com/bin/resetContactForm.sh +# example of shortcut versions +@monthly /home/maverick/bin/tape-backup +@reboot /home/cleanup +@yearly /home/maverick/bin/annual-maintenance + # rotate the ad banners every five minutes 0,20,40 * * * * /var/www/bin/ads/freshMint.sh 5,25,45 * * * * /var/www/bin/ads/greenTaffy.sh 10,30,50 * * * * /var/www/bin/ads/raspberry.sh -15,35,55 * * * * /var/www/bin/ads/robinsEgg.sh \ No newline at end of file +15,35,55 * * * * /var/www/bin/ads/robinsEgg.sh