From 0bebb312dd208c61d508855e863a77b3ec88057d Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sat, 17 Sep 2022 12:32:15 -0700 Subject: [PATCH] add proc-version parser --- docs/parsers/proc_version.md | 79 ++++++++++++++++++++++ jc/lib.py | 1 + jc/parsers/proc_version.py | 127 +++++++++++++++++++++++++++++++++++ man/jc.1 | 7 +- 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 docs/parsers/proc_version.md create mode 100644 jc/parsers/proc_version.py diff --git a/docs/parsers/proc_version.md b/docs/parsers/proc_version.md new file mode 100644 index 00000000..178ca9e1 --- /dev/null +++ b/docs/parsers/proc_version.md @@ -0,0 +1,79 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_version + +jc - JSON Convert `/proc/version` file parser + +> Note: This parser will parse `/proc/version` files that follow the +> common format used by most popular distributions. + +Usage (cli): + + $ cat /proc/version | jc --proc + +or + + $ jc /proc/version + +or + + $ cat /proc/version | jc --proc-version + +Usage (module): + + import jc + result = jc.parse('proc', proc_version_file) + +or + + import jc + result = jc.parse('proc_version', proc_version_file) + +Schema: + + { + "version": string, + "email": string, + "gcc": string, + "build": string, + "flags": string/null, + "date": string + } + +Examples: + + $ cat /proc/version | jc --proc -p + { + "version": "5.8.0-63-generic", + "email": "buildd@lcy01-amd64-028", + "gcc": "gcc (Ubuntu 10.3.0-1ubuntu1~20.10) 10.3.0, GNU ld (GNU Binutils for Ubuntu) 2.35.1", + "build": "#71-Ubuntu", + "flags": "SMP", + "date": "Tue Jul 13 15:59:12 UTC 2021" + } + + + +### parse + +```python +def parse(data: str, raw: bool = False, quiet: bool = False) -> Dict +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + +Returns: + + Dictionary. Raw or processed structured data. + +### Parser Information +Compatibility: linux + +Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/lib.py b/jc/lib.py index 535240de..cee0f3e6 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -108,6 +108,7 @@ parsers = [ 'proc-stat', 'proc-swaps', 'proc-uptime', + 'proc-version', 'proc-pid-numa-maps', 'ps', 'route', diff --git a/jc/parsers/proc_version.py b/jc/parsers/proc_version.py new file mode 100644 index 00000000..89bab8a1 --- /dev/null +++ b/jc/parsers/proc_version.py @@ -0,0 +1,127 @@ +"""jc - JSON Convert `/proc/version` file parser + +> Note: This parser will parse `/proc/version` files that follow the +> common format used by most popular distributions. + +Usage (cli): + + $ cat /proc/version | jc --proc + +or + + $ jc /proc/version + +or + + $ cat /proc/version | jc --proc-version + +Usage (module): + + import jc + result = jc.parse('proc', proc_version_file) + +or + + import jc + result = jc.parse('proc_version', proc_version_file) + +Schema: + + { + "version": string, + "email": string, + "gcc": string, + "build": string, + "flags": string/null, + "date": string + } + +Examples: + + $ cat /proc/version | jc --proc -p + { + "version": "5.8.0-63-generic", + "email": "buildd@lcy01-amd64-028", + "gcc": "gcc (Ubuntu 10.3.0-1ubuntu1~20.10) 10.3.0, GNU ld (GNU Binutils for Ubuntu) 2.35.1", + "build": "#71-Ubuntu", + "flags": "SMP", + "date": "Tue Jul 13 15:59:12 UTC 2021" + } +""" +import re +from typing import Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc/version` file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + hidden = True + + +__version__ = info.version + + +# inspired by https://gist.github.com/ty2/ad61340e7a4067def2e3c709496bca9d +version_pattern = re.compile(r''' + Linux\ version\ (?P\S+)\s + \((?P\S+?)\)\s + \((?Pgcc.+)\)\s + (?P\#\d+(\S+)?)\s + (?P.*)? + (?P(Sun|Mon|Tue|Wed|Thu|Fri|Sat).+) + ''', re.VERBOSE +) + + +def _process(proc_data: Dict) -> Dict: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (Dictionary) raw structured data to process + + Returns: + + Dictionary. Structured to conform to the schema. + """ + return proc_data + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> Dict: + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output: Dict = {} + + if jc.utils.has_data(data): + version_match = version_pattern.match(data) + + if version_match: + + ver_dict = version_match.groupdict() + raw_output = {x: y.strip() or None for x, y in ver_dict.items()} + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index b59a8963..7d74df22 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2022-09-16 1.21.2 "JSON Convert" +.TH jc 1 2022-09-17 1.21.2 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings .SH SYNOPSIS @@ -525,6 +525,11 @@ PLIST file parser \fB--proc-uptime\fP `/proc/uptime` file parser +.TP +.B +\fB--proc-version\fP +`/proc/version` file parser + .TP .B \fB--proc-pid-numa-maps\fP