#!/usr/bin/env python3 """Prove the repair is exactly one substring: structural diff of the served vs corrected record. python diff-served-vs-fixed.py Walks both parsed records and reports every path whose value differs, so "one substring changed and nothing else" is a computed statement. Also lists every occurrence of the author's name in both bytes, because the name can appear as a *handle* (legitimate) and a name-only count cannot tell the two apart. """ import json import re import sys # A Windows console writes cp1252 by default, so a single non-ASCII separator (an ellipsis) turns # this log into bytes the file store refuses: it takes text only, UTF-8, and the first upload of this # log failed with "invalid start byte" at the 0x85 those separators became. Pin the stream instead. sys.stdout.reconfigure(encoding="utf-8", errors="replace") served, fixed = sys.argv[1], sys.argv[2] a = json.load(open(served, encoding="utf-8")) b = json.load(open(fixed, encoding="utf-8")) diffs = [] def walk(x, y, path): if type(x) is not type(y): diffs.append((path, repr(x)[:80], repr(y)[:80])) return if isinstance(x, dict): if set(x) != set(y): diffs.append((path + " (keys)", sorted(x), sorted(y))) return for k in x: walk(x[k], y[k], "%s.%s" % (path, k)) elif isinstance(x, list): if len(x) != len(y): diffs.append((path + " (length)", len(x), len(y))) return for i, (u, v) in enumerate(zip(x, y)): walk(u, v, "%s[%d]" % (path, i)) elif x != y: diffs.append((path, x, y)) walk(a, b, "$") print("leaf differences: %d" % len(diffs)) for path, x, y in diffs: print("\n path: %s" % path) print(" served : %r" % (x,)) print(" corrected: %r" % (y,)) if isinstance(x, str) and isinstance(y, str): print(" removed : %r (%d chars)" % (x.replace(y, ""), len(x) - len(y))) print("\nthe name in the bytes (a handle is not a path):") for label, p in (("served", served), ("corrected", fixed)): text = open(p, encoding="utf-8").read() for m in re.finditer(r"victor", text): lo = max(0, m.start() - 60) print(" %-9s ...%s..." % (label, text[lo:m.end() + 40].replace("\n", " ")))