"""构建 wheel、解压到临时目录并用 python -m 运行(模拟安装后 smoke)。""" from __future__ import annotations import os import shutil import subprocess import sys import tempfile import zipfile from pathlib import Path PYPROJECT = """\ [build-system] requires = ["uv_build>=0.9,<0.14"] build-backend = "uv_build" [project] name = "capstone-smoke" version = "1.0.0" requires-python = ">=3.12" [project.scripts] capstone-smoke = "capstone_smoke.cli:main" """ INIT = """\ __version__ = "1.0.0" """ MAIN = """\ from .cli import main raise SystemExit(main()) """ CLI = """\ import sys VERSION = "1.0.0" def main(argv: list[str] | None = None) -> int: args = list(sys.argv[1:] if argv is None else argv) if args[:1] == ["--version"]: print(f"capstone-smoke {VERSION}") return 0 print("usage: capstone-smoke --version", file=sys.stderr) return 2 """ def uv_available() -> bool: return shutil.which("uv") is not None def wheel_smoke() -> tuple[int, str]: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) package = root / "src" / "capstone_smoke" package.mkdir(parents=True) (root / "pyproject.toml").write_text(PYPROJECT, encoding="utf-8") (package / "__init__.py").write_text(INIT, encoding="utf-8") (package / "__main__.py").write_text(MAIN, encoding="utf-8") (package / "cli.py").write_text(CLI, encoding="utf-8") result = subprocess.run( ["uv", "build", "--wheel", "--offline"], cwd=root, capture_output=True, text=True, check=False, ) if result.returncode != 0: raise RuntimeError(result.stderr) wheel = next((root / "dist").glob("*.whl")) site = root / "site" with zipfile.ZipFile(wheel) as archive: archive.extractall(site) env = os.environ.copy() env["PYTHONPATH"] = str(site) run = subprocess.run( [sys.executable, "-m", "capstone_smoke", "--version"], capture_output=True, text=True, env=env, check=False, ) return run.returncode, run.stdout.strip() def main() -> None: if not uv_available(): print("uv: missing") return code, output = wheel_smoke() print(f"exit: {code}") print(f"output: {output}") if __name__ == "__main__": main()