omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / x86 / prepare.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Extract an amd64 Ubuntu Python oracle without installing host packages. |
| 3 | |
| 4 | Usage: python3 tests/native/x86/prepare.py EMPTY_OUTPUT |
| 5 | Reads Ubuntu noble amd64 package indices, verifies each archive against its |
| 6 | index SHA256 and records exact versions/digests in runtime-packages.json. |
| 7 | The extracted runtime is test infrastructure only, never a bridge dependency. |
| 8 | No interpreter or candidate binary is executed by this preparation command. |
| 9 | """ |
| 10 | import gzip |
| 11 | import hashlib |
| 12 | import json |
| 13 | from pathlib import Path |
| 14 | import shlex |
| 15 | import subprocess |
| 16 | import sys |
| 17 | import urllib.request |
| 18 | |
| 19 | PACKAGES = ("python3.12-minimal", "libpython3.12-minimal", "libpython3.12-stdlib", |
| 20 | "libexpat1", "libffi8", "zlib1g", "libbz2-1.0", "liblzma5", "libssl3t64", |
| 21 | "libuuid1", "libreadline8t64", "libtinfo6", "libncursesw6", "libsqlite3-0", |
| 22 | "libmpdec3", "libcrypt1", "libdb5.3t64", "libgdbm6t64", "libgdbm-compat4t64") |
| 23 | # These dependencies are supplied by the cross-toolchain sysroot. Record the |
| 24 | # sysroot separately in the integration receipt; do not copy its files here. |
| 25 | SYSTEM = {"libc6", "libm6", "libgcc-s1"} |
| 26 | |
| 27 | |
| 28 | def fetch(url): |
| 29 | with urllib.request.urlopen(url, timeout=120) as response: |
| 30 | return response.read() |
| 31 | |
| 32 | |
| 33 | def records(data): |
| 34 | for block in data.decode().split("\n\n"): |
| 35 | fields = {} |
| 36 | for line in block.splitlines(): |
| 37 | if line and not line[0].isspace() and ": " in line: |
| 38 | key, value = line.split(": ", 1) |
| 39 | fields[key] = value |
| 40 | if "Package" in fields: |
| 41 | yield fields |
| 42 | |
| 43 | |
| 44 | def main(): |
| 45 | if len(sys.argv) != 2: |
| 46 | raise SystemExit("usage: prepare.py EMPTY_OUTPUT") |
| 47 | out = Path(sys.argv[1]).resolve() |
| 48 | if out.exists() and any(out.iterdir()): |
| 49 | raise SystemExit("refusing nonempty runtime output: " + str(out)) |
| 50 | out.mkdir(parents=True, exist_ok=True) |
| 51 | root = out / "root" |
| 52 | root.mkdir() |
| 53 | available = {} |
| 54 | indices = [] |
| 55 | # The later indices override the earlier base package versions. |
| 56 | for suite in ("noble", "noble-updates", "noble-security"): |
| 57 | url = f"https://archive.ubuntu.com/ubuntu/dists/{suite}/main/binary-amd64/Packages.gz" |
| 58 | compressed = fetch(url) |
| 59 | indices.append({"url": url, "sha256": hashlib.sha256(compressed).hexdigest()}) |
| 60 | for package in records(gzip.decompress(compressed)): |
| 61 | if package["Package"] in PACKAGES: |
| 62 | previous = available.get(package["Package"]) |
| 63 | if previous is None or subprocess.run(["dpkg", "--compare-versions", package["Version"], "gt", previous["Version"]]).returncode == 0: |
| 64 | available[package["Package"]] = package |
| 65 | manifest = {"architecture": "amd64", "indices": indices, "packages": [], |
| 66 | "sysroot_dependencies": sorted(SYSTEM), "execution": "NOT_RUN"} |
| 67 | for name in PACKAGES: |
| 68 | # libmpdec is not used by every distro build of CPython; it is optional |
| 69 | # only if the archive does not publish it. ctypes dependencies are required. |
| 70 | if name not in available: |
| 71 | if name == "libmpdec3": |
| 72 | continue |
| 73 | raise SystemExit("package absent from indices: " + name) |
| 74 | package = available[name] |
| 75 | url = "https://archive.ubuntu.com/ubuntu/" + package["Filename"] |
| 76 | data = fetch(url) |
| 77 | actual = hashlib.sha256(data).hexdigest() |
| 78 | if actual != package["SHA256"]: |
| 79 | raise SystemExit("archive SHA256 mismatch: " + name) |
| 80 | archive = out / Path(package["Filename"]).name |
| 81 | archive.write_bytes(data) |
| 82 | subprocess.run(["dpkg-deb", "--extract", str(archive), str(root)], check=True) |
| 83 | manifest["packages"].append({"name": name, "version": package["Version"], "url": url, "sha256": actual}) |
| 84 | print(name + " " + package["Version"] + " sha256=" + actual, flush=True) |
| 85 | (out / "runtime-packages.json").write_text(json.dumps(manifest, indent=2) + "\n") |
| 86 | print("export OMEGA_NATIVE_X86_PYTHON=" + shlex.quote(str(root / "usr/bin/python3.12"))) |
| 87 | print("export OMEGA_NATIVE_X86_LIBPATH=" + shlex.quote(str(root / "usr/lib/x86_64-linux-gnu") + ":" + str(root / "lib/x86_64-linux-gnu") + ":/usr/x86_64-linux-gnu/lib")) |
| 88 | |
| 89 | |
| 90 | if __name__ == "__main__": |
| 91 | main() |