Split metric-bench's main into run_scores and measured, find the checkout from the history, drop an ambiguous shortened path, write places from the repository root, and give item 85 the history's reader

This commit is contained in:
2026-10-08 21:35:54 +02:00
parent 382472bd39
commit 8a2dbd70f1
7 changed files with 56 additions and 38 deletions
+29 -27
View File
@@ -2,8 +2,9 @@
usage: metric-bench [history]
Run it inside a project checkout. history defaults to docs/comprehension-history.md there. For each
scoring run in it, at the commit the seats read, it measures every Python function and method git
history defaults to docs/comprehension-history.md in the checkout it runs in, and the bench reads
the checkout that holds the history. For each scoring run in it, at the commit the seats read, it
measures every Python function and method git
tracks, test files left out: cognitive complexity (SonarSource's white paper, read for Python),
length in lines, shared state (attributes on self another method of the class also writes, plus
names declared global or nonlocal), and change spread (how many other files changed in a commit with
@@ -22,8 +23,8 @@ import sys
from metric_bench.bench import bench, co_changed, medians, METRICS, RunScores, Score
from failure import Failure
from metric_bench.places import resolved, scoring_runs
from metric_bench.python_units import python_units, Unit
from metric_bench.python_units import measured, python_units
from metric_bench.places import resolved, scoring_runs, ScoringRun
def main(argv: list[str]) -> int:
@@ -33,26 +34,22 @@ def main(argv: list[str]) -> int:
if len(argv) > 1 or argv[:1] and argv[0].startswith("-"):
print(f"unknown arguments {' '.join(argv)}; usage: metric-bench [history]", file=sys.stderr)
return 1
top = git(["rev-parse", "--show-toplevel"], None)
if isinstance(top, Failure):
return failed(top)
history = pathlib.Path(argv[0]) if argv else pathlib.Path(top.strip(), "docs", "comprehension-history.md")
given = pathlib.Path(argv[0]).resolve() if argv else None
found = git(["rev-parse", "--show-toplevel"], str(given.parent) if given else None)
if isinstance(found, Failure):
return failed(found)
top = found.strip()
history = given or pathlib.Path(top, "docs", "comprehension-history.md")
try:
text = history.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as failure:
return failed(Failure(f"could not read {history}: {failure}"))
rows = []
for run in scoring_runs(text):
tracked = git(["ls-tree", "-r", "--name-only", run.commit], top.strip())
if isinstance(tracked, Failure):
return failed(tracked)
units = commit_units(run.commit, tracked.split("\n"), top.strip())
if isinstance(units, Failure):
return failed(units)
log = git(["log", "--no-merges", "--format=format:@@", "--name-only", run.commit], top.strip())
if isinstance(log, Failure):
return failed(log)
if scores := bench(units, resolved(run.places, tracked.split("\n")), co_changed(log)):
scores = run_scores(run, top)
if isinstance(scores, Failure):
return failed(scores)
if scores:
rows.append((run.heading, scores))
if not rows:
return failed(Failure(f"no scoring run in {history} names a Python function or method the bench measures"))
@@ -60,16 +57,21 @@ def main(argv: list[str]) -> int:
return 0
def commit_units(commit: str, tracked: list[str], top: str) -> list[Unit] | Failure:
def run_scores(run: ScoringRun, top: str) -> RunScores | None | Failure:
listed = git(["ls-tree", "-r", "--name-only", run.commit], top)
if isinstance(listed, Failure):
return listed
tracked = listed.split("\n")
units = []
for path in tracked:
name = path.rsplit("/", 1)[-1]
if path.endswith(".py") and not name.startswith("test_") and not name.endswith("_test.py") and "tests" not in path.split("/")[:-1]:
source = git(["show", f"{commit}:{path}"], top)
if isinstance(source, Failure):
return source
units += python_units(path, source)
return units
for path in filter(measured, tracked):
source = git(["show", f"{run.commit}:{path}"], top)
if isinstance(source, Failure):
return source
units += python_units(path, source)
log = git(["log", "--no-merges", "--format=format:@@", "--name-only", run.commit], top)
if isinstance(log, Failure):
return log
return bench(units, resolved(run.places, tracked), co_changed(log))
def table(rows: list[tuple[str, RunScores]]) -> str: