Skip to content

Commit

Permalink
tighten crontab and crontab-u parser variable detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjonbrazil committed Aug 5, 2020
1 parent 549780c commit f3d84bd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
jc changelog

20200805 v1.13.4
- Updae crontab and crontab-u parsers to tighten up variable detection

20200804 v1.13.3
- Updae ping parser for Raspberry Pi compatibility

Expand Down
2 changes: 1 addition & 1 deletion jc/parsers/crontab.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def parse(data, raw=False, quiet=False):
# Pop any variable assignment lines
cron_var = []
for i, line in reversed(list(enumerate(cleandata))):
if '=' in line:
if '=' in line and not line.strip()[0].isdigit() and not line.strip()[0] == '@':
var_line = cleandata.pop(i)
var_name = var_line.split('=', maxsplit=1)[0].strip()
var_value = var_line.split('=', maxsplit=1)[1].strip()
Expand Down
4 changes: 2 additions & 2 deletions jc/parsers/crontab_u.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@


class info():
version = '1.3'
version = '1.5'
description = 'crontab file parser with user support'
author = 'Kelly Brazil'
author_email = '[email protected]'
Expand Down Expand Up @@ -236,7 +236,7 @@ def parse(data, raw=False, quiet=False):
# Pop any variable assignment lines
cron_var = []
for i, line in reversed(list(enumerate(cleandata))):
if '=' in line:
if '=' in line and not line.strip()[0].isdigit() and not line.strip()[0] == '@':
var_line = cleandata.pop(i)
var_name = var_line.split('=', maxsplit=1)[0].strip()
var_value = var_line.split('=', maxsplit=1)[1].strip()
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/debian10/crontab-u.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"variables": [], "schedule": [{"minute": ["30"], "hour": ["3"], "day_of_month": ["*"], "month": ["*"], "day_of_week": ["0"], "user": "root", "command": "test -e /run/systemd/system || SERVICE_MODE=1 /usr/lib/x86_64-linux-gnu/e2fsprogs/e2scrub_all_cron"}, {"minute": ["10"], "hour": ["3"], "day_of_month": ["*"], "month": ["*"], "day_of_week": ["*"], "user": "root", "command": "test -e /run/systemd/system || SERVICE_MODE=1 /sbin/e2scrub_all -A -r"}]}
2 changes: 2 additions & 0 deletions tests/fixtures/debian10/crontab-u.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
30 3 * * 0 root test -e /run/systemd/system || SERVICE_MODE=1 /usr/lib/x86_64-linux-gnu/e2fsprogs/e2scrub_all_cron
10 3 * * * root test -e /run/systemd/system || SERVICE_MODE=1 /sbin/e2scrub_all -A -r
12 changes: 12 additions & 0 deletions tests/test_crontab_u.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ def setUp(self):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/crontab-u.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_crontab_u = f.read()

with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/crontab-u.out'), 'r', encoding='utf-8') as f:
self.debian10_crontab_u = f.read()

# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/crontab-u.json'), 'r', encoding='utf-8') as f:
self.ubuntu_18_4_crontab_u_json = json.loads(f.read())

with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/crontab-u.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_crontab_u_json = json.loads(f.read())

with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/debian10/crontab-u.json'), 'r', encoding='utf-8') as f:
self.debian10_crontab_u_json = json.loads(f.read())

def test_crontab_u_nodata(self):
"""
Test 'crontab' with no data (has a user field)
Expand All @@ -41,6 +47,12 @@ def test_crontab_u_centos_7_7(self):
"""
self.assertEqual(jc.parsers.crontab_u.parse(self.centos_7_7_crontab_u, quiet=True), self.centos_7_7_crontab_u_json)

def test_crontab_u_debian10(self):
"""
Test 'crontab' on Debian10 (has a user field)
"""
self.assertEqual(jc.parsers.crontab_u.parse(self.debian10_crontab_u, quiet=True), self.debian10_crontab_u_json)


if __name__ == '__main__':
unittest.main()

0 comments on commit f3d84bd

Please sign in to comment.