"""Build an offline review copy from an exact server source snapshot.""" import argparse import hashlib import json from pathlib import Path import shutil import subprocess BUNDLE = Path(__file__).resolve().parent REPO = BUNDLE.parent.parent def sha(path): with path.open("rb") as file: return hashlib.file_digest(file, "sha256").hexdigest() def check(directory, expected): for name, digest in expected.items(): path = directory / name if not path.is_file() or sha(path) != digest: raise ValueError(f"SHA256 mismatch or missing file: {name}") def build(source, destination): manifest = json.loads((BUNDLE / "pins.json").read_text(encoding="utf-8")) check(source, manifest["source"]) check(REPO / "ptz_tracker_modular", manifest["modules"]) check(BUNDLE, {"server.patch": manifest["patch_sha256"]}) destination.mkdir() package = destination / "ptz_tracker_modular" package.mkdir() for name in manifest["source"]: shutil.copyfile(source / name, package / name) for name in manifest["modules"]: shutil.copyfile(REPO / "ptz_tracker_modular" / name, package / name) patch = str(BUNDLE / "server.patch") subprocess.run(["git", "-c", "core.autocrlf=false", "apply", "--check", patch], cwd=destination, check=True) subprocess.run(["git", "-c", "core.autocrlf=false", "apply", patch], cwd=destination, check=True) check(package, manifest["result"]) for path in (BUNDLE / "tests").glob("test_*.py"): shutil.copyfile(path, destination / path.name) print(f"Verified offline copy: {destination}") if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("source", type=Path, help="directory containing exact server source files") parser.add_argument("destination", type=Path, help="new local output directory") args = parser.parse_args() build(args.source.resolve(), args.destination.resolve())