screen-locker/scripts/check_file_length.py
Krzysztof kuhy Rudnicki 22294ec541
Some checks are pending
pre-commit / pre-commit (push) Waiting to run
Tests / test (3.10) (push) Waiting to run
Tests / test (3.11) (push) Waiting to run
Tests / test (3.12) (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
Enforce the 250-line cap for real, and on Dart
The gate this whole refactor was aimed at had two holes:

- `MAX_LINES` was 400, while `refresh_refactor_todo.py` reported against 250.
  The hook and the TODO generator disagreed about what "over the cap" meant.
- `types_or` was [python, shell], so the hook had never inspected a single
  Dart file. Every file in the refactor — workout_screen.dart at 633 lines
  included — was exempt from the check it was being split to satisfy.

A violation also exited 1 while printing nothing, so a developer got a failed
commit with no indication of which file or by how much. It now names the file
and the overage on stderr.

Verified by planting a 260-line Dart file: the hook exits 1 and reports it.
Boundary checked at 250 (pass) / 251 (fail).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EveB813BQ8abAP68VSqZ5Y
2026-08-15 22:42:57 +02:00

49 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Pre-commit hook: fail if any file exceeds MAX_LINES lines."""
import logging
import sys
MAX_LINES = 250
_logger = logging.getLogger(__name__)
def main() -> int:
"""Return 1 if any file exceeds the line limit, else 0."""
# Without this the module relies on logging's last-resort handler, which
# drops anything below WARNING and prints no context — a hook that fails
# without saying which file is a silent failure.
logging.basicConfig(format="%(levelname)s: %(message)s", stream=sys.stderr)
failed = False
for filepath in sys.argv[1:]:
try:
with open(filepath, encoding="utf-8", errors="replace") as fh:
count = sum(1 for _ in fh)
except OSError as exc:
_logger.warning(
"Could not read %s to count its lines (%s) — failing the check "
"rather than assuming the file is within the %d-line limit",
filepath,
exc,
MAX_LINES,
)
failed = True
continue
if count > MAX_LINES:
# A bare non-zero exit tells nobody which file blew the cap, so the
# violation has to name itself and the size of the split it needs.
_logger.error(
"%s is %d lines — %d over the %d-line cap. Split it before committing.",
filepath,
count,
count - MAX_LINES,
MAX_LINES,
)
failed = True
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())