Files
OBLITERATUS/scripts/release_dependency_bytecode.py
Joseph Magly eb38b1508e ci: precompile verified release dependencies
Prepare only locked dependencies in owned release environments before installing the exact project. Preserve verified wheel-supplied caches outside import paths, require zero caches before compilation, and bind generated cache bytes to source and retained diagnostics.

Keep project caches forbidden, preserve the no-write startup policy, sanitize inherited compilation settings, and retain bootstrap evidence separately without changing test selection or release gates.

Refs #306
2026-10-29 06:44:59 -04:00

211 lines
11 KiB
Python

"""Compile only locked dependencies in an already claimed disposable release venv.
The receipt is bootstrap diagnostics, not coverage acceptance metadata. Cache
hashes are checked within this environment, never compared between hosts.
"""
from __future__ import annotations
import argparse
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys
from scripts.release_bytecode_policy import canonical, owned_site, ownership, reject_project_bytecode
from scripts.release_checkout_identity import verify_checkout
from scripts.release_dependency_quarantine import dependency_sources, destination, prepare, verify_preserved
from scripts.release_source_identity import source_identity
from scripts.release_test_environment import release_extras
UV_VERSION = "0.12.4"
def digest(path: Path) -> str:
return hashlib.sha256(path.read_bytes()).hexdigest()
def project_absent(directory: Path) -> None:
if (list(directory.glob("obliteratus*")) or (directory / "app.py").exists()
or (directory / "app.py").is_symlink()):
raise ValueError("dependency compilation requires the project to be absent")
reject_project_bytecode(directory / "obliteratus")
def tool_identity(uv: str) -> dict:
executable = shutil.which(uv)
if executable is None:
raise ValueError("pinned uv executable is missing")
path = Path(executable).resolve()
version = subprocess.check_output([str(path), "--version"], text=True).strip()
if version.split()[:2] != ["uv", UV_VERSION]:
raise ValueError(f"dependency bytecode requires uv {UV_VERSION}")
return {"path": str(path), "version": version, "sha256": digest(path)}
def binding(root: Path, environment: Path, uv: str) -> dict:
directory = owned_site(environment)
if sys.pycache_prefix is not None or os.environ.get("PYTHONPYCACHEPREFIX"):
raise ValueError("dependency caches must remain in the owned site-packages")
revision = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=root, text=True).strip()
return {
"schema_version": 1, "scope": "owned-dependencies-before-project",
"environment": ownership(environment), "site_packages": str(directory),
"target_sha": revision, "checkout_tree_digest": verify_checkout(root, revision),
"project_sha256": digest(root / "pyproject.toml"), "lock_sha256": digest(root / "uv.lock"),
"extras": release_extras(root / "pyproject.toml"), "uv": tool_identity(uv),
"helpers": {name: digest(Path(__file__).with_name(name)) for name in (
"release_dependency_bytecode.py", "release_bytecode_policy.py", "release_checkout_identity.py",
"release_source_identity.py", "release_test_environment.py", "release_dependency_quarantine.py")},
"python": {"version": platform.python_version(), "cache_tag": sys.implementation.cache_tag,
"optimization": sys.flags.optimize,
"magic": importlib.util.MAGIC_NUMBER.hex(), "executable_sha256": digest(Path(sys.executable))},
}
def inventory(directory: Path) -> dict:
"""Bind source and cache bytes; timestamp header agreement alone is not proof."""
reject_project_bytecode(directory / "obliteratus")
sources, caches = dependency_sources(directory), {}
paths = sorted(directory.rglob("*"))
# source_from_cache rejects legitimate dotted filenames (e.g. Optuna's
# v0.9.0.a.py migration). Derive allowed caches from inventoried sources.
expected_caches = {Path(importlib.util.cache_from_source(str(directory / name), optimization=level)): name
for name in sources for level in ("", "1", "2")}
for path in paths:
if path.suffix != ".pyc":
continue
canonical(path)
name = expected_caches.get(path)
if name is None:
raise ValueError(f"dependency cache has no inventoried source: {path}")
source = directory / name
data, info = path.read_bytes(), source.stat()
if len(data) < 16 or data[:4] != importlib.util.MAGIC_NUMBER:
raise ValueError(f"dependency cache has a foreign or incomplete header: {path}")
flags = int.from_bytes(data[4:8], "little")
if flags == 0:
valid = (int.from_bytes(data[8:12], "little") == int(info.st_mtime) & 0xFFFFFFFF
and int.from_bytes(data[12:16], "little") == info.st_size & 0xFFFFFFFF)
mode = "timestamp"
elif flags == 3:
valid = data[8:16] == importlib.util.source_hash(source.read_bytes())
mode = "checked-hash"
else:
raise ValueError(f"dependency cache has unchecked or invalid flags: {path}")
if not valid:
raise ValueError(f"dependency cache header differs from source: {path}")
caches[path.relative_to(directory).as_posix()] = {
"source": name, "mode": mode, "header": data[:16].hex(),
"sha256": hashlib.sha256(data).hexdigest(),
}
return {"sources": sources, "caches": caches}
def compile_dependencies(root: Path, environment: Path, uv: str, receipt: Path) -> None:
directory = owned_site(environment)
project_absent(directory)
precompile = receipt.with_name(receipt.stem + "-precompile" + receipt.suffix)
if any(path.exists() or path.is_symlink() for path in (receipt, precompile)):
raise ValueError("bootstrap receipt already exists; never rebind or overwrite it")
archive = destination(receipt, root, environment)
before = binding(root, environment, uv)
command = [before["uv"]["path"], "sync", "--locked", "--no-default-groups"]
for extra in before["extras"]:
command.extend(["--extra", extra])
command += ["--no-install-project", "--no-editable", "--link-mode", "copy"]
# uv 0.12.4 treats even an inherited false value as conflicting with the
# negative CLI flag. Remove only this setting; choose both stages explicitly.
env = {key: value for key, value in os.environ.items() if key != "UV_COMPILE_BYTECODE"}
env.update(UV_PROJECT_ENVIRONMENT=str(environment), PYTHONDONTWRITEBYTECODE="1")
sync_command = [*command, "--no-compile-bytecode"]
print(json.dumps({"stage": "install-final-dependencies", "command": sync_command,
"environment_removed_for_explicit_flags": ["UV_COMPILE_BYTECODE"]}), flush=True)
subprocess.run(sync_command, cwd=root, env=env, check=True)
project_absent(directory)
uncompiled = dependency_sources(directory)
if binding(root, environment, uv) != before:
raise ValueError("bootstrap identity changed during dependency installation")
preparation = prepare(directory, archive, uncompiled, before)
# uv uses force=False: an existing timestamp cache may preserve stale code.
# Only verified wheel-supplied inputs may be preserved outside the venv.
# Every compiler run must still start with zero caches.
if list(directory.rglob("*.pyc")):
raise ValueError("dependency compilation requires a fresh cache-free environment")
if inventory(directory)["sources"] != uncompiled:
raise ValueError("dependency source changed during cache preparation")
if binding(root, environment, uv) != before:
raise ValueError("bootstrap identity changed during dependency installation")
command += ["--compile-bytecode"]
receipt.parent.mkdir(parents=True, exist_ok=True)
with precompile.open("x", encoding="utf-8") as stream:
json.dump({**before, "stage": "ready-to-compile", "cache_free": True, "sources": uncompiled,
"preparation": preparation, "command": command, "dependency_sync_command": sync_command},
stream, indent=2, sort_keys=True)
stream.write("\n")
print(json.dumps({"stage": "compile-dependencies", "command": command}), flush=True)
subprocess.run(command, cwd=root, env=env, check=True)
project_absent(directory)
if binding(root, environment, uv) != before:
raise ValueError("bootstrap source, ownership, Python or uv changed during compilation")
recorded = inventory(directory)
if recorded["sources"] != uncompiled:
raise ValueError("dependency source bytes changed during compilation")
if not recorded["sources"] or not recorded["caches"]:
raise ValueError("dependency compilation produced no source/cache inventory")
verify_preserved(preparation)
with receipt.open("x", encoding="utf-8") as stream:
json.dump({**before, **recorded, "preparation": preparation,
"command": command, "dependency_sync_command": sync_command},
stream, indent=2, sort_keys=True)
stream.write("\n")
print(json.dumps({"receipt": str(receipt), "receipt_sha256": digest(receipt),
"sources": len(recorded["sources"]), "caches": len(recorded["caches"])}))
def verify_dependencies(root: Path, environment: Path, uv: str, receipt: Path) -> None:
if receipt.is_symlink() or not receipt.is_file():
raise ValueError("bootstrap receipt must be a regular retained file")
recorded = json.loads(receipt.read_text(encoding="utf-8"))
current = binding(root, environment, uv)
if not isinstance(recorded, dict) or type(recorded.get("schema_version")) is not int:
raise ValueError("invalid bootstrap receipt schema")
if any(recorded.get(key) != value for key, value in current.items()):
raise ValueError("bootstrap source, ownership, Python or uv identity differs")
verify_preserved(recorded["preparation"])
actual = inventory(owned_site(environment))
if any(recorded.get(key) != value for key, value in actual.items()):
raise ValueError("dependency source/cache bytes changed after compilation")
# Run outside the checkout, as the workflow does, so source egg-info cannot
# substitute for the noneditable distribution installed in the owned venv.
installed = source_identity(root)["source_roots"][1]
if Path(installed) != owned_site(environment) / "obliteratus":
raise ValueError("project distribution is not installed in the owned site-packages")
app = root / "app.py"
if app.is_file() and digest(app) != digest(owned_site(environment) / "app.py"):
raise ValueError("installed app module differs from exact project source")
print(json.dumps({"verified": True, "receipt_sha256": digest(receipt),
"project_bytecode_absent": True, "dependency_bytes_unchanged": True}))
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("command", choices=("compile", "verify"))
parser.add_argument("--root", required=True, type=Path)
parser.add_argument("--environment", required=True, type=Path)
parser.add_argument("--receipt", required=True, type=Path)
parser.add_argument("--uv", default="uv")
args = parser.parse_args()
function = compile_dependencies if args.command == "compile" else verify_dependencies
function(args.root.resolve(), args.environment, args.uv, args.receipt)
if __name__ == "__main__":
main()