| 1 | """Fail when strict source paths gain prose comments.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import argparse |
| 6 | from pathlib import Path |
| 7 | |
| 8 | |
| 9 | ROOT = Path(__file__).resolve().parents[1] |
| 10 | STRICT_COMMENT_FREE_DIRS = ( |
| 11 | ROOT / "oversight-rust" / "oversight-registry" / "src", |
| 12 | ) |
| 13 | RUST_EXTENSIONS = {".rs"} |
| 14 | COMMENT_PREFIXES = ("//", "///", "//!") |
| 15 | |
| 16 | |
| 17 | def iter_source_files() -> list[Path]: |
| 18 | files: list[Path] = [] |
| 19 | for directory in STRICT_COMMENT_FREE_DIRS: |
| 20 | if directory.exists(): |
| 21 | files.extend( |
| 22 | path |
| 23 | for path in directory.rglob("*") |
| 24 | if path.is_file() and path.suffix in RUST_EXTENSIONS |
| 25 | ) |
| 26 | return sorted(files) |
| 27 | |
| 28 | |
| 29 | def comment_violations(path: Path) -> list[tuple[int, str]]: |
| 30 | violations: list[tuple[int, str]] = [] |
| 31 | for lineno, line in enumerate(path.read_text(encoding="utf-8").splitlines(), start=1): |
| 32 | stripped = line.lstrip() |
| 33 | if stripped.startswith(COMMENT_PREFIXES): |
| 34 | violations.append((lineno, line.rstrip())) |
| 35 | return violations |
| 36 | |
| 37 | |
| 38 | def main() -> int: |
| 39 | parser = argparse.ArgumentParser(description=__doc__) |
| 40 | parser.add_argument( |
| 41 | "--list-paths", |
| 42 | action="store_true", |
| 43 | help="Print strict paths before checking.", |
| 44 | ) |
| 45 | args = parser.parse_args() |
| 46 | |
| 47 | if args.list_paths: |
| 48 | for directory in STRICT_COMMENT_FREE_DIRS: |
| 49 | print(directory.relative_to(ROOT)) |
| 50 | |
| 51 | failures: list[str] = [] |
| 52 | for path in iter_source_files(): |
| 53 | for lineno, line in comment_violations(path): |
| 54 | failures.append(f"{path.relative_to(ROOT)}:{lineno}: {line}") |
| 55 | |
| 56 | if failures: |
| 57 | print("Comment-style violations:") |
| 58 | print("\n".join(failures)) |
| 59 | return 1 |
| 60 | |
| 61 | print("source comment style ok") |
| 62 | return 0 |
| 63 | |
| 64 | |
| 65 | if __name__ == "__main__": |
| 66 | raise SystemExit(main()) |