From f3d84bd5bf06349b681e9b72d904676e9f6bca19 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 5 Aug 2020 11:28:37 -0700 Subject: [PATCH] tighten crontab and crontab-u parser variable detection --- CHANGELOG | 3 +++ jc/parsers/crontab.py | 2 +- jc/parsers/crontab_u.py | 4 ++-- tests/fixtures/debian10/crontab-u.json | 1 + tests/fixtures/debian10/crontab-u.out | 2 ++ tests/test_crontab_u.py | 12 ++++++++++++ 6 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/debian10/crontab-u.json create mode 100644 tests/fixtures/debian10/crontab-u.out diff --git a/CHANGELOG b/CHANGELOG index cb025111..db007f21 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/jc/parsers/crontab.py b/jc/parsers/crontab.py index ec73640e..a155dded 100644 --- a/jc/parsers/crontab.py +++ b/jc/parsers/crontab.py @@ -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() diff --git a/jc/parsers/crontab_u.py b/jc/parsers/crontab_u.py index 150501c2..0b3484c2 100644 --- a/jc/parsers/crontab_u.py +++ b/jc/parsers/crontab_u.py @@ -133,7 +133,7 @@ import jc.parsers.universal class info(): - version = '1.3' + version = '1.5' description = 'crontab file parser with user support' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -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() diff --git a/tests/fixtures/debian10/crontab-u.json b/tests/fixtures/debian10/crontab-u.json new file mode 100644 index 00000000..9113e3b9 --- /dev/null +++ b/tests/fixtures/debian10/crontab-u.json @@ -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"}]} diff --git a/tests/fixtures/debian10/crontab-u.out b/tests/fixtures/debian10/crontab-u.out new file mode 100644 index 00000000..711b0b29 --- /dev/null +++ b/tests/fixtures/debian10/crontab-u.out @@ -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 diff --git a/tests/test_crontab_u.py b/tests/test_crontab_u.py index ca36e7bc..e191599d 100644 --- a/tests/test_crontab_u.py +++ b/tests/test_crontab_u.py @@ -16,6 +16,9 @@ class MyTests(unittest.TestCase): 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()) @@ -23,6 +26,9 @@ class MyTests(unittest.TestCase): 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) @@ -41,6 +47,12 @@ class MyTests(unittest.TestCase): """ 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()