#!/usr/bin/env python3 """Verify the shared chart module is single-sourced and byte-identical. Checks, in order, and exits non-zero naming every failure: 1. canonical ops-docs/scripts/shared/coop_chart.js == member-dashboard/app/coop_chart.js (byte-identical, sha256) == admin-panel/app/coop_chart.js (byte-identical, sha256) 2. each repo's tests/test_chart_sync.py pins the CANONICAL revision (so CI — which has no ops-docs checkout — still goes red when a repo copy is hand-edited). 3. each repo's app/main.py splices the module into PAGE_HTML (loader + placeholder replace present). Run from anywhere: python3 ops-docs/scripts/check-chart-sync.py This is the pre-deploy gate for either thin app; sync-chart-module.sh runs it at the end of every sync. """ import hashlib import sys from pathlib import Path HERE = Path(__file__).resolve().parent # ops-docs/scripts REPOS = HERE.parent.parent # ~/repos CANONICAL = HERE / "shared" / "coop_chart.js" SPLICE = 'PAGE_HTML = PAGE_HTML.replace("/*@@CHART_MODULE@@*/", CHART_MODULE_JS)' LOADER = 'CHART_MODULE_JS = (Path(__file__).resolve().parent / "coop_chart.js").read_text()' APPS = { "member-dashboard": ("app/coop_chart.js", "tests/test_chart_sync.py", "app/main.py"), "admin-panel": ("app/coop_chart.js", "tests/test_chart_sync.py", "app/main.py"), } def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() failures: list[str] = [] if not CANONICAL.exists(): print(f"FAILED: canonical module missing: {CANONICAL}") sys.exit(1) canonical_sha = sha256(CANONICAL) print(f"canonical sha256: {canonical_sha}") for repo, (copy_rel, test_rel, main_rel) in APPS.items(): copy = REPOS / repo / copy_rel test = REPOS / repo / test_rel main = REPOS / repo / main_rel if not copy.exists(): failures.append(f"{repo}: {copy_rel} missing") else: got = sha256(copy) if got == canonical_sha: print(f"ok - {repo}: {copy_rel} byte-identical to canonical") else: failures.append( f"{repo}: {copy_rel} DRIFTED from the canonical module\n" f" canonical {canonical_sha}\n" f" actual {got}\n" f" fix: edit the canonical module (ops-docs: scripts/shared/coop_chart.js) " f"and run ops-docs/scripts/sync-chart-module.sh — never hand-edit the copy.") if not test.exists(): failures.append(f"{repo}: {test_rel} missing") else: tsrc = test.read_text() if canonical_sha in tsrc: print(f"ok - {repo}: {test_rel} pins the canonical revision") else: failures.append( f"{repo}: {test_rel} does not pin the CURRENT canonical revision " f"({canonical_sha[:16]}…) — run ops-docs/scripts/sync-chart-module.sh to re-pin.") if not main.exists(): failures.append(f"{repo}: {main_rel} missing") else: msrc = main.read_text() missing = [name for name, needle in (("loader", LOADER), ("splice", SPLICE)) if needle not in msrc] if missing: failures.append(f"{repo}: {main_rel} lacks the chart-module {', '.join(missing)}") else: print(f"ok - {repo}: {main_rel} loads and splices the module") if failures: print() for f in failures: print("FAILED: " + f) sys.exit(1) print("chart module sync: OK")