#!/usr/bin/env python3 """One-command installability verification for the `cain-trust` distribution (audit, DX gap). The audit found `pip install cain-trust` could not be trusted to work from a clean environment. This reproduces the audit's own verification on demand: python3 scripts/verify_installability.py It copies the packaging inputs into a temporary directory (so the live tree is never polluted), builds the wheel, installs it into a fresh virtual environment, then: * imports `cain`, * imports `cain_sdk`, * runs the `cain` console entry point (`cain --help`), * checks the packaged trust roots ship inside the wheel. Every check is reported; the result is PASS only when all pass. It needs only the standard library plus whatever the distribution needs at import time (installed system-wide with --system-site-packages). It imports nothing from CAIN. Exit 0 on PASS, 1 on any failure. """ from __future__ import annotations import json import shutil import subprocess import sys import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] COPY = ("pyproject.toml", "setup.py", "README.md", "MANIFEST.in", "cain", "cain_sdk", "cain_cli_entry.py") PACKAGE = "cain-trust" def run(cmd, cwd=None, env=None, timeout=1800): return subprocess.run(cmd, cwd=cwd, env=env, capture_output=True, text=True, timeout=timeout) def main() -> int: checks = [] def check(name, ok, detail=""): checks.append({"check": name, "ok": bool(ok), "detail": detail}) with tempfile.TemporaryDirectory(prefix="cain-install-") as tmp: src = Path(tmp) / "src" src.mkdir() for name in COPY: p = ROOT / name if not p.exists(): continue if p.is_dir(): shutil.copytree(p, src / name, ignore=shutil.ignore_patterns("__pycache__", "*.pyc")) else: shutil.copy2(p, src / name) check("packaging inputs copied", (src / "pyproject.toml").exists() and (src / "cain").is_dir()) wheels = Path(tmp) / "wheels" wheels.mkdir() r = run([sys.executable, "-m", "pip", "wheel", ".", "--no-deps", "-w", str(wheels), "-q"], cwd=src) built = sorted(wheels.glob("cain_trust-*.whl")) check("wheel builds", r.returncode == 0 and bool(built), (r.stderr or r.stdout)[-400:]) if not built: _report(checks) return 1 wheel = built[0] with zipfile.ZipFile(wheel) as z: names = z.namelist() ep = "\n".join(_read(z, n) for n in names if n.endswith("entry_points.txt")) check("wheel ships the cain package", any(n.startswith("cain/__init__.py") for n in names)) check("wheel ships the packaged substrate", any(n.startswith("cain/substrate/__init__.py") for n in names)) check("wheel ships cain_sdk", any(n.startswith("cain_sdk/") for n in names)) check("wheel ships the trust roots", any("trust_roots/freetsa" in n for n in names), "cain/substrate/trust_roots/*.pem in package-data") check("wheel declares the cain entry point", "cain_cli_entry" in ep and "cain" in ep, ep.strip()[:200]) venv = Path(tmp) / "venv" r = run([sys.executable, "-m", "venv", "--system-site-packages", str(venv)]) check("virtual environment created", r.returncode == 0, r.stderr[-200:]) py = venv / "bin" / "python" if not py.exists(): _report(checks) return 1 r = run([str(py), "-m", "pip", "install", "--no-deps", "-q", str(wheel)]) check("wheel installs cleanly", r.returncode == 0, (r.stderr or r.stdout)[-400:]) # Run from a neutral directory so the repository tree is NOT on sys.path; otherwise these would import # the source checkout instead of the installed wheel and prove nothing. neutral = Path(tmp) / "cwd" neutral.mkdir() r = run([str(py), "-c", "import cain, sys; print(cain.__file__)"], cwd=neutral) installed = "site-packages" in (r.stdout or "") check("`import cain` works after install (from the wheel, not the checkout)", r.returncode == 0 and installed, (r.stdout + r.stderr)[-300:]) r = run([str(py), "-c", "import cain_sdk, cain_sdk.action_decision; print('ok')"], cwd=neutral) check("`import cain_sdk` works after install", r.returncode == 0, (r.stderr or r.stdout)[-300:]) cain_bin = venv / "bin" / "cain" r = run([str(cain_bin), "--help"], cwd=neutral) if cain_bin.exists() else run( [str(py), "-m", "cain_cli_entry", "--help"], cwd=neutral) check("`cain --help` runs after install", r.returncode == 0 and "CAIN Trust Kernel CLI" in (r.stdout + r.stderr), (r.stdout + r.stderr)[-200:]) # ---- second package shown to engineers: the client SDK (sdk/python -> cainstudio) ---- sdk_src = Path(tmp) / "sdk_python" shutil.copytree(ROOT / "sdk" / "python", sdk_src, ignore=shutil.ignore_patterns("__pycache__", "*.pyc", "*.egg-info")) check("client-SDK packaging inputs copied", (sdk_src / "pyproject.toml").exists()) sdk_wheels = Path(tmp) / "sdk_wheels" sdk_wheels.mkdir() r = run([sys.executable, "-m", "pip", "wheel", ".", "--no-deps", "-w", str(sdk_wheels), "-q"], cwd=sdk_src) sdk_built = sorted(sdk_wheels.glob("cainstudio-*.whl")) check("cainstudio wheel builds", r.returncode == 0 and bool(sdk_built), (r.stderr or r.stdout)[-300:]) if sdk_built: with zipfile.ZipFile(sdk_built[0]) as z: snames = z.namelist() sep = "\n".join(_read(z, n) for n in snames if n.endswith("entry_points.txt")) check("cainstudio wheel ships the package", any(n.startswith("cainstudio/") for n in snames)) check("cainstudio wheel declares its CLI", "cainstudio" in sep, sep.strip()[:200]) r = run([str(py), "-m", "pip", "install", "--no-deps", "-q", str(sdk_built[0])]) check("cainstudio installs cleanly", r.returncode == 0, (r.stderr or r.stdout)[-300:]) r = run([str(py), "-c", "import cainstudio; print(cainstudio.__file__)"], cwd=neutral) check("`import cainstudio` works after install", r.returncode == 0 and "site-packages" in (r.stdout or ""), (r.stdout + r.stderr)[-300:]) cli = venv / "bin" / "cainstudio" r = run([str(cli), "--help"], cwd=neutral) if cli.exists() else run( [str(py), "-m", "cainstudio.cli", "--help"], cwd=neutral) check("`cainstudio --help` runs after install", r.returncode == 0, (r.stdout + r.stderr)[-200:]) ok = all(c["ok"] for c in checks) _report(checks, result="PASS" if ok else "FAIL") return 0 if ok else 1 def _read(z: zipfile.ZipFile, name: str) -> str: try: return z.read(name).decode("utf-8", "replace") except Exception: return "" def _report(checks, result=None): out = {"verifier": "verify_installability.py", "package": PACKAGE, "checks": checks, "passed": sum(1 for c in checks if c["ok"]), "total": len(checks)} if result: out["result"] = result print(json.dumps(out, indent=2)) if __name__ == "__main__": raise SystemExit(main())