Coverage for scripts/check_file_length.py: 100%
22 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-12 12:29 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-12 12:29 +0000
1"""Fail when a tracked, hand-written text file exceeds the project's line limit."""
3from __future__ import annotations
5import subprocess
6import sys
7from pathlib import Path
9MAX_LINES = 300
10# Binary or third-party verbatim files; nothing generated is tracked.
11EXEMPT_SUFFIXES = {".png", ".jpg", ".ico", ".whl"}
12# The owner's call (2026-11-09): the README is the landing page on GitHub and
13# PyPI, so it keeps every essential behind a table of contents rather than
14# sending readers through several pages. A higher ceiling, not none: an
15# unbounded exemption would let a pasted log through. Only the top-level README.
16LIMITS = {Path("README.md"): 600}
19def tracked_files() -> list[Path]:
20 # S603: the command is a constant, no untrusted input reaches the process.
21 command = ["git", "ls-files"]
22 output = subprocess.run(command, check=True, capture_output=True, text=True).stdout # noqa: S603
23 return [Path(line) for line in output.splitlines() if line]
26def main() -> int:
27 offenders = []
28 for path in tracked_files():
29 if path.suffix in EXEMPT_SUFFIXES or not path.is_file():
30 continue
31 limit = LIMITS.get(path, MAX_LINES)
32 count = len(path.read_text(encoding="utf-8", errors="replace").splitlines())
33 if count > limit:
34 offenders.append(f"{path}: {count} lines (limit {limit})")
35 print("\n".join(offenders) or "all tracked files are within their line limits")
36 return 1 if offenders else 0
39if __name__ == "__main__":
40 sys.exit(main())