data-gen-bpu/scripts/sync_patch.py

214 lines
9.9 KiB
Python

"""Preview managed source changes; apply only an explicit, unchanged preview.
sync.sh local preview, no SSH
sync.sh --remote user@host --remote-dir /path --baseline /tmp/preview.json
sync.sh --remote user@host --remote-dir /path --baseline /tmp/preview.json --apply
--files selects managed paths through this same contract. Server-generated registry
and runtime data are never uploaded. Source deletions require a separate reviewed
operation: this command refuses them rather than deleting whole directories.
"""
from __future__ import annotations
import argparse
import base64
import hashlib
import json
import os
from pathlib import Path
import shlex
import subprocess
import uuid
try:
from scripts import release_files
except ModuleNotFoundError:
import release_files
snapshot = release_files.snapshot
PLAN_SCHEMA = 'bench-sync-preview.v1'
# Exactly the same stdlib-only source enumeration runs on both sides. No import
# from the target checkout, shell interpolation of filenames or secret/state reads.
REMOTE_PROGRAM = Path(release_files.__file__).read_text() + r'''
import base64, fcntl, json, re, sys
def destination(root, name):
path = Path(name)
if (path.is_absolute() or '..' in path.parts or path.as_posix() != name
or not allowed(path) or name in SERVER_GENERATED):
raise ValueError('not a managed source destination: ' + name)
result = root
for part in path.parts:
result = result / part
if result.is_symlink():
raise ValueError('symlink in destination: ' + name)
return result
def remote_main(p):
root = Path(p['root']).resolve(strict=True)
actual = snapshot(root, for_sync=True)
if p['operation'] == 'preview':
return actual
if p['operation'] != 'apply':
raise ValueError('unknown sync operation')
if actual != p['expected']:
raise ValueError('remote drift; preview again, no source written')
if not re.fullmatch('[a-f0-9]{32}', p['id']):
raise ValueError('invalid backup identity')
entries = []
names = set()
for x in p['files']:
f = destination(root, x['path'])
if x['path'] in names:
raise ValueError('duplicate source destination')
names.add(x['path'])
raw = base64.b64decode(x['data'], validate=True)
if hashlib.sha256(raw).hexdigest() != x['sha256'] or x['mode'] not in (0o644, 0o755):
raise ValueError('payload identity mismatch')
entries.append((x, f, raw))
if not entries:
return {'changed': [], 'snapshot': actual, 'backup': None}
sync = root / '.sync'
if sync.is_symlink() or (sync / 'patch.lock').is_symlink():
raise ValueError('symlink in sync metadata path')
sync.mkdir(exist_ok=True)
with (sync / 'patch.lock').open('a') as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
if snapshot(root, for_sync=True) != p['expected']:
raise ValueError('remote drift while acquiring sync lock')
backup = sync / p['id']
backup.mkdir()
(backup / 'request.json').write_text(json.dumps(p, indent=2))
changed = []
for x, f, raw in entries:
destination(root, x['path'])
expected = p['expected']['files'].get(x['path'])
current = ({'sha256': hashlib.sha256(f.read_bytes()).hexdigest(),
'mode': 0o755 if f.stat().st_mode & 0o111 else 0o644} if f.exists() else None)
if current != expected:
raise ValueError('concurrent remote change: ' + x['path'])
if f.exists():
old = backup / x['path']; old.parent.mkdir(parents=True, exist_ok=True)
old.write_bytes(f.read_bytes()); old.chmod(f.stat().st_mode & 0o777)
f.parent.mkdir(parents=True, exist_ok=True)
temp = f.with_name(f.name + '.' + p['id'])
with temp.open('xb') as stream:
stream.write(raw)
temp.chmod(x['mode']); os.replace(temp, f)
changed.append(x['path'])
(backup / 'receipt.json').write_text(json.dumps({'changed': changed, 'complete': False}, indent=2))
expected_after = {**p['expected'], 'files': dict(p['expected']['files'])}
for x, _, _ in entries:
expected_after['files'][x['path']] = {'sha256': x['sha256'], 'mode': x['mode']}
after = snapshot(root, for_sync=True)
if after != expected_after:
raise ValueError('post-sync identity mismatch; inspect backup ' + str(backup))
result = {'changed': changed, 'snapshot': after, 'backup': str(backup), 'complete': True}
(backup / 'receipt.json').write_text(json.dumps(result, indent=2))
return result
try:
print(json.dumps(remote_main(json.load(sys.stdin)), ensure_ascii=False))
except (ValueError, OSError, KeyError) as exc:
raise SystemExit(str(exc))
'''
def remote_call(target: dict, payload: dict) -> dict:
command = ['ssh', '-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10', '-p', str(target['port']),
target['remote'], 'python3 -c ' + shlex.quote(REMOTE_PROGRAM)]
result = subprocess.run(command, input=json.dumps(payload), capture_output=True, text=True, timeout=120)
if result.returncode:
raise ValueError('sync transport failed: ' + result.stderr.strip())
return json.loads(result.stdout)
def selection(names: list[str] | None) -> list[str] | None:
if names is None:
return None
selected = sorted(set(names))
for name in selected:
p = Path(name)
if (p.is_absolute() or '..' in p.parts or p.as_posix() != name
or not release_files.allowed(p) or name in release_files.SERVER_GENERATED):
raise ValueError('not a managed source path: ' + name)
return selected
def changes(local: dict, remote: dict, selected: list[str] | None) -> dict:
names = set(selected) if selected is not None else local['files'].keys() | remote['files'].keys()
return {name: {'before': remote['files'].get(name), 'after': local['files'].get(name)}
for name in sorted(names) if local['files'].get(name) != remote['files'].get(name)}
def validate_plan(plan: dict, target: dict, local: dict, selected: list[str] | None):
if plan.get('schema') != PLAN_SCHEMA or plan.get('target') != target:
raise ValueError('baseline target/schema does not match; preview again')
if plan.get('local') != local or plan.get('selected') != selected:
raise ValueError('local source/selection changed after preview')
def uploads(root: Path, local: dict, remote: dict, selected: list[str] | None) -> list[dict]:
diff = changes(local, remote, selected)
deleted = [name for name, item in diff.items() if item['after'] is None]
if deleted:
raise ValueError('source deletion requires separate review; refusing apply: ' + ', '.join(deleted))
entries = []
for name, item in diff.items():
raw = (root / name).read_bytes()
if hashlib.sha256(raw).hexdigest() != item['after']['sha256']:
raise ValueError('local source changed while reading: ' + name)
entries.append({'path': name, 'data': base64.b64encode(raw).decode(), **item['after']})
return entries
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--root', type=Path, default=Path(__file__).resolve().parents[1])
parser.add_argument('--remote', default=os.environ.get('BENCH_REMOTE'))
parser.add_argument('--remote-dir', default=os.environ.get('BENCH_REMOTE_DIR'))
parser.add_argument('--port', type=int, default=22)
parser.add_argument('--baseline', type=Path, help='preview writes a new LOCAL file; apply reads it unchanged')
parser.add_argument('--files', nargs='+')
parser.add_argument('--apply', action='store_true')
args = parser.parse_args(argv)
root = args.root.resolve()
selected = selection(args.files)
local = snapshot(root, for_sync=True)
if not args.remote or not args.remote_dir:
if args.apply or args.baseline or args.remote or args.remote_dir:
raise ValueError('explicit target requires both --remote and --remote-dir')
print(json.dumps({'mode': 'local-preview', 'local': local,
'next': 'set explicit --remote/--remote-dir; preview --baseline before --apply'}, indent=2))
return 0
if (args.remote.startswith('-') or any(c.isspace() for c in args.remote)
or not Path(args.remote_dir).is_absolute() or args.remote_dir == '/' or not 1 <= args.port <= 65535):
raise ValueError('invalid explicit target')
target = {'remote': args.remote, 'port': args.port, 'root': args.remote_dir}
if args.apply:
if not args.baseline or not args.baseline.is_file():
raise ValueError('apply requires an existing --baseline from preview')
plan = json.loads(args.baseline.read_text())
validate_plan(plan, target, local, selected)
files = uploads(root, local, plan['remote'], selected)
result = remote_call(target, {'operation': 'apply', 'root': target['root'],
'expected': plan['remote'], 'files': files, 'id': uuid.uuid4().hex})
print(json.dumps(result, indent=2, ensure_ascii=False))
else:
remote = remote_call(target, {'operation': 'preview', 'root': target['root']})
plan = {'schema': PLAN_SCHEMA, 'target': target, 'local': local, 'remote': remote,
'selected': selected, 'changes': changes(local, remote, selected),
'note': 'Observation only; review remote-only edits before explicitly applying.'}
if args.baseline:
with args.baseline.open('x') as stream:
json.dump(plan, stream, indent=2, ensure_ascii=False); stream.write('\n')
print(json.dumps(plan, indent=2, ensure_ascii=False))
return 0
if __name__ == '__main__':
try:
raise SystemExit(main())
except (ValueError, OSError) as exc:
raise SystemExit(str(exc))