Files
troupe-standup/wizard-work/ledger/verify_tracked_directory_census.py
T

75 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Independent verifier for the tracked-directory census.
Re-derives the ranked table from the LIVE git tree (`git ls-files` at HEAD) with
a standalone loop -- it never imports
`extract_tracked_directory_census` -- and asserts the committed
`2026-11-03-tracked-directory-census.csv` byte-matches.
Exit code 0 = artifact matches re-derivation; 1 = mismatch; 2 = misuse.
"""
import os
import subprocess
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
REPO = os.path.dirname(os.path.dirname(HERE))
ART = os.path.join(REPO, "wizard-work", "ledger",
"2026-11-03-tracked-directory-census.csv")
def directory_of(path):
d = os.path.dirname(path)
return d if d else "(root)"
def re_derivation():
counts = {}
for rel in subprocess.run(
["git", "ls-files"], cwd=REPO, check=True,
capture_output=True, text=True,
).stdout.splitlines():
rel = rel.rstrip("\r")
if not rel:
continue
counts[directory_of(rel)] = counts.get(directory_of(rel), 0) + 1
total = sum(counts.values())
ranked = sorted(counts.items(), key=lambda kv: (-kv[1], kv[0]))
rows = []
for i, (directory, count) in enumerate(ranked, 1):
rows.append([str(i), directory, str(count), str(total)])
return ["rank", "directory", "tracked_count", "total"], rows
def read_artifact():
with open(ART, encoding="utf-8") as f:
lines = [ln.rstrip("\n") for ln in f]
header = lines[0].split(",")
rows = [ln.split(",") for ln in lines[1:]]
return header, rows
def main():
header, rows = read_artifact()
expected_header, expected_rows = re_derivation()
if header != expected_header:
print("FAIL: header artifact=%r expected=%r"
% (header, expected_header))
return 1
if len(rows) != len(expected_rows):
print("FAIL: row count artifact=%d expected=%d"
% (len(rows), len(expected_rows)))
return 1
for i, (exp, act) in enumerate(zip(expected_rows, rows), 1):
if act != exp:
print("FAIL: row %d artifact=%r expected=%r"
% (i, act, exp))
return 1
print("OK: %d rows byte-match re-derivation from git ls-files at HEAD; "
"header=%r" % (len(rows), header))
return 0
if __name__ == "__main__":
sys.exit(main())