The queue had drifted from the patched tree it describes: 0003 predated rounds 12-19 of bridge work, 0005 no longer applied on top of it, and the Cargo.toml note and the case-duplicate readme.md were in no patch at all. Vendor the pristine crates.io crate (sha256 pinned in VERSION) and add scripts/wx_rs_patches.py: 'check' rebuilds the tree from upstream + patches and requires an exact match, 'capture' records an edit as the next patch. 0002/0003 are regenerated to the current bridge files (0005's GTK tooltip fix is folded into 0003); 0005 now carries the packaging leftovers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
5.4 KiB
Python
152 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: MIT
|
|
"""Keep thirdparty/wx-rs/patches/ an exact recipe for the patched wx-rs tree.
|
|
|
|
The patched crate (thirdparty/wx-rs/wx-rs-0.2.0-patched) is what cargo builds;
|
|
the numbered patches are how a reviewer or an upstream bump reproduces it from
|
|
the pristine crates.io tarball (thirdparty/wx-rs/upstream/, digest pinned in
|
|
thirdparty/wx-rs/VERSION). The two drift apart whenever someone edits the tree
|
|
and forgets the patch, so:
|
|
|
|
wx_rs_patches.py check
|
|
Unpack the upstream crate, apply every patch in order (no fuzz), and
|
|
require the result to equal the patched tree byte for byte. Offline; CI
|
|
runs it on every bridge change.
|
|
|
|
wx_rs_patches.py capture NNNN-short-name "Subject line" [PATH ...]
|
|
Write the difference between (upstream + current queue) and the patched
|
|
tree as the next patch, optionally limited to PATHs, then re-check.
|
|
Workflow: edit the patched tree, build/test, capture, commit both.
|
|
"""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tarfile
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
WXRS = ROOT / "thirdparty/wx-rs"
|
|
PATCHED = WXRS / "wx-rs-0.2.0-patched"
|
|
PATCHES = WXRS / "patches"
|
|
CRATE = WXRS / "upstream/wx-rs-0.2.0.crate"
|
|
# Build products and the wx source download cache are never part of the recipe.
|
|
IGNORED = {"target", "dist"}
|
|
|
|
|
|
def pinned_digest() -> str:
|
|
for line in (WXRS / "VERSION").read_text().splitlines():
|
|
if line.startswith("sha256:"):
|
|
return line.split()[1]
|
|
sys.exit("thirdparty/wx-rs/VERSION has no sha256 line")
|
|
|
|
|
|
def unpack_upstream(dest: Path) -> Path:
|
|
data = CRATE.read_bytes()
|
|
if hashlib.sha256(data).hexdigest() != pinned_digest():
|
|
sys.exit(f"{CRATE.relative_to(ROOT)} does not match the pinned sha256")
|
|
with tarfile.open(CRATE) as tar:
|
|
tar.extractall(dest, filter="data")
|
|
tree = dest / "wx-rs-0.2.0"
|
|
if not tree.is_dir():
|
|
sys.exit("upstream crate did not unpack to wx-rs-0.2.0/")
|
|
return tree
|
|
|
|
|
|
def patch_files() -> list:
|
|
return sorted(PATCHES.glob("[0-9][0-9][0-9][0-9]-*.patch"))
|
|
|
|
|
|
def apply_queue(tree: Path) -> None:
|
|
for patch in patch_files():
|
|
result = subprocess.run(
|
|
["patch", "-p1", "-s", "-F0", "--no-backup-if-mismatch", "-i", str(patch)],
|
|
cwd=tree, capture_output=True, text=True)
|
|
if result.returncode != 0:
|
|
sys.exit(f"{patch.name} does not apply cleanly:\n{result.stdout}{result.stderr}")
|
|
|
|
|
|
def copy_patched(dest: Path) -> Path:
|
|
tree = dest / "b"
|
|
shutil.copytree(PATCHED, tree, ignore=lambda d, names: [
|
|
n for n in names if Path(d) == PATCHED and n in IGNORED])
|
|
return tree
|
|
|
|
|
|
def unified_diff(work: Path, paths: list) -> str:
|
|
"""diff -ruN a b, run from `work` so headers read a/<path> and b/<path>."""
|
|
targets = [(f"a/{p}", f"b/{p}") for p in paths] if paths else [("a", "b")]
|
|
out = []
|
|
for left, right in targets:
|
|
result = subprocess.run(["diff", "-ruN", left, right], cwd=work,
|
|
capture_output=True, text=True)
|
|
if result.returncode > 1:
|
|
sys.exit(result.stderr)
|
|
out.append(result.stdout)
|
|
# Drop the mtimes diff appends to ---/+++ headers so a regenerated patch
|
|
# only changes when its content does.
|
|
return "".join(
|
|
line.split("\t", 1)[0] + "\n" if line.startswith(("--- ", "+++ ")) else line
|
|
for line in "".join(out).splitlines(keepends=True))
|
|
|
|
|
|
def prepare(work: Path) -> None:
|
|
unpack_upstream(work).rename(work / "a")
|
|
apply_queue(work / "a")
|
|
copy_patched(work)
|
|
|
|
|
|
def check() -> int:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
work = Path(tmp)
|
|
prepare(work)
|
|
drift = unified_diff(work, [])
|
|
if drift:
|
|
print("wx-rs patch queue does not reproduce the patched tree;", file=sys.stderr)
|
|
print("capture the change with scripts/wx_rs_patches.py capture:", file=sys.stderr)
|
|
print(drift[:4000], file=sys.stderr)
|
|
return 1
|
|
print(f"wx-rs patch queue OK ({len(patch_files())} patches reproduce the tree)")
|
|
return 0
|
|
|
|
|
|
def capture(name: str, subject: str, paths: list) -> int:
|
|
existing = patch_files()
|
|
number = int(existing[-1].name[:4]) + 1 if existing else 1
|
|
if not name[:4].isdigit():
|
|
name = f"{number:04d}-{name}"
|
|
target = PATCHES / (name if name.endswith(".patch") else name + ".patch")
|
|
if target.exists():
|
|
sys.exit(f"{target.name} already exists")
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
work = Path(tmp)
|
|
prepare(work)
|
|
body = unified_diff(work, paths)
|
|
if not body:
|
|
sys.exit("nothing to capture: the queue already reproduces the tree")
|
|
target.write_text(f"Subject: [PATCH {target.name[:4]}] {subject}\n\n{body}")
|
|
print(f"wrote {target.relative_to(ROOT)}")
|
|
return check()
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__,
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
sub = parser.add_subparsers(dest="cmd", required=True)
|
|
sub.add_parser("check")
|
|
cap = sub.add_parser("capture")
|
|
cap.add_argument("name")
|
|
cap.add_argument("subject")
|
|
cap.add_argument("paths", nargs="*")
|
|
args = parser.parse_args()
|
|
if args.cmd == "check":
|
|
return check()
|
|
return capture(args.name, args.subject, args.paths)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|