File size: 3,971 Bytes
6028162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cdcdbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6028162
2cdcdbd
6028162
2cdcdbd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""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())