#!/usr/bin/env python3 """null verification for the Wizard Troupe standup. Exit code is the verdict: 0 = everything holds, 1 = something broke. Run: python3 /root/troupe-standup/wizard-work/null/check.py """ import os import re import subprocess import sys MYDIR = os.path.dirname(os.path.abspath(__file__)) REPO = os.path.dirname(os.path.dirname(MYDIR)) PASSED, FAILED = [], [] def check(name, ok, detail=""): (PASSED if ok else FAILED).append(name) print(("PASS " if ok else "FAIL ") + name + (" -> " + detail if detail else "")) def main(): # --- 1. artifact presence (all three files) --- for fn in ("wizard.log.md", "wizard.py", "check.py"): check("artifact present: " + fn, os.path.isfile(os.path.join(MYDIR, fn))) # --- 2. wizard.py compiles, exposes the expected API --- src = open(os.path.join(MYDIR, "wizard.py")).read() check("wizard.py compiles", _compiles(src)) ns = {} if _compiles(src): exec(compile(src, "wizard.py", "exec"), ns) check("API: craft exists", callable(ns.get("craft"))) check("API: VERDICT/CHECK markers exist", "VERDICT:" in src and "CHECK:" in src) # --- 3. craft() unit test: deterministic, non-empty, format correct --- try: verdict, chk, summary = ns["craft"]( "fix the build", "staging", "Codeforge", "d7a1f", "alex" ) ok = bool(verdict) and chk.startswith("CHECK: ") and isinstance(summary, str) and summary check("craft() returns VERDICT/CHECK/summary", ok, repr((verdict, chk[:40] + "...", summary[:24]))) except Exception as e: # noqa: BLE001 check("craft() returns VERDICT/CHECK/summary", False, "%s: %s" % (type(e).__name__, e)) # --- 4. report parses: exactly one VERDICT: and one CHECK: --- log = open(os.path.join(MYDIR, "wizard.log.md")).read() nv = len(re.findall(r"(?m)^VERDICT:", log)) nc = len(re.findall(r"(?m)^CHECK:", log)) check("wizard.log.md has exactly one VERDICT:", nv == 1, "found %d" % nv) check("wizard.log.md has exactly one CHECK:", nc == 1, "found %d" % nc) # --- 5. self-verification: run check.py and require exit 0 --- r = subprocess.run([sys.executable, os.path.join(MYDIR, "check.py")], capture_output=True, text=True) check("check.py self-run exits 0", r.returncode == 0, "exit=%d" % r.returncode) # --- 6. git hygiene: commit exists and pushed HEAD is a descendant of it --- ok, detail = _git_ok() check("commit pushed to origin/main", ok, detail) print("\n%d passed, %d failed" % (len(PASSED), len(FAILED))) if FAILED: print("FAILED: " + ", ".join(FAILED)) return 1 print("ALL CHECKS GREEN") return 0 def _compiles(src): try: compile(src, "wizard.py", "exec") return True except SyntaxError: return False def _git_ok(): try: logs = subprocess.run( ["git", "-C", REPO, "log", "-1", "--format=%H%n%an%n%s"], capture_output=True, text=True).stdout.splitlines() if len(logs) < 3: return False, "no commit found" commit, author, subject = logs[0], logs[1], logs[2] if author != "null": return False, "commit author is %r, expected null" % author if not subject.startswith("null:"): return False, "subject %r lacks 'null:' prefix" % subject files = subprocess.run( ["git", "-C", REPO, "show", "--name-only", "--format=", commit], capture_output=True, text=True).stdout.split() need = {"wizard-work/null/wizard.log.md", "wizard-work/null/wizard.py", "wizard-work/null/check.py"} if not need.issubset(set(files)): return False, "commit missing files: %s" % sorted(need - set(files)) local = subprocess.run(["git", "-C", REPO, "rev-parse", "HEAD"], capture_output=True, text=True).stdout.strip() origin = subprocess.run(["git", "-C", REPO, "rev-parse", "origin/main"], capture_output=True, text=True).stdout.strip() if local != commit: return False, "HEAD %s != commit %s" % (local[:8], commit[:8]) if origin != commit: return False, "origin/main %s != commit %s" % (origin[:8], commit[:8]) return True, "commit %s by null pushed to origin/main" % commit[:8] except Exception as e: # noqa: BLE001 return False, "%s: %s" % (type(e).__name__, e) if __name__ == "__main__": sys.exit(main())