"""Prepare pinned public tools/dependencies, before any target build or execution.""" import hashlib import io import os from pathlib import Path import shutil import subprocess import sys import tarfile import tempfile import time import urllib.request ARCHIVES = { 'amd64': ('x86_64-linux', '1cbe9df9f27e6b78d14ccbca43b6703a404ef79ef1c463de901d7f088d4e2026'), 'arm64': ('aarch64-linux', '9e8d11661d4ae3bd57702a3832781e23ad151dde5798e16a5ccd503f65234ff8'), } ZIO_REVISION = '7d3a2e398bcca44afdc2e1fabd714b5922a230c0' ZIO_HASH = 'zio-0.19.0-xHbVVDjiLgBKwVXGZoPcq9hhFA5nnpb9w6VM9h_cVr_T' def download(url): request = urllib.request.Request(url, headers={'User-Agent': 'zttp-conformance-tools/1'}) print(f'downloading {url}', flush=True) started = time.monotonic() chunks = [] size = 0 with urllib.request.urlopen(request, timeout=30) as response: while True: chunk = response.read1(1024 * 1024) if not chunk: break chunks.append(chunk) size += len(chunk) if size > 80 * 1024 * 1024 or time.monotonic() - started > 180: raise ValueError('archive exceeds bootstrap resource bound') print(f'downloaded {size} bytes', flush=True) return b''.join(chunks) def main(): os.sched_setaffinity(0, sorted(os.sched_getaffinity(0))[:2]) arch, digest = ARCHIVES[sys.argv[1]] payload = download(f'https://ziglang.org/download/0.17.0/zig-{arch}-0.17.0.tar.xz') if hashlib.sha256(payload).hexdigest() != digest: raise ValueError('Zig archive checksum mismatch') with tarfile.open(fileobj=io.BytesIO(payload)) as archive: archive.extractall('/opt', filter='data') Path(f'/opt/zig-{arch}-0.17.0').rename('/opt/zig') payload = download(f'https://tangled.org/zzstoatzz.io/zio-lalinsky/archive/{ZIO_REVISION}.tar.gz') with tempfile.TemporaryDirectory() as temporary: root = Path(temporary) with tarfile.open(fileobj=io.BytesIO(payload)) as archive: members = archive.getmembers() if len(members) > 4096 or sum(member.size for member in members) > 32 * 1024 * 1024: raise ValueError('zio export exceeds bootstrap resource bound') archive.extractall(root / 'export', members=members, filter='data') roots = list((root / 'export').iterdir()) source = roots[0] if len(roots) == 1 and roots[0].is_dir() else root / 'export' clean = root / 'zio.tar' with tarfile.open(clean, 'w') as archive: for path in sorted(source.iterdir()): archive.add(path, arcname=path.name) fetched = subprocess.run(['/opt/zig/zig', 'fetch', str(clean)], cwd='/opt', capture_output=True, text=True, check=True, timeout=600) if fetched.stdout.strip() != ZIO_HASH: raise ValueError('zio package fingerprint mismatch') shutil.copytree(source, Path('/opt/zig-pkg') / ZIO_HASH) shutil.rmtree('/root/.cache', ignore_errors=True) if __name__ == '__main__': main()