SHA256
38 lines
1.9 KiB
Python
38 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Rebuild this approved M6 snapshot from M5 and explicit reviewed file list.
|
|
Not a generic working-tree archiver; preserves reference and baseline exactly.
|
|
Usage: python M6_PACK.py /path/to/M5.zip /path/to/M6.zip
|
|
"""
|
|
import csv
|
|
from pathlib import Path, PurePosixPath
|
|
import sys
|
|
import zipfile
|
|
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
MANIFEST='docs/CORE-CONTENT-MINIMAL/M6_CHANGED_FILES.tsv'
|
|
|
|
def main():
|
|
if len(sys.argv)!=3: raise SystemExit(__doc__)
|
|
base,out=map(lambda p:Path(p).resolve(),sys.argv[1:]); assert base!=out
|
|
with (ROOT/MANIFEST).open() as f:
|
|
names={r['path'] for r in csv.DictReader(f,delimiter='\t')}
|
|
names.add(MANIFEST)
|
|
for name in names:
|
|
p=PurePosixPath(name)
|
|
if p.is_absolute() or '..' in p.parts or name.startswith('reference/') or not (ROOT/name).is_file() or (ROOT/name).is_symlink():
|
|
raise ValueError(f'Unsafe/absent overlay: {name}')
|
|
with zipfile.ZipFile(base) as src,zipfile.ZipFile(out,'w',compression=zipfile.ZIP_DEFLATED,compresslevel=6) as dst:
|
|
for entry in src.infolist():
|
|
if entry.filename in names: dst.write(ROOT/entry.filename,entry.filename)
|
|
else: dst.writestr(entry,src.read(entry))
|
|
for name in sorted(names-set(src.namelist())): dst.write(ROOT/name,name)
|
|
with zipfile.ZipFile(base) as src,zipfile.ZipFile(out) as dst:
|
|
assert dst.testzip() is None
|
|
assert set(dst.namelist())==set(src.namelist())|names
|
|
ref=[n for n in src.namelist() if n.startswith('reference/') and not n.endswith('/')]
|
|
for n in src.namelist():
|
|
if n not in names: assert src.read(n)==dst.read(n),n
|
|
for n in names: assert dst.read(n)==(ROOT/n).read_bytes(),n
|
|
print(f'PASS: {len(dst.namelist())} entries, {len(ref)} reference files exact, {len(names)} reviewed overlay files; ZIP CRC OK')
|
|
if __name__=='__main__': main()
|