"""Scoped sync behind scripts/sync.sh --files; refuse remote preimage drift.""" import base64 import datetime import hashlib import json import os from pathlib import Path import shlex import subprocess import sys REMOTE_APPLY = r''' import base64,fcntl,hashlib,json,os,sys from pathlib import Path p=json.load(sys.stdin);root=Path(p['root']);sync=root/'.sync';sync.mkdir(exist_ok=True) with (sync/'patch.lock').open('a') as lock: fcntl.flock(lock,fcntl.LOCK_EX) for x in p['files']: f=root/x['path'];actual=hashlib.md5(f.read_bytes()).hexdigest() if f.exists() else None if actual!=x['expected']:raise SystemExit('remote drift: '+x['path']) backup=sync/p['id'];backup.mkdir() (backup/'request.json').write_text(json.dumps(p)) result=[] for x in p['files']: f=root/x['path'];old=f.read_bytes() if f.exists() else None actual=hashlib.md5(old).hexdigest() if old is not None else None if actual!=x['expected']:raise SystemExit('concurrent remote change: '+x['path']) if old is not None: b=backup/x['path'];b.parent.mkdir(parents=True,exist_ok=True);b.write_bytes(old) raw=base64.b64decode(x['data']);f.parent.mkdir(parents=True,exist_ok=True) tmp=f.with_name(f.name+'.'+p['id']);tmp.write_bytes(raw) tmp.chmod(f.stat().st_mode & 0o777 if f.exists() else 0o644);os.replace(tmp,f) result.append({'path':x['path'],'md5':hashlib.md5(f.read_bytes()).hexdigest(),'sha256':hashlib.sha256(f.read_bytes()).hexdigest()}) (backup/'receipt.json').write_text(json.dumps(result,indent=2)) print(json.dumps({'backup':str(backup),'files':result},indent=2)) ''' def main(): root = Path(__file__).resolve().parents[1] names = list(dict.fromkeys(sys.argv[1:])) if not names: raise SystemExit('usage: scripts/sync.sh --files path ... (committed code only)') stamp = root / '.sync/last_pushed.md5' known = {s.split(None, 1)[1]: s.split(None, 1)[0] for s in stamp.read_text().splitlines()} entries = [] for name in names: p = Path(name) if p.is_absolute() or '..' in p.parts or p.as_posix() != name: raise SystemExit('invalid relative path: ' + name) if p.parts[0] not in ('harness', 'scripts', 'docs') and name not in ('README.md', 'CONTRACT.md'): raise SystemExit('not a scoped code/document path: ' + name) data = (root / name).read_bytes() committed = subprocess.check_output(['git', 'show', 'HEAD:' + name], cwd=root) if data != committed: raise SystemExit('commit selected files before sync: ' + name) entries.append({'path': name, 'expected': known.get('./' + name), 'data': base64.b64encode(data).decode()}) payload = {'root': os.environ.get('BENCH_REMOTE_DIR', '/mnt/data/ming01.wang/model-compile-hbdk/bench'), 'id': 'patch-' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S-%f'), 'files': entries} remote = os.environ.get('BENCH_REMOTE', 'ming01.wang@120.48.151.164') r = subprocess.run(['ssh', '-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10', remote, 'python3 -c ' + shlex.quote(REMOTE_APPLY)], input=json.dumps(payload), capture_output=True, text=True, timeout=120) if r.returncode: sys.stderr.write(r.stderr) raise SystemExit(r.returncode) receipt = json.loads(r.stdout) for x in receipt['files']: expected = hashlib.md5((root / x['path']).read_bytes()).hexdigest() if x['md5'] != expected: raise SystemExit('post-sync hash mismatch: ' + x['path']) known['./' + x['path']] = expected # Only selected entries advance; unrelated drift remains detectable. stamp.write_text(''.join(f'{h} {p}\n' for p, h in sorted(known.items()))) (root / '.sync' / (payload['id'] + '.json')).write_text(r.stdout) print(r.stdout) if __name__ == '__main__': main()