#!/usr/bin/env python3 # Job #622: the display-rule change to research/qc/questions.js. # served = the served file (LF) sha256(LF) e5890d25... # base = served + audit #238's pending revision sha256(LF) c357a9fb... # revised = base + this change # Two patches: the INCREMENTAL one (base -> revised), which is the audit's # `patch` because #238 is still pending, and the COMBINED one (served -> # revised), so the integrator can apply in either order. import difflib, hashlib, io, os CLONE = "../pub/research/qc/questions.js" def read_lf(p): return io.open(p, encoding="utf-8", newline="").read().replace("\r\n", "\n") revised = read_lf(CLONE) base = read_lf("pa/research/qc/questions.js") served = read_lf("served-questions.js") for name, text, want in (("served", served, "e5890d255bfeabc026d60c2c34db066752b1e2d1b57a5cb3d16607ed9ea786b6"), ("base", base, "c357a9fbf3dc256a0a20f5f070827b4bc8bb58e409d13168ca24520e787fd10e")): got = hashlib.sha256(text.encode()).hexdigest() print(f"{name:8} sha256(LF) {got} {'OK' if got == want else 'MISMATCH vs ' + want}") print("revised sha256(LF)", hashlib.sha256(revised.encode()).hexdigest()) io.open("questions.revised.js", "w", encoding="utf-8", newline="").write(revised) a = base.split("\n"); b = revised.split("\n") assert len(a) != len(b) or a != b, "the revision is empty" for name, lo, hi in (("questions-newest-rule.patch", base, revised), ("questions-full-served-to-revised.patch", served, revised)): d = difflib.unified_diff(lo.split("\n"), hi.split("\n"), fromfile="a/research/qc/questions.js", tofile="b/research/qc/questions.js", lineterm="", n=3) txt = "\n".join(d) + "\n" io.open(name, "w", encoding="utf-8", newline="").write(txt) plus = sum(1 for l in txt.split("\n") if l.startswith("+") and not l.startswith("+++")) minus = sum(1 for l in txt.split("\n") if l.startswith("-") and not l.startswith("---")) print(f"{name}: +{plus} -{minus} sha256 {hashlib.sha256(txt.encode()).hexdigest()[:16]}") # apply-check both against synthetic trees import subprocess, shutil, tempfile for patch, src, label in (("questions-newest-rule.patch", base, "base"), ("questions-full-served-to-revised.patch", served, "served")): d = tempfile.mkdtemp() os.makedirs(os.path.join(d, "research/qc")) io.open(os.path.join(d, "research/qc/questions.js"), "w", encoding="utf-8", newline="").write(src) r = subprocess.run(["patch", "--binary", "-p1", "-i", os.path.abspath(patch)], cwd=d, capture_output=True, text=True) got = read_lf(os.path.join(d, "research/qc/questions.js")) print(f"apply {patch} to {label}: exit={r.returncode} equals revised: {got == revised}") shutil.rmtree(d) print() print("changed lines in the incremental patch:") for l in io.open("questions-newest-rule.patch", encoding="utf-8").read().split("\n"): if l.startswith(("@@", "+", "-")) and not l.startswith(("+++", "---")): print(" " + l[:150])