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.
37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
"""The policy file limits.json: the numbers, what is out of scope, and which Python is held to the same limits."""
|
|
import json
|
|
from dataclasses import dataclass
|
|
|
|
TEST_DIRS = frozenset(("tests", "benches", "examples"))
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Policy:
|
|
limits: dict
|
|
tab_width: int
|
|
exclude: tuple
|
|
python_include: tuple
|
|
base_candidates: tuple
|
|
|
|
|
|
def load(root):
|
|
data = json.loads((root / "limits.json").read_text(encoding="utf-8"))
|
|
return Policy(data["limits"], data["tab_width"], tuple(data["exclude"]), tuple(data["python_include"]),
|
|
tuple(data["base_candidates"]))
|
|
|
|
|
|
def language(policy, rel):
|
|
"""'rust', 'python' or None for a repository-relative posix path."""
|
|
if "target" in rel.split("/") or rel.startswith(policy.exclude):
|
|
return None
|
|
if rel.endswith(".rs"):
|
|
return "rust"
|
|
return "python" if rel.endswith(".py") and rel.startswith(policy.python_include) else None
|
|
|
|
|
|
def is_test_path(rel):
|
|
"""Files that only exist for tests, benches and examples; inline `#[cfg(test)]` modules are found by the scanner."""
|
|
parts = rel.split("/")
|
|
name = parts[-1]
|
|
test_name = name in ("tests.rs", "conftest.py") or name.endswith(("_tests.rs", "_test.py"))
|
|
return bool(TEST_DIRS & set(parts[:-1])) or test_name |