harness.py
python
sha256:3124154ed73a175f93e9a4fecc7aeeac8debf6b6ef7f01b45227c9f60a4128d3
test(install_e2e): the wheelhouse carries the earlier relea…
Opus 5.5
19 hours ago
| 1 | """Real-installer harness (musehub#114, Phase 2b). |
| 2 | |
| 3 | Everything runs in temporary directories with a temporary ``HOME``; the stable |
| 4 | ``~/.local/share/muse/venv``, ``~/.local/bin`` and ``~/.muse`` are never touched. |
| 5 | The script under test is the output of :func:`generate_install_script` -- the same |
| 6 | text ``GET /install.sh`` serves -- executed by ``sh`` against a loopback server. |
| 7 | Dependencies come from an offline wheelhouse through the installer's own |
| 8 | ``MUSE_WHEELHOUSE`` support, so the runs are deterministic and need no network |
| 9 | once the wheelhouse is built. |
| 10 | |
| 11 | Environment: |
| 12 | ``MUSE_CHECKOUT`` muse working tree (default ``~/ecosystem/muse``) |
| 13 | ``MUSE_CANDIDATE_SDIST`` prebuilt candidate sdist instead of building one |
| 14 | ``MUSE_PREVIOUS_SDIST`` the previously released sdist (default ``<checkout>/dist/muse-0.2.1rc18.tar.gz``) |
| 15 | ``MUSE_OLDER_SDIST`` an older released sdist (default ``<checkout>/dist/muse-0.2.1rc17.tar.gz``) |
| 16 | """ |
| 17 | |
| 18 | from __future__ import annotations |
| 19 | |
| 20 | import dataclasses |
| 21 | import functools |
| 22 | import hashlib |
| 23 | import http.server |
| 24 | import json |
| 25 | import os |
| 26 | import pathlib |
| 27 | import re |
| 28 | import subprocess |
| 29 | import sys |
| 30 | import tarfile |
| 31 | import threading |
| 32 | import zipfile |
| 33 | from collections.abc import Iterator |
| 34 | from typing import Any |
| 35 | from contextlib import contextmanager |
| 36 | |
| 37 | from musehub.api.routes.musehub.install import generate_install_script |
| 38 | |
| 39 | CHECKOUT = pathlib.Path(os.environ.get("MUSE_CHECKOUT", pathlib.Path.home() / "ecosystem" / "muse")) |
| 40 | PYTHON = pathlib.Path(sys.base_prefix) / "bin" / "python3" |
| 41 | # The interpreter used to build sdists/wheels. The release gate sets MUSE_GATE_PYTHON to its hermetic |
| 42 | # venv; the checkout's own .venv is only a fallback for running these tests by hand. |
| 43 | BUILDER = pathlib.Path(os.environ.get("MUSE_GATE_PYTHON", CHECKOUT / ".venv" / "bin" / "python")) |
| 44 | |
| 45 | |
| 46 | def sdist_version(sdist: pathlib.Path) -> str: |
| 47 | m = re.fullmatch(r"muse-(.+)\.tar\.gz", sdist.name) |
| 48 | assert m, sdist |
| 49 | return m.group(1) |
| 50 | |
| 51 | |
| 52 | def sha256(path: pathlib.Path) -> str: |
| 53 | return hashlib.sha256(path.read_bytes()).hexdigest() |
| 54 | |
| 55 | |
| 56 | def build_candidate_sdist(outdir: pathlib.Path) -> pathlib.Path: |
| 57 | override = os.environ.get("MUSE_CANDIDATE_SDIST") |
| 58 | if override: |
| 59 | return pathlib.Path(override) |
| 60 | builder = BUILDER |
| 61 | proc = subprocess.run( |
| 62 | [str(builder), "-m", "build", "--sdist", "--outdir", str(outdir), str(CHECKOUT)], |
| 63 | capture_output=True, text=True, timeout=600, |
| 64 | ) |
| 65 | assert proc.returncode == 0, f"sdist build failed:\n{proc.stdout}\n{proc.stderr}" |
| 66 | (built,) = sorted(outdir.glob("muse-*.tar.gz")) |
| 67 | return built |
| 68 | |
| 69 | |
| 70 | def build_wheelhouse(outdir: pathlib.Path, sdist: pathlib.Path) -> pathlib.Path: |
| 71 | """Wheels for ``muse[all]``'s dependencies, the build backend and a stand-in Swift grammar.""" |
| 72 | outdir.mkdir(parents=True, exist_ok=True) |
| 73 | pip = [str(BUILDER), "-m", "pip"] |
| 74 | for cmd in ( |
| 75 | [*pip, "wheel", "-q", f"{sdist}[all]", "-w", str(outdir)], |
| 76 | [*pip, "download", "-q", "hatchling", "-d", str(outdir)], |
| 77 | ): |
| 78 | proc = subprocess.run(cmd, capture_output=True, text=True, timeout=900) |
| 79 | assert proc.returncode == 0, f"{cmd}\n{proc.stderr}" |
| 80 | for built in outdir.glob("muse-*"): |
| 81 | built.unlink() # muse itself always comes from the sdist under test |
| 82 | return outdir |
| 83 | |
| 84 | |
| 85 | def write_stub_swift_wheel(outdir: pathlib.Path) -> pathlib.Path: |
| 86 | """A stand-in for ``py-tree-sitter-swift``, which cannot be built on every machine. |
| 87 | |
| 88 | It lets the installer's Swift *extra handling* be exercised for real; the |
| 89 | grammar inside is deliberately unusable, so diagnostics must call it broken. |
| 90 | """ |
| 91 | name, version = "py_tree_sitter_swift", "0.0.1" |
| 92 | wheel = outdir / f"{name}-{version}-py3-none-any.whl" |
| 93 | dist_info = f"{name}-{version}.dist-info" |
| 94 | files = { |
| 95 | f"{name}/__init__.py": "def language():\n raise RuntimeError('stand-in grammar')\n", |
| 96 | f"{dist_info}/METADATA": f"Metadata-Version: 2.1\nName: py-tree-sitter-swift\nVersion: {version}\n", |
| 97 | f"{dist_info}/WHEEL": "Wheel-Version: 1.0\nGenerator: harness\nRoot-Is-Purelib: true\nTag: py3-none-any\n", |
| 98 | } |
| 99 | record = "".join(f"{p},,\n" for p in files) + f"{dist_info}/RECORD,,\n" |
| 100 | with zipfile.ZipFile(wheel, "w") as z: |
| 101 | for path, content in files.items(): |
| 102 | z.writestr(path, content) |
| 103 | z.writestr(f"{dist_info}/RECORD", record) |
| 104 | return wheel |
| 105 | |
| 106 | |
| 107 | def _write_sdist(dest: pathlib.Path, root: str, files: dict[str, str]) -> pathlib.Path: |
| 108 | import io |
| 109 | |
| 110 | with tarfile.open(dest, "w:gz") as tar: |
| 111 | for name, content in files.items(): |
| 112 | data = content.encode() |
| 113 | info = tarfile.TarInfo(f"{root}/{name}") |
| 114 | info.size = len(data) |
| 115 | tar.addfile(info, io.BytesIO(data)) |
| 116 | return dest |
| 117 | |
| 118 | |
| 119 | def bad_sdist(dest_dir: pathlib.Path, version: str, kind: str) -> pathlib.Path: |
| 120 | """A fake sdist for *version* that misbehaves. |
| 121 | |
| 122 | ``unbuildable`` pip fails while preparing it, before the venv is touched. |
| 123 | ``hollow`` pip *succeeds* and replaces ``muse`` with an empty package: the |
| 124 | partially-upgraded state a crash or a bad release would leave. |
| 125 | """ |
| 126 | pkg_info = f"Metadata-Version: 2.1\nName: muse\nVersion: {version}\nProvides-Extra: all\n" |
| 127 | build = '[build-system]\nrequires = ["hatchling>=1.29.0"]\nbuild-backend = "hatchling.build"\n' |
| 128 | pyproject = { |
| 129 | "unbuildable": "[build-system\nthis is not toml", |
| 130 | "hollow": build + f'[project]\nname = "muse"\nversion = "{version}"\n' |
| 131 | '[tool.hatch.build.targets.wheel]\npackages = ["does_not_exist"]\n', |
| 132 | }[kind] |
| 133 | return _write_sdist( |
| 134 | dest_dir / f"muse-{version}.tar.gz", f"muse-{version}", |
| 135 | {"PKG-INFO": pkg_info, "pyproject.toml": pyproject}, |
| 136 | ) |
| 137 | |
| 138 | |
| 139 | def retag_sdist(src: pathlib.Path, dest_dir: pathlib.Path, version: str) -> pathlib.Path: |
| 140 | """Same content as *src*, published as *version* (to exercise an upgrade to the candidate).""" |
| 141 | old = sdist_version(src) |
| 142 | dest = dest_dir / f"muse-{version}.tar.gz" |
| 143 | with tarfile.open(src) as tin, tarfile.open(dest, "w:gz") as tout: |
| 144 | for member in tin.getmembers(): |
| 145 | data = tin.extractfile(member).read() if member.isfile() else None # type: ignore[union-attr] |
| 146 | member.name = member.name.replace(f"muse-{old}", f"muse-{version}", 1) |
| 147 | if data is not None and member.name.endswith(("/PKG-INFO", "/pyproject.toml")): |
| 148 | data = data.decode().replace(f"Version: {old}", f"Version: {version}").replace( |
| 149 | f'version = "{old}"', f'version = "{version}"').encode() |
| 150 | member.size = len(data) |
| 151 | import io |
| 152 | |
| 153 | tout.addfile(member, io.BytesIO(data) if data is not None else None) |
| 154 | return dest |
| 155 | |
| 156 | |
| 157 | @contextmanager |
| 158 | def serve(directory: pathlib.Path) -> Iterator[str]: |
| 159 | class _Quiet(http.server.SimpleHTTPRequestHandler): |
| 160 | def log_message(self, *args: object) -> None: |
| 161 | pass |
| 162 | |
| 163 | server = http.server.ThreadingHTTPServer( |
| 164 | ("127.0.0.1", 0), functools.partial(_Quiet, directory=str(directory)) |
| 165 | ) |
| 166 | threading.Thread(target=server.serve_forever, daemon=True).start() |
| 167 | try: |
| 168 | yield f"http://127.0.0.1:{server.server_address[1]}" |
| 169 | finally: |
| 170 | server.shutdown() |
| 171 | server.server_close() |
| 172 | |
| 173 | |
| 174 | @dataclasses.dataclass |
| 175 | class Sandbox: |
| 176 | """One isolated installation target: HOME, venv and bin dir under *root*.""" |
| 177 | |
| 178 | root: pathlib.Path |
| 179 | wheelhouse: pathlib.Path |
| 180 | |
| 181 | def __post_init__(self) -> None: |
| 182 | self.home = self.root / "home" |
| 183 | self.venv = self.root / "venv" |
| 184 | self.bin_dir = self.root / "bin" |
| 185 | self.srv = self.root / "srv" |
| 186 | (self.srv / "releases").mkdir(parents=True, exist_ok=True) |
| 187 | self.home.mkdir(exist_ok=True) |
| 188 | |
| 189 | @property |
| 190 | def manifest_path(self) -> pathlib.Path: |
| 191 | return self.venv / "muse-install.json" |
| 192 | |
| 193 | def manifest(self) -> dict[str, Any]: |
| 194 | data: dict[str, Any] = json.loads(self.manifest_path.read_text()) |
| 195 | return data |
| 196 | |
| 197 | def env(self, **extra: str) -> dict[str, str]: |
| 198 | env = { |
| 199 | k: v for k, v in os.environ.items() |
| 200 | if not k.startswith(("MUSE_", "PIP_", "VIRTUAL_ENV", "PYTHON")) |
| 201 | and k.lower() not in {"http_proxy", "https_proxy", "all_proxy", "no_proxy"} |
| 202 | } |
| 203 | env["PATH"] = f"{PYTHON.parent}{os.pathsep}{env.get('PATH', '')}" |
| 204 | env["HOME"] = str(self.home) |
| 205 | env["MUSE_KEYCHAIN_BACKEND"] = "disabled" |
| 206 | env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1" |
| 207 | env.update(extra) |
| 208 | return env |
| 209 | |
| 210 | def install( |
| 211 | self, sdist: pathlib.Path, **env: str |
| 212 | ) -> subprocess.CompletedProcess[str]: |
| 213 | """Run the real generated installer for *sdist*; never raises on failure.""" |
| 214 | version = sdist_version(sdist) |
| 215 | (self.srv / "releases" / sdist.name).write_bytes(sdist.read_bytes()) |
| 216 | with serve(self.srv) as url: |
| 217 | script = self.root / "install.sh" |
| 218 | script.write_text(generate_install_script(url, version), encoding="utf-8") |
| 219 | base = { |
| 220 | "MUSE_VENV_DIR": str(self.venv), |
| 221 | "MUSE_INSTALL_DIR": str(self.bin_dir), |
| 222 | "MUSE_WHEELHOUSE": str(self.wheelhouse), |
| 223 | } |
| 224 | return subprocess.run( |
| 225 | ["sh", str(script)], env=self.env(**base, **env), cwd=self.root, |
| 226 | capture_output=True, text=True, timeout=900, |
| 227 | ) |
| 228 | |
| 229 | def legacy_install(self, sdist: pathlib.Path) -> None: |
| 230 | """An rc18-shaped installation: the pre-manifest installer ran a bare ``pip install <sdist>``.""" |
| 231 | subprocess.run([str(PYTHON), "-m", "venv", str(self.venv)], check=True, capture_output=True) |
| 232 | proc = subprocess.run( |
| 233 | [str(self.venv / "bin" / "pip"), "install", "-q", "--no-index", "--find-links", str(self.wheelhouse), str(sdist)], |
| 234 | env=self.env(), capture_output=True, text=True, timeout=600, |
| 235 | ) |
| 236 | assert proc.returncode == 0, proc.stderr |
| 237 | self.bin_dir.mkdir(exist_ok=True) |
| 238 | |
| 239 | def run_python(self, code: str) -> subprocess.CompletedProcess[str]: |
| 240 | return subprocess.run( |
| 241 | [str(self.venv / "bin" / "python"), "-I", "-c", code], |
| 242 | env=self.env(), capture_output=True, text=True, timeout=120, |
| 243 | ) |
| 244 | |
| 245 | def muse(self, *args: str) -> subprocess.CompletedProcess[str]: |
| 246 | return subprocess.run( |
| 247 | [str(self.venv / "bin" / "muse"), *args], |
| 248 | env=self.env(), cwd=self.root, capture_output=True, text=True, timeout=300, |
| 249 | ) |
| 250 | |
| 251 | def doctor(self) -> tuple[int, dict[str, Any]]: |
| 252 | proc = self.muse("code", "languages", "--doctor", "--json") |
| 253 | return proc.returncode, (json.loads(proc.stdout) if proc.stdout.strip() else {}) |
| 254 | |
| 255 | def installed_distributions(self) -> dict[str, str]: |
| 256 | proc = self.run_python( |
| 257 | "import importlib.metadata as m, json;" |
| 258 | "print(json.dumps({d.metadata['Name'].lower(): d.version for d in m.distributions()}))" |
| 259 | ) |
| 260 | assert proc.returncode == 0, proc.stderr |
| 261 | out: dict[str, str] = json.loads(proc.stdout) |
| 262 | return out |
File History
5 commits
sha256:3124154ed73a175f93e9a4fecc7aeeac8debf6b6ef7f01b45227c9f60a4128d3
test(install_e2e): the wheelhouse carries the earlier relea…
Opus 5.5
19 hours ago
sha256:a977b39d08e15683210a33608e25712fe3ed9c8f05f0388fb69ef38acf835426
feat(release): hermetic release gates in a provisioned, pin…
Opus 5.5
minor
⚠
12 days ago
sha256:ea520418c21db35a36ce28fd7a0f2426a89e14499f689bfb2861046d0fd37d9f
test(install): type the e2e harness; drop unused wheelhouse…
Opus 5.5
13 days ago
sha256:8d2339e3feaef5da2c7a3d6304444ec8d569fd4faf340cae62c47486d59fe8b6
feat(install): MUSE_PROFILE/MUSE_EXTRAS/MUSE_WHEELHOUSE and…
Opus 5.5
patch
13 days ago
sha256:42b85c455874b92a57e82fafa5bcea5088a00aa5cb41493aadf125cdd9c08fc8
test(install): red e2e + contract tests for profiles, manif…
Opus 5.5
patch
13 days ago