IsSynth-Cars / src /restore_source_checkout.py
DanielNguyen121's picture
Record epoch-32 checkpoint recovery and source backup fix
b3bd59d verified
Raw History Blame Contribute Delete
1.77 kB
#!/usr/bin/env python3
"""Restore the pinned SPC Git checkout when Molab supplies only a source snapshot."""
from pathlib import Path
import hashlib
import subprocess
ROOT = Path('/marimo/SPC')
STAGING = Path('/marimo/SPC_official_restore')
BACKUP = Path('/marimo/SPC_snapshot_backup_20261025')
COMMIT = '96834b22d5e59c852e53d713d434cbabb2f0d165'
def revision(path):
result = subprocess.run(['git', '-C', str(path), 'rev-parse', 'HEAD'], capture_output=True, text=True)
return result.stdout.strip() if result.returncode == 0 else None
if revision(ROOT) == COMMIT:
print('SPC_SOURCE_READY', COMMIT, flush=True)
else:
if (ROOT / '.git').exists():
raise RuntimeError('Existing SPC Git checkout has a different revision; refusing to replace it')
if not STAGING.exists():
subprocess.run(['git', 'clone', '--quiet', 'https://github.com/thanhhai1988/SPC.git', str(STAGING)], check=True)
subprocess.run(['git', '-C', str(STAGING), 'checkout', '--quiet', COMMIT], check=True)
if ROOT.exists():
for relative in ('data_train.py', 'util_data.py'):
existing, canonical = ROOT / relative, STAGING / relative
if not existing.exists() or hashlib.sha256(existing.read_bytes()).digest() != hashlib.sha256(canonical.read_bytes()).digest():
raise RuntimeError(f'Existing SPC snapshot differs at {relative}; refusing automatic replacement')
backup = BACKUP
suffix = 1
while backup.exists():
backup = BACKUP.with_name(f'{BACKUP.name}_{suffix}')
suffix += 1
ROOT.rename(backup)
print('SPC_SNAPSHOT_BACKUP', backup, flush=True)
STAGING.rename(ROOT)
assert revision(ROOT) == COMMIT
print('SPC_SOURCE_READY', COMMIT, flush=True)