"""Rebuild veela.py using hash-verified pure-Python wheels. Network is build-time only."""
import base64
import hashlib
import io
import json
from pathlib import Path
import sys
import urllib.request
import zipfile
ROOT = Path(__file__).resolve().parents[1]
LOCK = json.loads((ROOT / 'scripts/embedded-lock.json').read_text())
# Optional backends and test/CLI entry modules are not used by App.run().
EXCLUDE = ('h11/tests/', 'click/testing.py', 'uvicorn/__main__.py',
'uvicorn/workers.py', 'uvicorn/protocols/websockets/',
'uvicorn/protocols/http/httptools_impl.py', 'uvicorn/protocols/http/auto.py',
'uvicorn/protocols/http/auto_zttp_impl.py', 'uvicorn/protocols/http/zttp_',
'uvicorn/loops/zuvloop.py',
'uvicorn/supervisors/watchfilesreload.py', 'uvicorn/lifespan/on.py')
def build(wheel_directory):
wheel_directory.mkdir(parents=True, exist_ok=True)
entries = {}
notices = []
for name, pin in LOCK.items():
wheel = wheel_directory / pin['filename']
if not wheel.exists():
with urllib.request.urlopen(pin['url'], timeout=60) as response:
data = response.read()
if hashlib.sha256(data).hexdigest() != pin['sha256']:
raise ValueError(f'Wheel hash mismatch: {name}')
wheel.write_bytes(data)
data = wheel.read_bytes()
if hashlib.sha256(data).hexdigest() != pin['sha256']:
raise ValueError(f'Wheel hash mismatch: {name}')
with zipfile.ZipFile(io.BytesIO(data)) as source:
for filename in sorted(source.namelist()):
if filename.endswith('/') or filename.startswith(EXCLUDE):
continue
if filename.startswith(name + '/') or '.dist-info/' in filename:
if filename.endswith('/RECORD'):
continue
entries[filename] = source.read(filename)
if '/licenses/' in filename or filename.endswith(('/LICENSE', '/LICENSE.txt')):
notices.append(f'{name} {pin["version"]}: {filename}\n\n' + entries[filename].decode())
if len(notices) != len(LOCK):
raise ValueError('Missing or unexpected bundled license notices')
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as archive:
for filename, data in sorted(entries.items()):
info = zipfile.ZipInfo(filename, date_time=(1980, 1, 1, 0, 0, 0))
info.compress_type = zipfile.ZIP_DEFLATED
info.create_system = 3
info.external_attr = 0o100644 << 16
archive.writestr(info, data, compresslevel=9)
payload = base64.b64encode(buffer.getvalue()).decode('ascii')
licenses = '\n\n'.join(notices)
source = (ROOT / 'source/veela.py').read_text()
output = source.rstrip() + '\n\n\n_EMBEDDED_ZIP = ' + repr(payload) + '\n'
(ROOT / 'veela.py').write_text(output)
(ROOT / 'THIRD_PARTY_LICENSES.txt').write_text(licenses.rstrip() + '\n')
print(f'Generated veela.py: {len(output.encode()):,} bytes; ZIP: {len(buffer.getvalue()):,} bytes; {len(entries)} entries')
if __name__ == '__main__':
build(Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT / 'build/embedded-wheels')