Files
portfolio/tools/package.py
T

33 lines
1.9 KiB
Python

"""Package the clean, reviewed portfolio checkout for publication."""
from pathlib import Path
import hashlib
import json
import subprocess
import zipfile
ROOT=Path(__file__).resolve().parents[1]
OUT=ROOT/'.local'
OUT.mkdir(exist_ok=True)
def git(*args): return subprocess.check_output(['git','-C',str(ROOT),*args],text=True).strip()
if git('status','--porcelain'):
raise SystemExit('Commit the reviewed source before packaging.')
commit=git('rev-parse','HEAD')
files=git('ls-files').splitlines()
assert files and not any(name.startswith(('.local/','verification/')) for name in files)
manifest={name:hashlib.sha256((ROOT/name).read_bytes()).hexdigest() for name in files}
subprocess.run(['git','-C',str(ROOT),'archive','--format=tar','-o',str(OUT/'source.tar'),'HEAD'],check=True)
subprocess.run(['git','-C',str(ROOT),'bundle','create',str(OUT/'source.bundle'),'HEAD'],check=True)
source=OUT/'portfolio-v1.0.0-source.zip'
subprocess.run(['git','-C',str(ROOT),'archive','--format=zip','--prefix=portfolio/','-o',str(source),'HEAD'],check=True)
site=OUT/'portfolio-v1.0.0-site.zip'
with zipfile.ZipFile(site,'w',zipfile.ZIP_DEFLATED) as archive:
for path in sorted((ROOT/'dist').rglob('*')):
if path.is_file(): archive.write(path,path.relative_to(ROOT/'dist').as_posix())
packages={path.name:{'sha256':hashlib.sha256(path.read_bytes()).hexdigest(),'bytes':path.stat().st_size} for path in (source,site)}
checksums=OUT/'SHA256SUMS.txt'
checksums.write_text(''.join(f"{values['sha256']} {name}\n" for name,values in packages.items()),encoding='ascii')
packages[checksums.name]={'sha256':hashlib.sha256(checksums.read_bytes()).hexdigest(),'bytes':checksums.stat().st_size}
receipt={'commit':commit,'files':manifest,'assets':packages}
(OUT/'package.json').write_text(json.dumps(receipt,indent=2)+'\n',encoding='utf-8')
print(json.dumps({'commit':commit,'files':len(files),'assets':packages},indent=2))