mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-17 00:07:37 +02:00
support shortcut schedules
This commit is contained in:
@ -22,7 +22,7 @@ import jc.parsers.universal
|
|||||||
|
|
||||||
class info():
|
class info():
|
||||||
version = '1.0'
|
version = '1.0'
|
||||||
description = '/etc/crontab file parser'
|
description = 'crontab file parser'
|
||||||
author = 'Kelly Brazil'
|
author = 'Kelly Brazil'
|
||||||
author_email = 'kellyjonbrazil@gmail.com'
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
# details = 'enter any other details here'
|
# details = 'enter any other details here'
|
||||||
@ -45,6 +45,7 @@ def process(proc_data):
|
|||||||
|
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
|
"occurrence" string,
|
||||||
"minute": [
|
"minute": [
|
||||||
string
|
string
|
||||||
],
|
],
|
||||||
@ -66,12 +67,15 @@ def process(proc_data):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
# put itmes in lists
|
# put itmes in lists
|
||||||
for entry in proc_data['schedule']:
|
try:
|
||||||
entry['minute'] = entry['minute'].split(',')
|
for entry in proc_data['schedule']:
|
||||||
entry['hour'] = entry['hour'].split(',')
|
entry['minute'] = entry['minute'].split(',')
|
||||||
entry['day_of_month'] = entry['day_of_month'].split(',')
|
entry['hour'] = entry['hour'].split(',')
|
||||||
entry['month'] = entry['month'].split(',')
|
entry['day_of_month'] = entry['day_of_month'].split(',')
|
||||||
entry['day_of_week'] = entry['day_of_week'].split(',')
|
entry['month'] = entry['month'].split(',')
|
||||||
|
entry['day_of_week'] = entry['day_of_week'].split(',')
|
||||||
|
except (KeyError):
|
||||||
|
pass
|
||||||
|
|
||||||
return proc_data
|
return proc_data
|
||||||
|
|
||||||
@ -109,23 +113,37 @@ def parse(data, raw=False, quiet=False):
|
|||||||
for i, line in reversed(list(enumerate(cleandata))):
|
for i, line in reversed(list(enumerate(cleandata))):
|
||||||
if line.find('=') != -1:
|
if line.find('=') != -1:
|
||||||
var_line = cleandata.pop(i)
|
var_line = cleandata.pop(i)
|
||||||
var_name = var_line.split('=', maxsplit=1)[0]
|
var_name = var_line.split('=', maxsplit=1)[0].strip()
|
||||||
var_value = var_line.split('=', maxsplit=1)[1]
|
var_value = var_line.split('=', maxsplit=1)[1].strip()
|
||||||
cron_var.append({'name': var_name,
|
cron_var.append({'name': var_name,
|
||||||
'value': var_value})
|
'value': var_value})
|
||||||
|
|
||||||
raw_output['variables'] = cron_var
|
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
|
# 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:
|
if len(cleandata) > 1:
|
||||||
cron_list = jc.parsers.universal.simple_table_parse(cleandata)
|
cron_list = jc.parsers.universal.simple_table_parse(cleandata)
|
||||||
|
|
||||||
raw_output['schedule'] = cron_list
|
raw_output['schedule'] = cron_list
|
||||||
|
|
||||||
|
# Add shortcut entries back in
|
||||||
|
for item in shortcut_list:
|
||||||
|
raw_output['schedule'].append(item)
|
||||||
|
|
||||||
if raw:
|
if raw:
|
||||||
return raw_output
|
return raw_output
|
||||||
else:
|
else:
|
||||||
|
11
tests/fixtures/centos-7.7/crontab.out
vendored
11
tests/fixtures/centos-7.7/crontab.out
vendored
@ -24,10 +24,10 @@ MAILTO=root
|
|||||||
* * * * * /var/www/devdaily.com/bin/check-apache.sh
|
* * * * * /var/www/devdaily.com/bin/check-apache.sh
|
||||||
|
|
||||||
# generate links to new blog posts twice a day
|
# 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
|
# 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)
|
# re-generate the blog "categories" list (four times a day)
|
||||||
5 0,4,10,16 * * * /var/www/devdaily.com/bin/create-cat-list.sh
|
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
|
# reset the contact form just after midnight
|
||||||
5 0 * * * /var/www/devdaily.com/bin/resetContactForm.sh
|
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
|
# rotate the ad banners every five minutes
|
||||||
|
|
||||||
0,20,40 * * * * /var/www/bin/ads/freshMint.sh
|
0,20,40 * * * * /var/www/bin/ads/freshMint.sh
|
||||||
5,25,45 * * * * /var/www/bin/ads/greenTaffy.sh
|
5,25,45 * * * * /var/www/bin/ads/greenTaffy.sh
|
||||||
10,30,50 * * * * /var/www/bin/ads/raspberry.sh
|
10,30,50 * * * * /var/www/bin/ads/raspberry.sh
|
||||||
15,35,55 * * * * /var/www/bin/ads/robinsEgg.sh
|
15,35,55 * * * * /var/www/bin/ads/robinsEgg.sh
|
||||||
|
Reference in New Issue
Block a user