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

initial add du parser

This commit is contained in:
Kelly Brazil
2019-12-16 13:52:42 -08:00
parent 3a3016adb6
commit e882bf55bc
7 changed files with 8755 additions and 0 deletions

View File

@ -1,6 +1,7 @@
jc changelog jc changelog
2019xxxx v1.6.1 2019xxxx v1.6.1
- Add du parser (linux and OSX support)
- Add OSX support for the ifconfig, arp, df, mount, and uname parsers - Add OSX support for the ifconfig, arp, df, mount, and uname parsers
- Add tests for ls, dig, ps, w, uptime on OSX - Add tests for ls, dig, ps, w, uptime on OSX
- Add about option - Add about option

View File

@ -10,6 +10,7 @@ import jc.utils
import jc.parsers.arp import jc.parsers.arp
import jc.parsers.df import jc.parsers.df
import jc.parsers.dig import jc.parsers.dig
import jc.parsers.du
import jc.parsers.env import jc.parsers.env
import jc.parsers.free import jc.parsers.free
import jc.parsers.fstab import jc.parsers.fstab
@ -40,6 +41,7 @@ parser_map = {
'--arp': jc.parsers.arp, '--arp': jc.parsers.arp,
'--df': jc.parsers.df, '--df': jc.parsers.df,
'--dig': jc.parsers.dig, '--dig': jc.parsers.dig,
'--du': jc.parsers.du,
'--env': jc.parsers.env, '--env': jc.parsers.env,
'--free': jc.parsers.free, '--free': jc.parsers.free,
'--fstab': jc.parsers.fstab, '--fstab': jc.parsers.fstab,

148
jc/parsers/du.py Normal file
View File

@ -0,0 +1,148 @@
"""jc - JSON CLI output utility du Parser
Usage:
specify --du as the first argument if the piped input is coming from du
Compatibility:
'linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'
Examples:
$ du /usr | jc --du -p
[
{
"size": 104608,
"name": "/usr/bin"
},
{
"size": 56,
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/_CodeSignature"
},
{
"size": 0,
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/Firmware/usr/local/standalone"
},
{
"size": 0,
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/Firmware/usr/local"
},
{
"size": 0,
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/Firmware/usr"
},
{
"size": 1008,
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/Firmware/dfu"
},
...
]
$ du /usr | jc --du -p -r
[
{
"size": "104608",
"name": "/usr/bin"
},
{
"size": "56",
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/_CodeSignature"
},
{
"size": "0",
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/Firmware/usr/local/standalone"
},
{
"size": "0",
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/Firmware/usr/local"
},
{
"size": "0",
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/Firmware/usr"
},
{
"size": "1008",
"name": "/usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/Firmware/dfu"
},
...
]
"""
import jc.utils
import jc.parsers.universal
class info():
version = '1.0'
description = 'du parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
# details = 'enter any other details here'
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
def process(proc_data):
"""
Final processing to conform to the schema.
Parameters:
proc_data: (dictionary) raw structured data to process
Returns:
dictionary structured data with the following schema:
[
{
"size": integer,
"name": string
}
]
"""
int_list = ['size']
for entry in proc_data:
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:
dictionary raw or processed structured data
"""
if not quiet:
jc.utils.compatibility(__name__, info.compatible)
raw_output = []
cleandata = data.splitlines()
# Clear any blank lines
cleandata = list(filter(None, cleandata))
if cleandata:
cleandata.insert(0, 'size name')
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
if raw:
return raw_output
else:
return process(raw_output)

1
tests/fixtures/centos-7.7/du.json vendored Normal file

File diff suppressed because one or more lines are too long

5199
tests/fixtures/centos-7.7/du.out vendored Normal file

File diff suppressed because it is too large Load Diff

3364
tests/fixtures/osx-10.14.6/du.out vendored Normal file

File diff suppressed because it is too large Load Diff

40
tests/test_du.py Normal file
View File

@ -0,0 +1,40 @@
import os
import json
import unittest
import jc.parsers.du
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class MyTests(unittest.TestCase):
def setUp(self):
# input
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/du.out'), 'r') as f:
self.centos_7_7_du = f.read()
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/du.out'), 'r') as f:
# self.ubuntu_18_4_du = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/du.json'), 'r') as f:
self.centos_7_7_du_json = json.loads(f.read())
# with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/du.json'), 'r') as f:
# self.ubuntu_18_4_du_json = json.loads(f.read())
def test_du_centos_7_7(self):
"""
Test 'du' on Centos 7.7
"""
self.assertEqual(jc.parsers.du.parse(self.centos_7_7_du, quiet=True), self.centos_7_7_du_json)
# def test_du_ubuntu_18_4(self):
# """
# Test 'du' on Ubuntu 18.4
# """
# self.assertEqual(jc.parsers.du.parse(self.ubuntu_18_4_du, quiet=True), self.ubuntu_18_4_du_json)
if __name__ == '__main__':
unittest.main()