PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
audit_generic_expansion.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""Reject generic documentation-expansion debris anywhere in the repository Markdown corpus."""
3
4from __future__ import annotations
5
6import json
7import sys
8from pathlib import Path
9
10
11REPO_ROOT = Path(__file__).resolve().parents[2]
12CONTRACT_PATH = REPO_ROOT / "tests" / "tooling" / "generic_expansion_contract.json"
13
14
15def load_contract() -> dict:
16 """!
17 @brief Load the forbidden-signature contract.
18 @return Parsed contract mapping.
19 """
20 return json.loads(CONTRACT_PATH.read_text(encoding="utf-8"))
21
22
23def iter_markdown(contract: dict):
24 """!
25 @brief Yield every repository Markdown file inside the contract's scan scope.
26 @param[in] contract Parsed contract mapping.
27 @return Generator of Markdown paths.
28 """
29 excluded = set(contract["scan"]["exclude_dirs"])
30 for path in sorted(REPO_ROOT.rglob("*.md")):
31 if excluded.intersection(path.relative_to(REPO_ROOT).parts):
32 continue
33 yield path
34
35
36def normalize(text: str) -> str:
37 """!
38 @brief Collapse whitespace so formatting changes cannot hide an exact template copy.
39 @param[in] text Raw file text.
40 @return Whitespace-normalized text.
41 """
42 return " ".join(text.split())
43
44
45def scan(contract: dict) -> list[str]:
46 """!
47 @brief Collect every forbidden-signature hit in the Markdown corpus.
48 @param[in] contract Parsed contract mapping.
49 @return Human-readable violation lines.
50 """
51 signatures = [
52 ("marker", contract["forbidden_markers"]),
53 ("heading", contract["forbidden_headings"]),
54 ("fragment", contract["forbidden_fragments"]),
55 ]
56 violations: list[str] = []
57 for path in iter_markdown(contract):
58 text = path.read_text(encoding="utf-8", errors="replace")
59 collapsed = normalize(text)
60 relative = path.relative_to(REPO_ROOT)
61 for kind, patterns in signatures:
62 for pattern in patterns:
63 if pattern in text or normalize(pattern) in collapsed:
64 violations.append(f"{relative}: forbidden {kind}: {pattern[:72]}")
65 return violations
66
67
68def main() -> int:
69 """!
70 @brief Fail when generic expansion debris is present in the Markdown corpus.
71 @return Process status code.
72 """
73 contract = load_contract()
74 violations = scan(contract)
75 if violations:
76 print("Generic documentation-expansion debris detected:", file=sys.stderr)
77 for violation in violations:
78 print(f" {violation}", file=sys.stderr)
79 print(
80 "\nThese signatures mark uniform agent-generated filler. Replace them with "
81 "guidance specific to this page's subject, or remove the section.",
82 file=sys.stderr,
83 )
84 return 1
85 print(f"Generic-expansion audit passed: {sum(1 for _ in iter_markdown(contract))} Markdown files clean.")
86 return 0
87
88
89if __name__ == "__main__":
90 raise SystemExit(main())
dict load_contract()
Load the forbidden-signature contract.
int main()
Fail when generic expansion debris is present in the Markdown corpus.
str normalize(str text)
Collapse whitespace so formatting changes cannot hide an exact template copy.
iter_markdown(dict contract)
Yield every repository Markdown file inside the contract's scan scope.
list[str] scan(dict contract)
Collect every forbidden-signature hit in the Markdown corpus.