diff --git a/CHANGELOG b/CHANGELOG index 0a7f3ad0..9db03f65 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ jc changelog 20201229 v1.14.0 - Add hashsum parser tested on linux, macos +- Add hash parser tested on linux, macos - Add cksum parser tested on linux, macos - Add python 3.9 to github automation tests diff --git a/EXAMPLES.md b/EXAMPLES.md index 69b7c509..73c53a29 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -175,6 +175,29 @@ blkid -o udev -ip /dev/sda2 | jc --blkid -p # or: jc -p blkid -o udev } ] ``` +### cksum +```bash +cksum * | jc --cksum -p # or: jc -p cksum * +``` +```json +[ + { + "filename": "__init__.py", + "checksum": 4294967295, + "blocks": 0 + }, + { + "filename": "airport.py", + "checksum": 2208551092, + "blocks": 3745 + }, + { + "filename": "airport_s.py", + "checksum": 1113817598, + "blocks": 4572 + } +] +``` ### crontab ```bash cat /etc/crontab | jc --crontab -p # or: jc -p crontab -l @@ -843,6 +866,54 @@ cat /etc/gshadow | jc --gshadow -p } ] ``` +### hash +```bash +hash | jc --hash -p +``` +```json +[ + { + "hits": 2, + "command": "/bin/cat" + }, + { + "hits": 1, + "command": "/bin/ls" + } +] +``` +### hashsum +```bash +md5sum * | jc --hashsum -p # or: jc -p md5sum * +``` +```json +[ + { + "filename": "devtoolset-3-gcc-4.9.2-6.el7.x86_64.rpm", + "hash": "65fc958c1add637ec23c4b137aecf3d3" + }, + { + "filename": "digout", + "hash": "5b9312ee5aff080927753c63a347707d" + }, + { + "filename": "dmidecode.out", + "hash": "716fd11c2ac00db109281f7110b8fb9d" + }, + { + "filename": "file with spaces in the name", + "hash": "d41d8cd98f00b204e9800998ecf8427e" + }, + { + "filename": "id-centos.out", + "hash": "4295be239a14ad77ef3253103de976d2" + }, + { + "filename": "ifcfg.json", + "hash": "01fda0d9ba9a75618b072e64ff512b43" + } +] +``` ### history ```bash history | jc --history -p diff --git a/docgen.sh b/docgen.sh index 99f27e18..7d053fc1 100755 --- a/docgen.sh +++ b/docgen.sh @@ -24,6 +24,7 @@ pydocmd simple jc.parsers.free+ > ../docs/parsers/free.md pydocmd simple jc.parsers.fstab+ > ../docs/parsers/fstab.md pydocmd simple jc.parsers.group+ > ../docs/parsers/group.md pydocmd simple jc.parsers.gshadow+ > ../docs/parsers/gshadow.md +pydocmd simple jc.parsers.hash+ > ../docs/parsers/hash.md pydocmd simple jc.parsers.hashsum+ > ../docs/parsers/hashsum.md pydocmd simple jc.parsers.history+ > ../docs/parsers/history.md pydocmd simple jc.parsers.hosts+ > ../docs/parsers/hosts.md diff --git a/docs/parsers/hash.md b/docs/parsers/hash.md new file mode 100644 index 00000000..0179a34f --- /dev/null +++ b/docs/parsers/hash.md @@ -0,0 +1,78 @@ + +# jc.parsers.hash +jc - JSON CLI output utility `hash` command output parser + +Usage (cli): + + $ hash | jc --hash + +Usage (module): + + import jc.parsers.hash + result = jc.parsers.hash.parse(hash_command_output) + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'aix', 'freebsd' + +Examples: + + $ hash | jc --hash -p + [ + { + "hits": 2, + "command": "/bin/cat" + }, + { + "hits": 1, + "command": "/bin/ls" + } + ] + + +## info +```python +info() +``` + + +## process +```python +process(proc_data) +``` + +Final processing to conform to the schema. + +Parameters: + + proc_data: (dictionary) raw structured data to process + +Returns: + + List of dictionaries. Structured data with the following schema: + + [ + { + "command": string, + "hits": integer + } + ] + + +## parse +```python +parse(data, raw=False, quiet=False) +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + +Returns: + + List of dictionaries. Raw or processed structured data. + diff --git a/jc/cli.py b/jc/cli.py index 8781da2f..1feac4f4 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -49,6 +49,7 @@ parsers = [ 'fstab', 'group', 'gshadow', + 'hash', 'hashsum', 'history', 'hosts', diff --git a/jc/parsers/hash.py b/jc/parsers/hash.py new file mode 100644 index 00000000..9ef08d01 --- /dev/null +++ b/jc/parsers/hash.py @@ -0,0 +1,108 @@ +"""jc - JSON CLI output utility `hash` command output parser + +Usage (cli): + + $ hash | jc --hash + +Usage (module): + + import jc.parsers.hash + result = jc.parsers.hash.parse(hash_command_output) + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'aix', 'freebsd' + +Examples: + + $ hash | jc --hash -p + [ + { + "hits": 2, + "command": "/bin/cat" + }, + { + "hits": 1, + "command": "/bin/ls" + } + ] +""" +import jc.utils +import jc.parsers.universal + + +class info(): + version = '1.0' + description = 'hash command parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + + # compatible options: linux, darwin, cygwin, win32, aix, freebsd + compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd'] + + +__version__ = info.version + + +def process(proc_data): + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (dictionary) raw structured data to process + + Returns: + + List of dictionaries. Structured data with the following schema: + + [ + { + "command": string, + "hits": integer + } + ] + """ + for entry in proc_data: + # change to int + int_list = ['hits'] + for key in int_list: + if key in entry: + try: + key_int = int(entry[key]) + entry[key] = key_int + except (ValueError): + entry[key] = None + + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + + Returns: + + List of dictionaries. Raw or processed structured data. + """ + if not quiet: + jc.utils.compatibility(__name__, info.compatible) + + cleandata = data.splitlines() + raw_output = [] + + if jc.utils.has_data(data): + + cleandata[0] = cleandata[0].lower() + raw_output = jc.parsers.universal.simple_table_parse(cleandata) + + if raw: + return raw_output + else: + return process(raw_output)