"""limits-baseline.json: the census that exists today. A total may only fall; a rewrite never raises one.""" import json from . import census COMMENT = ("The legacy census of scripts/check-limits.py: functions over each limit and lines over the width, per " "scope (prod, test) and crate. A total over all crates may only fall (a function moved between crates " "keeps it). " "Do not edit by hand; regenerate with scripts/check-limits.py --update-baseline. Policy: limits.json.") def empty(): return {"limits": {}, "census": {scope: {} for scope in census.SCOPES}} def load(path): return json.loads(path.read_text(encoding="utf-8")) if path.exists() else empty() def save(path, data): with open(path, "w", encoding="utf-8", newline="\n") as handle: handle.write(json.dumps(data, indent=1) + "\n") def build(counts, limits): return {"_comment": COMMENT, "limits": dict(limits), "census": {scope: dict(sorted(counts[scope].items())) for scope in census.SCOPES}} def raised(old, new): """Problems for a total that would rise (the first baseline may be written freely).""" before, after = census.totals(old["census"]), census.totals(new["census"]) return [f"{scope} {kind}: total {before[scope][kind]} -> {n}" for scope in census.SCOPES for kind, n in after[scope].items() if n > before[scope][kind]] def findings(counts, base): """(errors, stale): a total above the baseline is an error, one below it asks for a tighter baseline.""" now, before = census.totals(counts), census.totals(base["census"]) errors, stale = [], [] for scope in census.SCOPES: for kind, n in now[scope].items(): old = before[scope][kind] if n > old: errors.append(f"census {scope} {kind} rose to {n} (baseline {old}); the count may only fall") elif n < old: stale.append(f"census {scope} {kind} fell from {old} to {n}: tighten the baseline") return errors, stale