1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

tighten crontab and crontab-u parser variable detection

This commit is contained in:
Kelly Brazil
2020-08-05 11:28:37 -07:00
parent 549780c232
commit f3d84bd5bf
6 changed files with 21 additions and 3 deletions

View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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
tests/fixtures/debian10/crontab-u.out vendored Normal file
View File

@ -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

View File

@ -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()