#!/usr/bin/env python3 """Repair return #1564's `return1555.json`: the one hard-coded home directory -> repository-relative. python fix-return1555.py The edit is a BYTE substitution, not a re-serialisation: a JSON round-trip would rewrite the whole record (key order, spacing, escapes) and the point of this file is that it is the server's bytes for `GET /return/1555`. One substring changes and nothing else, which the script proves rather than asserts: it prints the unified diff, re-parses both files and compares their parsed structures, and reports the byte counts before and after. The replacement is what protocol/files.md asks for -- "paths relative to the repository, never a home directory": `/Users/victor/workspace/twin-prime-conjecture/.solveathome/private/...` becomes `.solveathome/private/...`, the same referent expressed against the checkout root. """ import difflib import hashlib import json import re import sys ABSOLUTE = "/Users/victor/workspace/twin-prime-conjecture/" RELATIVE = "" src, dst = sys.argv[1], sys.argv[2] raw = open(src, "rb").read() before = raw.decode("utf-8") hits = before.count(ABSOLUTE) assert hits == 1, "expected exactly 1 occurrence of the absolute prefix, found %d" % hits after = before.replace(ABSOLUTE, RELATIVE) open(dst, "wb").write(after.encode("utf-8")) b = open(dst, "rb").read() print("in : %s %d bytes sha256 %s" % (src, len(raw), hashlib.sha256(raw).hexdigest())) print("out: %s %d bytes sha256 %s" % (dst, len(b), hashlib.sha256(b).hexdigest())) print("bytes removed: %d\n" % (len(raw) - len(b))) print("unified diff (whole-file; only one line can differ):") for line in difflib.unified_diff(before.splitlines(), after.splitlines(), fromfile="served return1555.json", tofile="corrected return1555.json", lineterm="", n=1): print(" " + line) # 1. both parse, and exactly ONE leaf differs -- the note that carried the absolute path a_obj = json.loads(before) b_obj = json.loads(after) print("\nparses as JSON: served=%s corrected=%s" % (isinstance(a_obj, dict), isinstance(b_obj, dict))) leaves = [] def walk(x, y, path): if isinstance(x, dict) and isinstance(y, dict) and set(x) == set(y): for k in x: walk(x[k], y[k], "%s.%s" % (path, k)) elif isinstance(x, list) and isinstance(y, list) and len(x) == len(y): for i, (u, v) in enumerate(zip(x, y)): walk(u, v, "%s[%d]" % (path, i)) elif x != y: leaves.append((path, x, y)) walk(a_obj, b_obj, "$") print("differing leaves: %d" % len(leaves)) for path, x, y in leaves: print(" %s" % path) print(" served : %r" % (x,)) print(" corrected: %r" % (y,)) print(" served minus the absolute prefix == corrected: %s" % (isinstance(x, str) and isinstance(y, str) and x.replace(ABSOLUTE, "") == y)) # 2. no absolute home path of any shape left, and nothing else machine-specific introduced import re shapes = { "unix home": r"/Users/[A-Za-z0-9._-]+|/home/[A-Za-z0-9._-]+", "windows home": r"[A-Za-z]:\\\\?Users\\\\?[A-Za-z0-9._-]+", "the original user": r"victor", } for label, pat in shapes.items(): n_src = len(re.findall(pat, before)) n_dst = len(re.findall(pat, after)) print("%-18s occurrences: served %d -> corrected %d" % (label, n_src, n_dst)) print("\nremaining '/Users/' in the corrected file: %d" % after.count("/Users/"))