#!/usr/bin/env python3 """Apply only exact version/hash-matched code overlays; dry-run unless execute.""" import argparse,hashlib,json,subprocess from pathlib import Path ROOT=Path(__file__).absolute().parents[1] def sha(p):return hashlib.sha256(Path(p).read_bytes()).hexdigest() def main(): p=argparse.ArgumentParser();p.add_argument('--code-checkout',type=Path,required=True);p.add_argument('--execute',action='store_true');a=p.parse_args();m=json.loads((ROOT/'SOURCE_OVERLAY_MANIFEST.json').read_text()) revision=subprocess.check_output(['git','-C',str(a.code_checkout),'rev-parse','HEAD'],text=True).strip() if revision!=m['revision']:raise ValueError('Checkout revision differs from locked source') for r in m['files']: target=a.code_checkout/r['path'];source=ROOT/r['patch_file'] if target.exists() and sha(target)==r['patched_sha256']:continue if target.exists() and sha(target)!=r['expected_git_source_sha256']:raise ValueError('Custom source modifications found; refusing to overwrite') if not target.exists() and r['expected_git_source_sha256']:raise ValueError('Expected source file is missing') print('PLAN',r['path']) if a.execute:target.parent.mkdir(parents=True,exist_ok=True);target.write_bytes(source.read_bytes()) if __name__=='__main__':main()