Repository navigation
Expand file tree
/
Copy pathaudit_folder_docs.py
More file actions
244 lines (206 loc) · 7.42 KB
/
Copy pathaudit_folder_docs.py
File metadata and controls
244 lines (206 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
"""Audit COGANT-owned folder README/AGENTS coverage and link hygiene."""
from __future__ import annotations
import argparse
import re
import subprocess
import sys
from collections.abc import Iterable
from dataclasses import dataclass
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
TEXT_DOC_NAMES = {"README.md", "AGENTS.md"}
HARD_EXCLUDED_PARTS = {
# .aii is an editor/tool sidecar owned by the surrounding workspace, not
# part of the COGANT source or release tree.
".aii",
".git",
".mypy_cache",
".pytest_cache",
".ruff_cache",
".tox",
".venv",
"__pycache__",
"htmlcov",
"node_modules",
"site",
"target",
}
EXCLUDED_PREFIXES = {
"cogant/evaluation/eval_repos",
"cogant/out",
"cogant/output",
"output",
}
README_EXCEPTIONS = {
"cogant/docs": "MkDocs uses docs/index.md as the site root; docs/README.md is intentionally absent.",
}
DOCUMENTABLE_SUFFIXES = {
".bib",
".html",
".ipynb",
".json",
".md",
".mmd",
".py",
".rs",
".toml",
".txt",
".yaml",
".yml",
}
DOCUMENTABLE_FILENAMES = {
"Dockerfile",
"Makefile",
}
PLACEHOLDER_PATTERNS = (
"Describe what belongs",
"This directory is part of the COGANT codebase-to-GNN translation engine",
"Machine-oriented index for automation and editors",
"Run `uv run pytest tests/` from the repository root",
)
LINK_RE = re.compile(r"(?<!!)\[[^\]]+\]\(([^)]+)\)")
@dataclass(frozen=True)
class Finding:
path: Path
message: str
line: int | None = None
def render(self) -> str:
rel = self.path.relative_to(ROOT) if self.path.is_relative_to(ROOT) else self.path
if self.line is None:
return f"{rel}: {self.message}"
return f"{rel}:{self.line}: {self.message}"
def is_excluded(path: Path) -> bool:
try:
rel = path.relative_to(ROOT).as_posix()
except ValueError:
return False
if any(part in HARD_EXCLUDED_PARTS for part in path.relative_to(ROOT).parts):
return True
return any(rel == prefix or rel.startswith(f"{prefix}/") for prefix in EXCLUDED_PREFIXES)
def tracked_and_untracked_files() -> set[Path]:
tracked = subprocess.check_output(["git", "ls-files"], cwd=ROOT, text=True).splitlines()
untracked = subprocess.check_output(
["git", "ls-files", "--others", "--exclude-standard"], cwd=ROOT, text=True
).splitlines()
files: set[Path] = set()
for raw in [*tracked, *untracked]:
path = ROOT / raw
if path.is_file() and not is_excluded(path):
files.add(path)
return files
def files_under(paths: Iterable[Path]) -> set[Path]:
files: set[Path] = set()
for raw_path in paths:
path = raw_path if raw_path.is_absolute() else ROOT / raw_path
if not path.exists() or is_excluded(path):
continue
if path.is_file():
files.add(path)
continue
for child in path.rglob("*"):
if child.is_file() and not is_excluded(child):
files.add(child)
return files
def is_documentable_file(path: Path) -> bool:
if path.name in DOCUMENTABLE_FILENAMES:
return True
return path.suffix.lower() in DOCUMENTABLE_SUFFIXES
def documentable_dirs(files: set[Path]) -> set[Path]:
dirs: set[Path] = set()
for path in files:
if not is_documentable_file(path):
continue
parent = path.parent
while parent != ROOT.parent and parent != parent.parent:
if parent == ROOT or is_excluded(parent):
break
dirs.add(parent)
parent = parent.parent
return dirs
def audit_coverage(files: set[Path]) -> list[Finding]:
findings: list[Finding] = []
file_set = {path.relative_to(ROOT) for path in files if path.is_relative_to(ROOT)}
for folder in sorted(documentable_dirs(files), key=lambda p: p.relative_to(ROOT).as_posix()):
rel = folder.relative_to(ROOT).as_posix()
readme = Path(rel) / "README.md"
agents = Path(rel) / "AGENTS.md"
if readme not in file_set and rel not in README_EXCEPTIONS:
findings.append(Finding(folder, "missing README.md"))
if agents not in file_set:
findings.append(Finding(folder, "missing AGENTS.md"))
return findings
def iter_doc_files(files: set[Path]) -> Iterable[Path]:
for path in sorted(files):
if path.name in TEXT_DOC_NAMES and not is_excluded(path):
yield path
def audit_placeholders(files: set[Path]) -> list[Finding]:
findings: list[Finding] = []
for path in iter_doc_files(files):
text = path.read_text(encoding="utf-8", errors="ignore")
for line_number, line in enumerate(text.splitlines(), start=1):
for pattern in PLACEHOLDER_PATTERNS:
if pattern in line:
findings.append(Finding(path, f"placeholder boilerplate: {pattern!r}", line_number))
return findings
def is_external_link(target: str) -> bool:
lowered = target.lower()
return (
target.startswith("#")
or lowered.startswith("http://")
or lowered.startswith("https://")
or lowered.startswith("mailto:")
)
def audit_links(files: set[Path]) -> list[Finding]:
findings: list[Finding] = []
for path in iter_doc_files(files):
text = path.read_text(encoding="utf-8", errors="ignore")
for line_number, line in enumerate(text.splitlines(), start=1):
for match in LINK_RE.finditer(line):
target = match.group(1).strip()
if is_external_link(target):
continue
target = target.split("#", 1)[0]
if not target:
continue
resolved = (path.parent / target).resolve()
if not resolved.exists():
findings.append(Finding(path, f"broken relative link: {match.group(1)!r}", line_number))
return findings
def audit_documented_exceptions(files: set[Path]) -> list[Finding]:
findings: list[Finding] = []
file_set = {path.relative_to(ROOT) for path in files if path.is_relative_to(ROOT)}
for rel, reason in README_EXCEPTIONS.items():
readme = Path(rel) / "README.md"
if readme in file_set:
findings.append(Finding(ROOT / readme, f"README.md should remain absent: {reason}"))
return findings
def audit(files: set[Path]) -> list[Finding]:
return [
*audit_coverage(files),
*audit_documented_exceptions(files),
*audit_placeholders(files),
*audit_links(files),
]
def parse_args(argv: list[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"paths",
nargs="*",
type=Path,
help="Optional files or directories to scan. Defaults to git-tracked and untracked COGANT-owned files.",
)
return parser.parse_args(argv)
def main(argv: list[str] | None = None) -> int:
args = parse_args(sys.argv[1:] if argv is None else argv)
files = files_under(args.paths) if args.paths else tracked_and_untracked_files()
findings = audit(files)
if findings:
print("Folder documentation audit failed:", file=sys.stderr)
for finding in findings:
print(f"- {finding.render()}", file=sys.stderr)
return 1
print("Folder documentation audit passed.")
return 0
if __name__ == "__main__":
raise SystemExit(main())