Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
100 lines
4.0 KiB
Python
100 lines
4.0 KiB
Python
"""The baseline file: what may be written to it, and what a rewrite may never raise."""
|
|
import json
|
|
|
|
EMPTY = {"violations": [], "oversize": {}, "source": {}, "mechanical_names": []}
|
|
COMMENT = ("Debt that exists today. Every number may only fall (scripts/check-architecture.py --update-baseline "
|
|
"refuses to raise one). Do not edit by hand; regenerate. Policy: architecture.json.")
|
|
|
|
|
|
def load(path):
|
|
"""The baseline at `path`, or an empty one when the file does not exist."""
|
|
return json.loads(path.read_text(encoding="utf-8")) if path.exists() else json.loads(json.dumps(EMPTY))
|
|
|
|
|
|
def save(path, baseline):
|
|
with open(path, "w", encoding="utf-8", newline="\n") as handle:
|
|
handle.write(json.dumps(baseline, indent=1) + "\n")
|
|
|
|
|
|
def build(limit, violations, sizes, counts, names=()):
|
|
"""The baseline that describes the current state."""
|
|
return {
|
|
"_comment": COMMENT,
|
|
"violations": sorted(violations),
|
|
"oversize": {p: n for p, n in sorted(sizes.items()) if n > limit},
|
|
"source": dict(sorted(counts.items())),
|
|
"mechanical_names": sorted(names),
|
|
}
|
|
|
|
|
|
def _stem_root(path, oversize):
|
|
"""The oversize file `path` was split from: a same-stem directory (responses.rs -> responses/*.rs)."""
|
|
parts = path.split("/")
|
|
for depth in range(len(parts) - 1, 0, -1):
|
|
root = "/".join(parts[:depth]) + ".rs"
|
|
if root in oversize:
|
|
return root
|
|
return None
|
|
|
|
|
|
SPLIT_OVERHEAD = 3 # lines a split adds per new file: the `mod` declaration and its attributes
|
|
|
|
|
|
def _family_lines(old, new):
|
|
"""Lines of each original oversize file plus its same-stem split files; every new file may add its declaration."""
|
|
totals = {}
|
|
for path, n in new.items():
|
|
root = path if path in old else _stem_root(path, old)
|
|
if root:
|
|
overhead = 0 if path in old else SPLIT_OVERHEAD
|
|
totals[root] = totals.get(root, 0) + n - overhead
|
|
return totals
|
|
|
|
|
|
def _moved(path, lines, old, new):
|
|
"""Same base name and no more lines than an oversize entry that disappeared: the file moved."""
|
|
name = path.rsplit("/", 1)[-1]
|
|
gone = [n for p, n in old.items() if p not in new and p.rsplit("/", 1)[-1] == name]
|
|
return any(lines <= before for before in gone)
|
|
|
|
|
|
def _oversize_problem(path, lines, old, new, family):
|
|
if path in old:
|
|
grew = lines > old[path] and family[path] > old[path]
|
|
return f"oversize {path}: {old[path]} -> {lines}" if grew else None
|
|
root = _stem_root(path, old)
|
|
if root:
|
|
too_big = family[root] > old[root]
|
|
return f"oversize {path}: split of {root} holds {family[root]} lines, was {old[root]}" if too_big else None
|
|
return None if _moved(path, lines, old, new) else f"oversize {path}: new file with {lines} lines"
|
|
|
|
|
|
def raised_oversize(old, new):
|
|
"""Problems for an oversize section that gained lines. Moves and same-stem splits are not gains."""
|
|
family = _family_lines(old, new)
|
|
found = [_oversize_problem(p, n, old, new, family) for p, n in new.items()]
|
|
return [problem for problem in found if problem]
|
|
|
|
|
|
def _totals(section):
|
|
totals = {}
|
|
for key, n in section.items():
|
|
pid = key.split(":", 1)[0]
|
|
totals[pid] = totals.get(pid, 0) + n
|
|
return totals
|
|
|
|
|
|
def raised_source(old, new):
|
|
"""Problems for a source ratchet whose total over all crates rose (code that moved crates keeps its total)."""
|
|
before, after = _totals(old), _totals(new)
|
|
return [f"source {pid}: total {before.get(pid, 0)} -> {n}" for pid, n in after.items() if n > before.get(pid, 0)]
|
|
|
|
|
|
def raised(old, new):
|
|
"""Everything a baseline rewrite would raise."""
|
|
problems = [f"new violation {v}" for v in new["violations"] if v not in old["violations"]]
|
|
names = [f"new mechanical file name {p}" for p in new.get("mechanical_names", [])
|
|
if p not in old.get("mechanical_names", [])]
|
|
sections = raised_oversize(old["oversize"], new["oversize"]) + raised_source(old["source"], new["source"])
|
|
return problems + sections + names
|