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

fix incorrect variable parsing when wildcard schedule

This commit is contained in:
Kelly Brazil
2023-04-02 13:18:16 -07:00
parent 313bd86e3e
commit 0bf6f7cd7c
11 changed files with 43 additions and 6 deletions

View File

@ -2,6 +2,7 @@ jc changelog
20230402 v1.23.2
- Fix `iwconfig` command parser for SSIDs with dashes in the name
- Fix `crontab` command parsers for incorrect variable parsing in some cases
20230323 v1.23.1
- Fix `zpool-status` command parser for lines that start with tab

View File

@ -196,4 +196,4 @@ Returns:
### Parser Information
Compatibility: linux, darwin, aix, freebsd
Version 1.7 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@ -193,4 +193,4 @@ Returns:
### Parser Information
Compatibility: linux, darwin, aix, freebsd
Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com)
Version 1.9 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@ -174,7 +174,7 @@ import jc.parsers.universal
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.7'
version = '1.8'
description = '`crontab` command and file parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@ -245,7 +245,11 @@ 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 and not line.strip()[0].isdigit() and not line.strip()[0] == '@':
if '=' in line \
and not line.strip()[0].isdigit() \
and not line.strip()[0] == '@' \
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

@ -171,7 +171,7 @@ import jc.parsers.universal
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.8'
version = '1.9'
description = '`crontab` file parser with user support'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@ -241,7 +241,11 @@ 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 and not line.strip()[0].isdigit() and not line.strip()[0] == '@':
if '=' in line \
and not line.strip()[0].isdigit() \
and not line.strip()[0] == '@' \
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":["*"],"hour":["*"],"day_of_month":["*"],"month":["*"],"day_of_week":["*"],"user":"root","command":"/bin/bash -l -c \"python3 my_script.py --output-file=/opt/output.json\""}]}

View File

@ -0,0 +1 @@
* * * * * root /bin/bash -l -c "python3 my_script.py --output-file=/opt/output.json"

View File

@ -0,0 +1 @@
{"variables":[],"schedule":[{"minute":["*"],"hour":["*"],"day_of_month":["*"],"month":["*"],"day_of_week":["*"],"command":"/bin/bash -l -c \"python3 my_script.py --output-file=/opt/output.json\""}]}

View File

@ -0,0 +1 @@
* * * * * /bin/bash -l -c "python3 my_script.py --output-file=/opt/output.json"

View File

@ -15,6 +15,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-no-normal-entries.out'), 'r', encoding='utf-8') as f:
generic_crontab_no_normal_entries = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-var-fix.out'), 'r', encoding='utf-8') as f:
generic_crontab_var_fix = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/crontab.json'), 'r', encoding='utf-8') as f:
centos_7_7_crontab_json = json.loads(f.read())
@ -22,6 +25,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-no-normal-entries.json'), 'r', encoding='utf-8') as f:
generic_crontab_no_normal_entries_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-var-fix.json'), 'r', encoding='utf-8') as f:
generic_crontab_var_fix_json = json.loads(f.read())
def test_crontab_nodata(self):
"""
@ -41,6 +47,12 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.crontab.parse(self.generic_crontab_no_normal_entries, quiet=True), self.generic_crontab_no_normal_entries_json)
def test_crontab_var_fix(self):
"""
Test 'crontab' with wildcard schedule should not generate variables from command line section
"""
self.assertEqual(jc.parsers.crontab.parse(self.generic_crontab_var_fix, quiet=True), self.generic_crontab_var_fix_json)
if __name__ == '__main__':
unittest.main()

View File

@ -21,6 +21,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-no-normal-entries.out'), 'r', encoding='utf-8') as f:
generic_crontab_u_no_normal_entries = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-var-fix.out'), 'r', encoding='utf-8') as f:
generic_crontab_u_var_fix = 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:
ubuntu_18_4_crontab_u_json = json.loads(f.read())
@ -34,6 +37,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-no-normal-entries.json'), 'r', encoding='utf-8') as f:
generic_crontab_u_no_normal_entries_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/crontab-u-var-fix.json'), 'r', encoding='utf-8') as f:
generic_crontab_u_var_fix_json = json.loads(f.read())
def test_crontab_u_nodata(self):
"""
@ -65,5 +71,11 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.crontab_u.parse(self.generic_crontab_u_no_normal_entries, quiet=True), self.generic_crontab_u_no_normal_entries_json)
def test_crontab_u_var_fix(self):
"""
Test 'crontab' with wildcard schedule should not generate variables from command line section
"""
self.assertEqual(jc.parsers.crontab_u.parse(self.generic_crontab_u_var_fix, quiet=True), self.generic_crontab_u_var_fix_json)
if __name__ == '__main__':
unittest.main()