gabriel / musehub public
harness.py python
268 lines 11.2 KB
Raw
sha256:3124154ed73a175f93e9a4fecc7aeeac8debf6b6ef7f01b45227c9f60a4128d3 test(install_e2e): the wheelhouse carries the earlier relea… Opus 5.5 13 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, *released: pathlib.Path) -> pathlib.Path:
71 """Wheels for ``muse[all]``'s dependencies, the build backend and a stand-in Swift grammar.
72
73 *released* are the earlier sdists the upgrade tests install first (``legacy_install``): their own
74 dependencies go in too, so a dependency the candidate dropped (0.2.2rc9 dropped ``keyrings.alt``) cannot make
75 the old release uninstallable and fail the upgrade gates for a reason that has nothing to do with upgrading.
76 """
77 outdir.mkdir(parents=True, exist_ok=True)
78 pip = [str(BUILDER), "-m", "pip"]
79 for cmd in (
80 [*pip, "wheel", "-q", f"{sdist}[all]", "-w", str(outdir)],
81 *([*pip, "wheel", "-q", str(r), "-w", str(outdir)] for r in released),
82 [*pip, "download", "-q", "hatchling", "-d", str(outdir)],
83 ):
84 proc = subprocess.run(cmd, capture_output=True, text=True, timeout=900)
85 assert proc.returncode == 0, f"{cmd}\n{proc.stderr}"
86 for built in outdir.glob("muse-*"):
87 built.unlink() # muse itself always comes from the sdist under test
88 return outdir
89
90
91 def write_stub_swift_wheel(outdir: pathlib.Path) -> pathlib.Path:
92 """A stand-in for ``py-tree-sitter-swift``, which cannot be built on every machine.
93
94 It lets the installer's Swift *extra handling* be exercised for real; the
95 grammar inside is deliberately unusable, so diagnostics must call it broken.
96 """
97 name, version = "py_tree_sitter_swift", "0.0.1"
98 wheel = outdir / f"{name}-{version}-py3-none-any.whl"
99 dist_info = f"{name}-{version}.dist-info"
100 files = {
101 f"{name}/__init__.py": "def language():\n raise RuntimeError('stand-in grammar')\n",
102 f"{dist_info}/METADATA": f"Metadata-Version: 2.1\nName: py-tree-sitter-swift\nVersion: {version}\n",
103 f"{dist_info}/WHEEL": "Wheel-Version: 1.0\nGenerator: harness\nRoot-Is-Purelib: true\nTag: py3-none-any\n",
104 }
105 record = "".join(f"{p},,\n" for p in files) + f"{dist_info}/RECORD,,\n"
106 with zipfile.ZipFile(wheel, "w") as z:
107 for path, content in files.items():
108 z.writestr(path, content)
109 z.writestr(f"{dist_info}/RECORD", record)
110 return wheel
111
112
113 def _write_sdist(dest: pathlib.Path, root: str, files: dict[str, str]) -> pathlib.Path:
114 import io
115
116 with tarfile.open(dest, "w:gz") as tar:
117 for name, content in files.items():
118 data = content.encode()
119 info = tarfile.TarInfo(f"{root}/{name}")
120 info.size = len(data)
121 tar.addfile(info, io.BytesIO(data))
122 return dest
123
124
125 def bad_sdist(dest_dir: pathlib.Path, version: str, kind: str) -> pathlib.Path:
126 """A fake sdist for *version* that misbehaves.
127
128 ``unbuildable`` pip fails while preparing it, before the venv is touched.
129 ``hollow`` pip *succeeds* and replaces ``muse`` with an empty package: the
130 partially-upgraded state a crash or a bad release would leave.
131 """
132 pkg_info = f"Metadata-Version: 2.1\nName: muse\nVersion: {version}\nProvides-Extra: all\n"
133 build = '[build-system]\nrequires = ["hatchling>=1.29.0"]\nbuild-backend = "hatchling.build"\n'
134 pyproject = {
135 "unbuildable": "[build-system\nthis is not toml",
136 "hollow": build + f'[project]\nname = "muse"\nversion = "{version}"\n'
137 '[tool.hatch.build.targets.wheel]\npackages = ["does_not_exist"]\n',
138 }[kind]
139 return _write_sdist(
140 dest_dir / f"muse-{version}.tar.gz", f"muse-{version}",
141 {"PKG-INFO": pkg_info, "pyproject.toml": pyproject},
142 )
143
144
145 def retag_sdist(src: pathlib.Path, dest_dir: pathlib.Path, version: str) -> pathlib.Path:
146 """Same content as *src*, published as *version* (to exercise an upgrade to the candidate)."""
147 old = sdist_version(src)
148 dest = dest_dir / f"muse-{version}.tar.gz"
149 with tarfile.open(src) as tin, tarfile.open(dest, "w:gz") as tout:
150 for member in tin.getmembers():
151 data = tin.extractfile(member).read() if member.isfile() else None # type: ignore[union-attr]
152 member.name = member.name.replace(f"muse-{old}", f"muse-{version}", 1)
153 if data is not None and member.name.endswith(("/PKG-INFO", "/pyproject.toml")):
154 data = data.decode().replace(f"Version: {old}", f"Version: {version}").replace(
155 f'version = "{old}"', f'version = "{version}"').encode()
156 member.size = len(data)
157 import io
158
159 tout.addfile(member, io.BytesIO(data) if data is not None else None)
160 return dest
161
162
163 @contextmanager
164 def serve(directory: pathlib.Path) -> Iterator[str]:
165 class _Quiet(http.server.SimpleHTTPRequestHandler):
166 def log_message(self, *args: object) -> None:
167 pass
168
169 server = http.server.ThreadingHTTPServer(
170 ("127.0.0.1", 0), functools.partial(_Quiet, directory=str(directory))
171 )
172 threading.Thread(target=server.serve_forever, daemon=True).start()
173 try:
174 yield f"http://127.0.0.1:{server.server_address[1]}"
175 finally:
176 server.shutdown()
177 server.server_close()
178
179
180 @dataclasses.dataclass
181 class Sandbox:
182 """One isolated installation target: HOME, venv and bin dir under *root*."""
183
184 root: pathlib.Path
185 wheelhouse: pathlib.Path
186
187 def __post_init__(self) -> None:
188 self.home = self.root / "home"
189 self.venv = self.root / "venv"
190 self.bin_dir = self.root / "bin"
191 self.srv = self.root / "srv"
192 (self.srv / "releases").mkdir(parents=True, exist_ok=True)
193 self.home.mkdir(exist_ok=True)
194
195 @property
196 def manifest_path(self) -> pathlib.Path:
197 return self.venv / "muse-install.json"
198
199 def manifest(self) -> dict[str, Any]:
200 data: dict[str, Any] = json.loads(self.manifest_path.read_text())
201 return data
202
203 def env(self, **extra: str) -> dict[str, str]:
204 env = {
205 k: v for k, v in os.environ.items()
206 if not k.startswith(("MUSE_", "PIP_", "VIRTUAL_ENV", "PYTHON"))
207 and k.lower() not in {"http_proxy", "https_proxy", "all_proxy", "no_proxy"}
208 }
209 env["PATH"] = f"{PYTHON.parent}{os.pathsep}{env.get('PATH', '')}"
210 env["HOME"] = str(self.home)
211 env["MUSE_KEYCHAIN_BACKEND"] = "disabled"
212 env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
213 env.update(extra)
214 return env
215
216 def install(
217 self, sdist: pathlib.Path, **env: str
218 ) -> subprocess.CompletedProcess[str]:
219 """Run the real generated installer for *sdist*; never raises on failure."""
220 version = sdist_version(sdist)
221 (self.srv / "releases" / sdist.name).write_bytes(sdist.read_bytes())
222 with serve(self.srv) as url:
223 script = self.root / "install.sh"
224 script.write_text(generate_install_script(url, version), encoding="utf-8")
225 base = {
226 "MUSE_VENV_DIR": str(self.venv),
227 "MUSE_INSTALL_DIR": str(self.bin_dir),
228 "MUSE_WHEELHOUSE": str(self.wheelhouse),
229 }
230 return subprocess.run(
231 ["sh", str(script)], env=self.env(**base, **env), cwd=self.root,
232 capture_output=True, text=True, timeout=900,
233 )
234
235 def legacy_install(self, sdist: pathlib.Path) -> None:
236 """An rc18-shaped installation: the pre-manifest installer ran a bare ``pip install <sdist>``."""
237 subprocess.run([str(PYTHON), "-m", "venv", str(self.venv)], check=True, capture_output=True)
238 proc = subprocess.run(
239 [str(self.venv / "bin" / "pip"), "install", "-q", "--no-index", "--find-links", str(self.wheelhouse), str(sdist)],
240 env=self.env(), capture_output=True, text=True, timeout=600,
241 )
242 assert proc.returncode == 0, proc.stderr
243 self.bin_dir.mkdir(exist_ok=True)
244
245 def run_python(self, code: str) -> subprocess.CompletedProcess[str]:
246 return subprocess.run(
247 [str(self.venv / "bin" / "python"), "-I", "-c", code],
248 env=self.env(), capture_output=True, text=True, timeout=120,
249 )
250
251 def muse(self, *args: str) -> subprocess.CompletedProcess[str]:
252 return subprocess.run(
253 [str(self.venv / "bin" / "muse"), *args],
254 env=self.env(), cwd=self.root, capture_output=True, text=True, timeout=300,
255 )
256
257 def doctor(self) -> tuple[int, dict[str, Any]]:
258 proc = self.muse("code", "languages", "--doctor", "--json")
259 return proc.returncode, (json.loads(proc.stdout) if proc.stdout.strip() else {})
260
261 def installed_distributions(self) -> dict[str, str]:
262 proc = self.run_python(
263 "import importlib.metadata as m, json;"
264 "print(json.dumps({d.metadata['Name'].lower(): d.version for d in m.distributions()}))"
265 )
266 assert proc.returncode == 0, proc.stderr
267 out: dict[str, str] = json.loads(proc.stdout)
268 return out
File History 5 commits
sha256:3124154ed73a175f93e9a4fecc7aeeac8debf6b6ef7f01b45227c9f60a4128d3 test(install_e2e): the wheelhouse carries the earlier relea… Opus 5.5 13 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 12 days ago
sha256:8d2339e3feaef5da2c7a3d6304444ec8d569fd4faf340cae62c47486d59fe8b6 feat(install): MUSE_PROFILE/MUSE_EXTRAS/MUSE_WHEELHOUSE and… Opus 5.5 patch 12 days ago
sha256:42b85c455874b92a57e82fafa5bcea5088a00aa5cb41493aadf125cdd9c08fc8 test(install): red e2e + contract tests for profiles, manif… Opus 5.5 patch 12 days ago