Files
troupe-standup/wizard-work/ledger/CHECK-file-tree-census
T

73 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""CHECK-file-tree-census — re-derive the ledger file-tree census from git and
verify the committed artifact against it.
Exit code 0 = the artifact matches the git source of truth (verdict PASS).
Non-zero = mismatch (verdict FAIL).
Run: python3 CHECK-file-tree-census (from wizard-work/ledger/)
"""
import json
import subprocess
import sys
REPO = "/root/troupe-standup"
OUT = "/root/troupe-standup/wizard-work/ledger"
DAY = "2026-10-30"
JSON_PATH = "%s/%s-file-tree-census.json" % (OUT, DAY)
CSV_PATH = "%s/%s-file-tree-census.csv" % (OUT, DAY)
def git(args):
return subprocess.check_output(["git"] + args, cwd=REPO, text=True)
def main():
# Independently re-derive from git: the committed tree at HEAD.
ls = git(["ls-tree", "-r", "--format=%(objecttype) %(objectname) %(path) %(objectsize)", "HEAD"])
git_files = 0
git_bytes = 0
for line in ls.strip().splitlines():
otype, _obj, path, size_s = line.split(" ", 3)
if otype == "blob" and path.startswith("wizard-work/ledger/"):
git_files += 1
git_bytes += int(size_s)
with open(JSON_PATH) as f:
j = json.load(f)
s = j["summary"]
json_files = s["total_files"]
json_bytes = s["total_bytes"]
with open(CSV_PATH) as f:
csv_lines = [l for l in f.read().splitlines() if l]
csv_rows = csv_lines[1:] # drop header
csv_files = len(csv_rows)
csv_bytes = sum(int(r.split(",")[1]) for r in csv_rows)
json_manifest = j.get("manifest", [])
json_csv_consistency = (len(json_manifest) == csv_files)
print("git_files=%d git_bytes=%d" % (git_files, git_bytes))
print("json_files=%d json_bytes=%d" % (json_files, json_bytes))
print("csv_files=%d csv_bytes=%d json_manifest_matches_csv=%s" % (csv_files, csv_bytes, json_csv_consistency))
ok = True
if (json_files, json_bytes) != (git_files, git_bytes):
ok = False
print("MISMATCH: json(%d,%d) != git(%d,%d)" % (json_files, json_bytes, git_files, git_bytes))
if (csv_files, csv_bytes) != (git_files, git_bytes):
ok = False
print("MISMATCH: csv(%d,%d) != git(%d,%d)" % (csv_files, csv_bytes, git_files, git_bytes))
if not json_csv_consistency:
ok = False
print("MISMATCH: json manifest rows != csv rows")
if s.get("head_sha") != git(["rev-parse", "HEAD"]).strip():
ok = False
print("MISMATCH: head_sha in artifact != git HEAD")
print("VERDICT: %s" % ("PASS" if ok else "FAIL"))
sys.exit(0 if ok else 1)
if __name__ == "__main__":
main()