Files
portfolio/tools/package.py
T

37 lines
2.1 KiB
Python
Raw Normal View History

"""Package the clean, reviewed portfolio checkout for publication."""
from pathlib import Path
import hashlib
import json
import subprocess
import sys
import re
import zipfile
ROOT=Path(__file__).resolve().parents[1]
OUT=ROOT/'.local'
OUT.mkdir(exist_ok=True)
VERSION=sys.argv[1] if len(sys.argv)>1 else 'v1.0.1'
assert re.fullmatch(r'v\d+\.\d+\.\d+',VERSION), 'Use a semantic version such as v1.0.1'
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/f'portfolio-{VERSION}-source.zip'
subprocess.run(['git','-C',str(ROOT),'archive','--format=zip','--prefix=portfolio/','-o',str(source),'HEAD'],check=True)
site=OUT/f'portfolio-{VERSION}-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,'version':VERSION,'history_count':int(git('rev-list','--count','HEAD')),'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))