"""Immutable content-addressed Web releases with offline gzip compression.""" from pathlib import Path import hashlib, gzip, json, shutil, os root=Path(__file__).resolve().parents[1] source=root/'build/web' output=root/'build/web-deploy' files=sorted(p for p in source.iterdir() if p.is_file() and not p.name.endswith(('.gz','.br'))) identity=hashlib.sha256() for p in files: identity.update(p.name.encode()+b'\0'+p.read_bytes()) version=identity.hexdigest()[:16] if output.exists(): shutil.rmtree(output) release=output/'releases'/version release.mkdir(parents=True) report=[] for p in files: destination=release/p.name shutil.copyfile(p,destination) raw=p.read_bytes() if p.suffix in ['.wasm','.pck','.js','.html']: compressed=gzip.compress(raw,compresslevel=9,mtime=0) assert gzip.decompress(compressed)==raw z=destination.with_name(destination.name+'.gz') z.write_bytes(compressed) os.utime(z,(destination.stat().st_mtime,destination.stat().st_mtime)) report.append({'file':p.name,'original':len(raw),'gzip':len(compressed),'sha256':hashlib.sha256(raw).hexdigest()}) html=(source/'index.html').read_text() html=html.replace('','\n\t\t',1) (output/'index.html').write_text(html) (root/'build/web-compression-report.json').write_text(json.dumps({'version':version,'files':report},indent=2)+'\n') print('Release:',version) for r in report: print(r['file'],r['original'],'->',r['gzip']) print('Total:',sum(r['original'] for r in report),'->',sum(r['gzip'] for r in report))