#!/usr/bin/env python3 """Print the text behind every pointer in a table file, so a person can read it. Read-only by construction: it opens files for reading and prints. It runs on DooPlex against the git clones only; it never touches a box. Usage: check-pointers.py [FILE ...] (default: ../TABLE.md) check-pointers.py --quiet [FILE] (only the verdict lines; rc 1 when a pointer is dead) A pointer is a backticked token `PREFIX/path[:line[-line]]` with one of the prefixes below. A pointer is DEAD when the file is missing or the line range lies beyond the end of the file. The script cannot judge whether the line says what the table claims — that is the reader's job. """ import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) AUDIT = os.path.dirname(HERE) REPO = os.path.abspath(os.path.join(AUDIT, "..", "..", "..")) # felhom.eu WORKSPACE = os.path.dirname(REPO) PREFIX = { "AG": os.path.join(WORKSPACE, "felhom-agent"), "CT": os.path.join(WORKSPACE, "felhom-controller", "controller"), "CAT": os.path.join(WORKSPACE, "app-catalog-felhom.eu"), "HUB": os.path.join(REPO, "hub", "internal"), "E": REPO, "AR": os.path.join(REPO, "documentation", "architecture"), "RB": os.path.join(REPO, "documentation", "runbooks"), "EV": os.path.join(AUDIT, "evidence"), } TOKEN = re.compile(r"`((?:%s)/[^`:\s]+)(?::(\d+)(?:-(\d+))?)?`" % "|".join(PREFIX)) def main() -> int: args = [a for a in sys.argv[1:] if a != "--quiet"] quiet = "--quiet" in sys.argv[1:] files = args or [os.path.join(AUDIT, "TABLE.md")] seen, dead, total = set(), 0, 0 for table in files: with open(table, encoding="utf-8") as fh: text = fh.read() for m in TOKEN.finditer(text): key = m.group(0) if key in seen: continue seen.add(key) total += 1 pre, rest = m.group(1).split("/", 1) path = os.path.join(PREFIX[pre], rest) first = int(m.group(2)) if m.group(2) else None last = int(m.group(3)) if m.group(3) else first if not os.path.exists(path): dead += 1 print(f"DEAD {key} (no such file: {path})") continue if first is None: if not quiet: print(f"OK {key} (file exists)") continue with open(path, encoding="utf-8", errors="replace") as fh: lines = fh.readlines() if last > len(lines): dead += 1 print(f"DEAD {key} (file has {len(lines)} lines)") continue if quiet: continue print(f"OK {key}") for n in range(first, min(last, first + 11) + 1): print(f" {n:>5} {lines[n - 1].rstrip()[:200]}") print(f"pointers: {total} dead: {dead}") return 1 if dead else 0 if __name__ == "__main__": sys.exit(main())