#!/usr/bin/env python3 """Compares Orbit's install snapshots (before and after an install). Exit 0 = nothing the household owns changed. Checked: every file present before is still there; decisions, history, tickets, choices, learning, yours, profiles, Taste events and Continue watching are byte-identical; every booking from before is still booked (new ones may be added as Orbit plans ahead); every planned programme still in the window is unchanged. Counts only, no titles. Usage: compare_snapshots.py """ import glob, hashlib, json, os, sys UNCHANGED = ("decisions", "history", "tickets", "choices", "learning", "yours", "orbit_profiles", "taste/", "continue") def files(root): return {os.path.relpath(p, root): p for p in glob.glob(root + "/**/*", recursive=True) if os.path.isfile(p)} def digest(path): return hashlib.sha256(open(path, "rb").read()).hexdigest() def booked_ids(path): o = json.load(open(path)) if isinstance(o, dict): out = [] for v in o.values(): if isinstance(v, list): out += [json.dumps(x, sort_keys=True) for x in v] return out return [json.dumps(x, sort_keys=True) for x in o] if isinstance(o, list) else [] def compare(before, after): b, a = files(before), files(after) problems, lines = [], [] missing = [k for k in b if k not in a and not k.endswith((".bak", ".writing", ".tmp")) and "continue-" not in k] if missing: problems.append(f"missing after install: {len(missing)}") for pat in UNCHANGED: ks = [k for k in b if pat in k and not k.endswith((".bak", ".writing", ".tmp", ".before-catchup-only")) and k in a] changed = [k for k in ks if digest(b[k]) != digest(a[k])] lines.append(f"{pat.rstrip('/')}: {len(ks)} files, changed {len(changed)}") if changed: problems.append(f"{pat.rstrip('/')} changed: {len(changed)} files") for k in [k for k in b if os.path.basename(k).startswith("booked-") and k.endswith(".json") and k in a]: before_ids, after_ids = booked_ids(b[k]), set(booked_ids(a[k])) lost = [x for x in before_ids if x not in after_ids] lines.append(f"{os.path.basename(k)}: {len(before_ids)} bookings before, {len(after_ids)} after, {len(lost)} lost") if lost: problems.append(f"bookings lost: {len(lost)}") total = changed = 0 for bp in glob.glob(before + "/no_backup/schedule/schedule-*.json"): ap = bp.replace(before, after, 1) try: eb, ea = json.load(open(bp)).get("entries", []), json.load(open(ap)).get("entries", []) except Exception: continue if not eb or not ea: continue key = lambda e: e.get("a", e.get("startMs")) lo, after_by = min(key(e) for e in ea), {key(e): e for e in ea} for e in eb: if key(e) >= lo: total += 1 changed += after_by.get(key(e)) != e lines.append(f"planned programmes compared {total}, changed {changed}") if changed: problems.append(f"planned programmes changed: {changed}") return lines, problems if __name__ == "__main__": lines, problems = compare(sys.argv[1], sys.argv[2]) print("\n".join(lines)) print("UNCHANGED" if not problems else "PROBLEMS: " + "; ".join(problems)) sys.exit(0 if not problems else 1)