mirror of
https://github.com/hohn/sarif-cli.git
synced 2025-12-15 17:03:04 +01:00
whole-file sarif-size
This commit is contained in:
committed by
=Michael Hohn
parent
edce50fb79
commit
9d20cd6304
36
bin/sarif-size
Executable file
36
bin/sarif-size
Executable file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python
|
||||
import orjson
|
||||
import sys
|
||||
|
||||
def sizeof(x):
|
||||
t = type(x)
|
||||
if t is dict:
|
||||
return sum(len(k) + sizeof(v) for k, v in x.items())
|
||||
if t is list:
|
||||
return sum(sizeof(v) for v in x)
|
||||
if t is str:
|
||||
return len(x)
|
||||
return 8 # numbers, bools, null
|
||||
|
||||
def walk(x, path="root"):
|
||||
if isinstance(x, dict):
|
||||
yield path, sizeof(x)
|
||||
for k,v in x.items():
|
||||
yield from walk(v, f"{path}.{k}")
|
||||
elif isinstance(x, list):
|
||||
yield path, sizeof(x)
|
||||
for i,v in enumerate(x):
|
||||
yield from walk(v, f"{path}[{i}]")
|
||||
|
||||
# with open(sys.stdin, 'rb') as f:
|
||||
# data_bytes = f.read()
|
||||
|
||||
data_bytes = sys.stdin.buffer.read()
|
||||
|
||||
parsed_data = orjson.loads(data_bytes)
|
||||
|
||||
sizes = sorted(walk(parsed_data), key=lambda kv: kv[1], reverse=True)
|
||||
for p, s in sizes[:50]:
|
||||
print(f"{s:10d} {p}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user