robottt-pi05-base-comparison / code /scripts /archive_runtime.py
jisue's picture
Experiment backup 2026-11-10T07:15:16.140705+00:00
2cdcdbd verified
Raw History Blame Contribute Delete
3.97 kB
"""Persist an inode-efficient runtime archive and restore its disposable directory."""
import argparse,hashlib,json,os,pathlib,shutil,tarfile,time
def sha(path):
digest=hashlib.sha256()
with path.open('rb') as source:
while chunk:=source.read(8*1024*1024):digest.update(chunk)
return digest.hexdigest()
def write(path,value):
temporary=path.with_name(path.name+'.partial');temporary.write_text(json.dumps(value,indent=2));temporary.replace(path)
def pack(source,archive):
if archive.exists():raise FileExistsError('Refusing to overwrite a completed runtime archive')
temporary=archive.with_suffix('.partial');archive.parent.mkdir(parents=True,exist_ok=True)
def keep(info):
return None if '__pycache__' in pathlib.PurePosixPath(info.name).parts or info.name.endswith(('.pyc','.pyo','.nbi','.nbc')) else info
with tarfile.open(temporary,'w:gz',compresslevel=1) as output:output.add(source,arcname='.',filter=keep)
with temporary.open('rb') as complete:os.fsync(complete.fileno())
digest=sha(temporary);temporary.replace(archive)
record=dict(archive=str(archive),sha256=digest,bytes=archive.stat().st_size,source=str(source),created_unix_time=time.time())
write(archive.parent/'runtime-archive.json',record)
print(json.dumps(record),flush=True)
def restore(archive,output):
metadata=json.loads((archive.parent/'runtime-archive.json').read_text())
if sha(archive)!=metadata['sha256']:raise ValueError('Runtime archive checksum mismatch')
ready=output/'.robottt-runtime-ready.json'
if ready.exists() and json.loads(ready.read_text()).get('archive_sha256')==metadata['sha256']:return
temporary=output.with_name(output.name+'.restore-'+str(os.getpid()));temporary.mkdir(parents=True,exist_ok=False)
# This is our own checksum-verified executable environment, including its
# intentional absolute Python interpreter symlink.
with tarfile.open(archive,'r:gz') as source:source.extractall(temporary)
write(temporary/'.robottt-runtime-ready.json',dict(archive_sha256=metadata['sha256'],restored_unix_time=time.time()))
if output.exists():shutil.rmtree(output)
temporary.replace(output)
print(json.dumps(dict(restored=str(output),archive_sha256=metadata['sha256'])),flush=True)
def restore_support(archive):
metadata=json.loads((archive.parent/'runtime-support.json').read_text())
python=pathlib.Path(metadata['python_target']);packages=archive.parent/'system-packages'
if (python/'bin/python3.11').is_file() and list(packages.glob('*.deb')):return
if sha(archive)!=metadata['sha256']:raise ValueError('Runtime support archive checksum mismatch')
temporary=pathlib.Path('/tmp')/('robottt-support-restore-'+str(os.getpid()));temporary.mkdir(exist_ok=False)
try:
with tarfile.open(archive,'r:gz') as source:source.extractall(temporary)
if not (python/'bin/python3.11').is_file():
if python.exists():raise RuntimeError('Existing managed Python is incomplete; refusing to overwrite it')
python.parent.mkdir(parents=True,exist_ok=True);shutil.move(temporary/'python',python)
packages.mkdir(parents=True,exist_ok=True)
for package in (temporary/'system-packages').glob('*.deb'):
target=packages/package.name
if not target.exists():shutil.copyfile(package,target)
finally:shutil.rmtree(temporary)
if __name__=='__main__':
parser=argparse.ArgumentParser();parser.add_argument('action',choices=['pack','restore','restore-support']);parser.add_argument('--archive',required=True);parser.add_argument('--source');parser.add_argument('--output');args=parser.parse_args()
if args.action=='pack':pack(pathlib.Path(args.source).resolve(),pathlib.Path(args.archive).resolve())
elif args.action=='restore':restore(pathlib.Path(args.archive).resolve(),pathlib.Path(args.output).resolve())
else:restore_support(pathlib.Path(args.archive).resolve())