radn-dev/metrics/metrics.py
2023-08-02 02:54:34 +00:00

49 lines
1.5 KiB
Python

import json
from pathlib import Path
from subprocess import check_output
def filter_schema(obj, schema):
if schema is True:
return obj
return {key: filter_schema(obj[key], value) for key, value in schema.items()}
entry_schema = {
"name": True,
"metrics": {
"halstead": {"bugs": True, "difficulty": True},
"loc": {"ploc": True, "cloc": True, "blank": True},
},
}
SOURCE = Path(__file__).read_text()
cache_path = Path("/code/cache/cache.json")
cache_path.parent.mkdir(exist_ok=True)
try:
with cache_path.open("r") as file:
cache: dict = json.load(file)
if cache.get("SOURCE") != SOURCE:
cache.clear()
except FileNotFoundError:
cache = {}
cache["SOURCE"] = SOURCE
commits = check_output(["git", "log", "--pretty=%H"], text=True).splitlines()
entries = []
for i, commit in enumerate(commits):
if commit not in cache:
print("running", commit)
check_output(["git", "checkout", commit], text=True)
lines = check_output(["rust-code-analysis-cli", "-m", "-p", "src", "-O", "json"], text=True).splitlines()
print(len(lines))
commit_entries = []
for line in lines:
entry = json.loads(line)
commit_entries.append(filter_schema(entry, entry_schema))
cache[commit] = commit_entries
entries.extend((i, entry) for entry in cache[commit])
with cache_path.open("w") as file:
json.dump(cache, file)
with open("/code/metrics.json", "w") as file:
json.dump(entries, file)