New and changed code is checked against the base (merge-base with the integration branch): every added or touched function and every added or changed line must be within the limits, unless limits-allowlist.json lists the function with a reason and an owner; an allowlisted function may only shrink. The legacy that exists today is a per-crate census in limits-baseline.json whose totals may only fall (same --update-baseline semantics as the architecture gate). Rust is measured from the rustsplit lexer, the gate's own Python from ast. 56 self-tests, including a temporary git repository.
74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
"""The whole-tree census: how many functions miss each limit, and how many lines are too wide, per crate and scope."""
|
|
from dataclasses import dataclass, field
|
|
|
|
from archcheck import measure as arch
|
|
|
|
from . import files, model, policy
|
|
|
|
SCOPES = ("prod", "test")
|
|
|
|
|
|
@dataclass
|
|
class Census:
|
|
counts: dict = field(default_factory=lambda: {scope: {} for scope in SCOPES}) # scope, crate, kind -> n
|
|
worst: list = field(default_factory=list) # (path, Fn) of every function over a limit
|
|
|
|
|
|
def crate_files(root, packages):
|
|
"""{relative path: crate}: a Rust file belongs to the deepest package directory that contains it."""
|
|
owner = {}
|
|
for pkg in sorted(packages, key=lambda p: len(p["dir"].parts), reverse=True):
|
|
for path in arch.rust_files(pkg["dir"]):
|
|
owner.setdefault(path.relative_to(root).as_posix(), pkg["name"])
|
|
return owner
|
|
|
|
|
|
def python_files(root, pol):
|
|
"""{relative path: "python"} for the Python the policy holds to the limits."""
|
|
found = {}
|
|
for prefix in pol.python_include:
|
|
target = root / prefix
|
|
paths = sorted(target.rglob("*.py")) if target.is_dir() else [target]
|
|
for path in paths:
|
|
if path.is_file() and "__pycache__" not in path.parts:
|
|
found[path.relative_to(root).as_posix()] = "python"
|
|
return found
|
|
|
|
|
|
def _wide_scope(n, fns, test_path):
|
|
return "test" if test_path or any(fn.test and fn.start <= n <= fn.end for fn in fns) else "prod"
|
|
|
|
|
|
def tally(rel, fns, wide, limits):
|
|
"""[(scope, kind, count)] for one file."""
|
|
rows = []
|
|
for fn in fns:
|
|
scope = "test" if fn.test else "prod"
|
|
rows += [(scope, kind, 1) for kind in model.over(fn.measures(), limits) if kind != "width"]
|
|
test_path = policy.is_test_path(rel)
|
|
rows += [(_wide_scope(n, fns, test_path), "width", 1) for n in wide]
|
|
return rows
|
|
|
|
|
|
def collect(root, pol, owners):
|
|
"""Scan every file of `owners` ({relative path: crate}) and count the functions and lines over the limits."""
|
|
result = Census()
|
|
for rel, crate in sorted(owners.items()):
|
|
if policy.language(pol, rel) is None:
|
|
continue
|
|
fns, wide = files.scan_file(root, pol, rel)
|
|
result.worst += [(rel, fn) for fn in fns if set(model.over(fn.measures(), pol.limits)) - {"width"}]
|
|
for scope, kind, n in tally(rel, fns, wide, pol.limits):
|
|
row = result.counts[scope].setdefault(crate, {})
|
|
row[kind] = row.get(kind, 0) + n
|
|
return result
|
|
|
|
|
|
def totals(counts):
|
|
"""{scope: {kind: n}} summed over the crates."""
|
|
out = {scope: dict.fromkeys(model.KINDS, 0) for scope in SCOPES}
|
|
for scope, crates in counts.items():
|
|
for row in crates.values():
|
|
for kind, n in row.items():
|
|
out[scope][kind] += n
|
|
return out |