64 lines
2.6 KiB
Python
64 lines
2.6 KiB
Python
"""Restore the original sealed v3 package into a new directory, without executing it."""
|
|
import argparse
|
|
import gzip
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
import tempfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def safe_path(root, name):
|
|
path = Path(name)
|
|
if path.is_absolute() or '..' in path.parts or not path.parts:
|
|
raise ValueError('Unsafe package path')
|
|
result = root / path
|
|
if not result.resolve().is_relative_to(root.resolve()):
|
|
raise ValueError('Package path leaves its directory')
|
|
return result
|
|
|
|
|
|
def materialize(source, destination):
|
|
source, destination = Path(source), Path(destination).absolute()
|
|
if destination.exists():
|
|
raise FileExistsError('Choose a new output directory')
|
|
entries = json.loads((source / 'PACKING.json').read_text())['files']
|
|
names = [entry['path'] for entry in entries]
|
|
if len(names) != len(set(names)):
|
|
raise ValueError('Duplicate package paths')
|
|
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
with tempfile.TemporaryDirectory(prefix='.sg90-restore-', dir=destination.parent) as temp:
|
|
stage = Path(temp) / 'package'
|
|
stage.mkdir()
|
|
for entry in entries:
|
|
original = entry['path']
|
|
expected_stored = original + ('.gz' if entry['gzip'] else '')
|
|
if entry['stored'] != expected_stored:
|
|
raise ValueError('Unexpected compressed-file mapping')
|
|
stored = safe_path(source / 'v3', entry['stored'])
|
|
target = safe_path(stage, original)
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
digest, size = hashlib.sha256(), 0
|
|
opener = gzip.open if entry['gzip'] else open
|
|
with opener(stored, 'rb') as incoming, target.open('wb') as outgoing:
|
|
for block in iter(lambda: incoming.read(1024 * 1024), b''):
|
|
size += len(block)
|
|
if size > entry['size']:
|
|
raise ValueError('Expanded file exceeds recorded size')
|
|
digest.update(block)
|
|
outgoing.write(block)
|
|
if size != entry['size'] or digest.hexdigest() != entry['sha256']:
|
|
raise ValueError('Package checksum mismatch: ' + original)
|
|
stage.rename(destination)
|
|
return len(entries)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--out', type=Path, default=ROOT / 'build/v3')
|
|
args = parser.parse_args()
|
|
count = materialize(ROOT / 'RL/reproduction', args.out)
|
|
print(json.dumps({'status': 'RESTORED', 'files': count, 'output': str(args.out)}))
|