diff --git a/.chronus/changes/python-playground-browser-wheels-offline-2026-10-16.md b/.chronus/changes/python-playground-browser-wheels-offline-2026-10-16.md new file mode 100644 index 00000000000..c8782c4f196 --- /dev/null +++ b/.chronus/changes/python-playground-browser-wheels-offline-2026-10-16.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Fix playground generation by bundling the generator's Python dependencies with the emitter instead of resolving them from PyPI in the browser. The playground build obtains hash-pinned dependencies from the configured Python feed and builds pure-Python PyYAML and MarkupSafe wheels without downloading from the Pyodide CDN. Update the generator's PyYAML dependency to 6.0.3. diff --git a/cspell.yaml b/cspell.yaml index 806c508b70d..d795d3527ab 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -245,6 +245,7 @@ words: - pyright - pyrightconfig - pytest + - pytokens - pytyped - pyyaml - rcfile diff --git a/packages/http-client-python/ARCHITECTURE.md b/packages/http-client-python/ARCHITECTURE.md index 69b4122745a..640bdf9d5f8 100644 --- a/packages/http-client-python/ARCHITECTURE.md +++ b/packages/http-client-python/ARCHITECTURE.md @@ -117,6 +117,27 @@ The emitter bridges TypeSpec and Python code generation. 5. Generate intermediate YAML representation 6. Invoke Python generator with YAML input +#### Browser runtime + +The playground loads the generator and all of its Python dependencies, including +pure-Python builds of PyYAML and MarkupSafe, from the emitter's published +`generator/dist/browser/manifest.json` and wheel assets using Pyodide's +`loadPackage`. Browser generation does not query PyPI, CFS, or the Pyodide package +CDN. Only the Pyodide core runtime and the emitter asset host must be reachable. + +`npm run build:playground` downloads the exact artifacts and SHA-256 hashes listed +in `eng/playground-requirements.txt` from `PIP_INDEX_URL` or `UV_DEFAULT_INDEX` +(default: the public `azure-sdk-for-python` feed). PyYAML and MarkupSafe are +built from their source distributions without their optional native extensions. +The build rejects native wheels and checks the dependency set with pip before +publishing it. The browser-only `platformdirs` pin avoids its unsupported +`os.geteuid` API. Update the requirements and hashes when generator dependencies +change. Ordinary `npm run build` skips browser-wheel preparation. + +Publish the browser manifest and wheels together with the emitter bundle under +the same package version. Previously published bundles do not pick up this fix +until a new version is uploaded and the playground's latest package index is updated. + ### 2. Generator (Python) **Location**: `packages/http-client-python/generator/pygen/` diff --git a/packages/http-client-python/emitter/src/emitter.ts b/packages/http-client-python/emitter/src/emitter.ts index 9af976be426..aac95f2dfdc 100644 --- a/packages/http-client-python/emitter/src/emitter.ts +++ b/packages/http-client-python/emitter/src/emitter.ts @@ -3,12 +3,7 @@ import type { EmitContext } from "@typespec/compiler"; import { emitFile, joinPaths, listServices, NoTarget } from "@typespec/compiler"; import pkgJson from "../../package.json" with { type: "json" }; import { emitCodeModel } from "./code-model.js"; -import { - BLOB_STORAGE_BASE_URL, - PACKAGE_NAME, - PYGEN_WHEEL_FILENAME, - PYODIDE_VERSION, -} from "./constants.js"; +import { BLOB_STORAGE_BASE_URL, PACKAGE_NAME, PYODIDE_VERSION } from "./constants.js"; import type { PythonEmitterOptions, PythonSdkContext } from "./lib.js"; import { reportDiagnostic } from "./lib.js"; import { runNodeEmit } from "./node-runner.js"; @@ -17,8 +12,21 @@ import { loadPyodide } from "./pyodide-loader.js"; import { getRootNamespace, md2Rst } from "./utils.js"; import { dumpCodeModelToYaml } from "./yaml-utils.js"; -function getBrowserPygenWheelUrl(): string { - return `${BLOB_STORAGE_BASE_URL}/${PACKAGE_NAME}/${pkgJson.version}/generator/dist/${PYGEN_WHEEL_FILENAME}`; +function getBrowserAssetUrl(filename: string): string { + return `${BLOB_STORAGE_BASE_URL}/${PACKAGE_NAME}/${pkgJson.version}/generator/dist/browser/${filename}`; +} + +function isBrowserWheelManifest(manifest: unknown): manifest is { wheels: string[] } { + return ( + typeof manifest === "object" && + manifest !== null && + "wheels" in manifest && + Array.isArray(manifest.wheels) && + manifest.wheels.length > 0 && + manifest.wheels.every( + (name: unknown) => typeof name === "string" && /^[\w.+-]+\.whl$/.test(name), + ) + ); } function addDefaultOptions(sdkContext: PythonSdkContext) { @@ -306,9 +314,24 @@ async function setupPyodideCallBrowser() { // use default MEMFS for browser, since NODEFS is not supported pyodide.FS.mkdirTree("/generator"); - await pyodide.loadPackage("micropip"); - const micropip = pyodide.pyimport("micropip"); - await micropip.install(getBrowserPygenWheelUrl()); + const response = await fetch(getBrowserAssetUrl("manifest.json")); + if (!response.ok) { + throw new Error(`Failed to load Python browser wheel manifest: HTTP ${response.status}`); + } + const manifest: unknown = await response.json(); + if (!isBrowserWheelManifest(manifest)) { + throw new Error("Invalid Python browser wheel manifest."); + } + await loadBrowserPackages(pyodide, manifest.wheels.map(getBrowserAssetUrl)); return pyodide; } + +async function loadBrowserPackages(pyodide: PyodideInterface, packages: string[]): Promise { + const errors: string[] = []; + // loadPackage can report failed downloads through its callback without rejecting. + await pyodide.loadPackage(packages, { errorCallback: (message) => errors.push(message) }); + if (errors.length > 0) { + throw new Error(`Failed to load Python browser packages: ${errors.join("\n")}`); + } +} diff --git a/packages/http-client-python/emitter/test/browser-pyodide.test.ts b/packages/http-client-python/emitter/test/browser-pyodide.test.ts index 09bad2b8471..fae10ce5a6d 100644 --- a/packages/http-client-python/emitter/test/browser-pyodide.test.ts +++ b/packages/http-client-python/emitter/test/browser-pyodide.test.ts @@ -1,5 +1,6 @@ +import { expectDiagnostics } from "@typespec/compiler/testing"; import { strictEqual } from "assert"; -import { afterEach, describe, it, vi } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; const loadPyodide = vi.hoisted(() => vi.fn()); @@ -8,6 +9,7 @@ vi.mock("../src/pyodide-loader.js", () => ({ loadPyodide })); describe("typespec-python: browser pyodide bootstrap", () => { afterEach(() => { delete (globalThis as any).window; + vi.unstubAllGlobals(); loadPyodide.mockReset(); vi.resetModules(); }); @@ -22,4 +24,140 @@ describe("typespec-python: browser pyodide bootstrap", () => { strictEqual(loadPyodide.mock.calls.length, 0); }); + + it("installs all published dependency wheels from emitter assets", async () => { + vi.stubGlobal("window", globalThis); + const wheels = [ + "pygen-0.1.0-py3-none-any.whl", + "pyyaml-6.0.3-py3-none-any.whl", + "markupsafe-2.1.5-py3-none-any.whl", + ]; + const fetch = vi.fn().mockResolvedValue({ + ok: true, + json: async () => ({ wheels }), + }); + vi.stubGlobal("fetch", fetch); + const pyodide = { + FS: { + mkdirTree: vi.fn(), + readdir: vi.fn().mockReturnValue([".", ".."]), + writeFile: vi.fn(), + }, + loadPackage: vi.fn(), + toPy: vi.fn(), + runPythonAsync: vi.fn(), + }; + loadPyodide.mockResolvedValue(pyodide); + const { EmitterTester } = await import("./test-host.js"); + const [, diagnostics] = await EmitterTester.compileAndDiagnose(` + import "@typespec/http"; + using TypeSpec.Http; + @service namespace Test; + @get op read(): string; + `); + + expectDiagnostics(diagnostics, []); + expect(fetch).toHaveBeenCalledWith( + expect.stringMatching(/\/generator\/dist\/browser\/manifest\.json$/), + ); + expect(pyodide.loadPackage).toHaveBeenCalledExactlyOnceWith( + wheels.map((name) => expect.stringMatching(new RegExp(`/browser/${name}$`))), + { errorCallback: expect.any(Function) }, + ); + expect(pyodide.runPythonAsync).toHaveBeenCalledOnce(); + }); + + it.each([ + { ok: false, status: 404, body: undefined, message: /HTTP 404/ }, + { ok: true, status: 200, body: null, message: /Invalid Python browser wheel/ }, + { ok: true, status: 200, body: {}, message: /Invalid Python browser wheel/ }, + { + ok: true, + status: 200, + body: { wheels: [] }, + message: /Invalid Python browser wheel/, + }, + { + ok: true, + status: 200, + body: { wheels: "not-an-array" }, + message: /Invalid Python browser wheel/, + }, + { + ok: true, + status: 200, + body: { wheels: [null] }, + message: /Invalid Python browser wheel/, + }, + { + ok: true, + status: 200, + body: { wheels: [42] }, + message: /Invalid Python browser wheel/, + }, + { + ok: true, + status: 200, + body: { wheels: ["../unexpected.whl"] }, + message: /Invalid Python browser wheel/, + }, + ])("reports unusable browser wheel manifests: $body", async ({ ok, status, body, message }) => { + vi.stubGlobal("window", globalThis); + vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok, status, json: async () => body })); + const loadPackage = vi.fn(); + loadPyodide.mockResolvedValue({ + FS: { mkdirTree: vi.fn() }, + loadPackage, + }); + const { EmitterTester } = await import("./test-host.js"); + const [, diagnostics] = await EmitterTester.compileAndDiagnose( + "@service namespace Test; op read(): string;", + ); + + expectDiagnostics(diagnostics, { + code: "@typespec/http-client-python/unknown-error", + message, + }); + expect(loadPackage).not.toHaveBeenCalled(); + }); + + it.each(["rejected", "reported"])( + "reports %s wheel loading failures and retries initialization on the next emit", + async (mode) => { + vi.stubGlobal("window", globalThis); + vi.stubGlobal( + "fetch", + vi.fn().mockResolvedValue({ + ok: true, + json: async () => ({ wheels: ["pygen-0.1.0-py3-none-any.whl"] }), + }), + ); + const loadPackage = vi.fn().mockImplementationOnce((_names, options) => { + if (mode === "rejected") return Promise.reject(new Error("Wheel download failed")); + options?.errorCallback("Wheel download failed"); + return Promise.resolve([]); + }); + loadPyodide.mockResolvedValue({ + FS: { + mkdirTree: vi.fn(), + readdir: vi.fn().mockReturnValue([".", ".."]), + writeFile: vi.fn(), + }, + loadPackage, + toPy: vi.fn(), + runPythonAsync: vi.fn(), + }); + const { EmitterTester } = await import("./test-host.js"); + const source = "@service namespace Test; op read(): string;"; + const [, failure] = await EmitterTester.compileAndDiagnose(source); + expectDiagnostics(failure, { + code: "@typespec/http-client-python/unknown-error", + message: /Wheel download failed/, + }); + const [, success] = await EmitterTester.compileAndDiagnose(source); + expectDiagnostics(success, []); + expect(loadPyodide).toHaveBeenCalledTimes(2); + expect(loadPackage).toHaveBeenCalledTimes(2); + }, + ); }); diff --git a/packages/http-client-python/eng/pipeline/publish.yml b/packages/http-client-python/eng/pipeline/publish.yml index 09b3aee24f1..6ea23cd309e 100644 --- a/packages/http-client-python/eng/pipeline/publish.yml +++ b/packages/http-client-python/eng/pipeline/publish.yml @@ -32,3 +32,4 @@ extends: EnableCadlRanchReport: false PythonVersion: "3.15" UploadPlaygroundBundle: true + PlaygroundBundleBuildScript: "build:playground" diff --git a/packages/http-client-python/eng/playground-requirements.txt b/packages/http-client-python/eng/playground-requirements.txt new file mode 100644 index 00000000000..188fb74e0dc --- /dev/null +++ b/packages/http-client-python/eng/playground-requirements.txt @@ -0,0 +1,13 @@ +black==26.3.1 --hash=sha256:2bd5aa94fc267d38bb21a70d7410a89f1a1d318841855f698746f8e7f51acd1b +click==8.5.1 --hash=sha256:8bb5376d2006950386f8c6427613334a2acadc5f559cb80e638d00b4df957e37 +docutils==0.23 --hash=sha256:25d013af9bf23bc1c7b2b093dff4208166c53a94786c9e447808335ef1185fea +Jinja2==3.1.6 --hash=sha256:85ece4451f492d0c13c5dd7c13a64681a86afae63a5f347908daf103ce6d2f67 +MarkupSafe==2.1.5 --hash=sha256:d283d37a890ba4c1ae73ffadf8046435c76e7bc2247bbb63c00bd1a709c6544b +mypy-extensions==1.2.0 --hash=sha256:993e7a20aa5671eb41a7132cf555af30062da99729601601b9a8382b11cae768 +packaging==26.3 --hash=sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c +pathspec==1.1.1 --hash=sha256:a00ce642f577bf7f473932318056212bc4f8bfdf53128c78bbd5af0b9b20b189 +platformdirs==4.3.8 --hash=sha256:ff7059bb7eb1179e2685604f4aaf157cfd9535242bd23742eadc3c13542139b4 +pytokens==0.4.1 --hash=sha256:26cef14744a8385f35d0e095dc8b3a7583f6c953c2e3d269c7f82484bf5ad2de +PyYAML==6.0.3 --hash=sha256:d76623373421df22fb4cf8817020cbb7ef15c725b9d5e45f17e189bfc384190f +setuptools==83.0.0 --hash=sha256:29b23c360f22f414dc7336bb39178cc7bcbf6021ed2733cde173f09dba19abb3 +tomli==2.0.1 --hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc diff --git a/packages/http-client-python/eng/scripts/setup/build.ts b/packages/http-client-python/eng/scripts/setup/build.ts index 71c258e68c6..ff46e1fc1e1 100644 --- a/packages/http-client-python/eng/scripts/setup/build.ts +++ b/packages/http-client-python/eng/scripts/setup/build.ts @@ -2,7 +2,7 @@ import { exec } from "child_process"; import { runPython3 } from "./run-python3.js"; async function main() { - await runPython3("./eng/scripts/setup/build_pygen_wheel.py"); + await runPython3("./eng/scripts/setup/build_pygen_wheel.py", ...process.argv.slice(2)); // remove the venv_build_wheel directory exec("rimraf ./venv_build_wheel", (error, stdout, _stderr) => { if (error) { diff --git a/packages/http-client-python/eng/scripts/setup/build_pygen_wheel.py b/packages/http-client-python/eng/scripts/setup/build_pygen_wheel.py index 40416a0991d..27a6a2dfde6 100644 --- a/packages/http-client-python/eng/scripts/setup/build_pygen_wheel.py +++ b/packages/http-client-python/eng/scripts/setup/build_pygen_wheel.py @@ -5,7 +5,16 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- +import argparse +import json +import os +from pathlib import Path, PurePosixPath +import shutil +import subprocess import sys +import tarfile +import tempfile +import zipfile if not sys.version_info >= (3, 10, 0): raise Exception("Autorest for Python extension requires Python 3.10 at least") @@ -13,29 +22,188 @@ try: from package_manager import detect_package_manager, PackageManagerNotFoundError - detect_package_manager() # Just check if we have a package manager + detect_package_manager() except (ImportError, ModuleNotFoundError, PackageManagerNotFoundError): raise Exception("Your Python installation doesn't have a suitable package manager (pip or uv) available") - -# Now we have a package manager (pip or uv) and Py >= 3.10, go to work - -from pathlib import Path - from venvtools import python_run from package_manager import install_packages, create_venv_with_package_manager -# eng/scripts/setup/build_pygen_wheel.py -> need to go up 4 levels to get to package root _ROOT_DIR = Path(__file__).parent.parent.parent.parent -def main(): - venv_path = _ROOT_DIR / "venv_build_wheel" - venv_context = create_venv_with_package_manager(venv_path) +def load_pyodide_lock(): + """Read the lock from the pinned Pyodide npm package.""" + expected = json.loads((_ROOT_DIR / "package.json").read_text())["dependencies"]["pyodide"] + for directory in (_ROOT_DIR, *_ROOT_DIR.parents): + package_dir = directory / "node_modules" / "pyodide" + if (package_dir / "package.json").is_file(): + break + else: + raise RuntimeError(f"pyodide npm package not found from {_ROOT_DIR}. Run npm install before building.") + version = json.loads((package_dir / "package.json").read_text())["version"] + if version != expected: + raise RuntimeError(f"Installed pyodide {version} at {package_dir} does not match pinned version {expected}.") + return json.loads((package_dir / "pyodide-lock.json").read_text()) + + +def download_browser_requirements(context, destination, lock): + env = os.environ.copy() + env["PIP_INDEX_URL"] = ( + env.get("PIP_INDEX_URL") + or env.get("UV_DEFAULT_INDEX") + or "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/" + ) + subprocess.run( + [ + context.env_exe, + "-m", + "pip", + "download", + "--require-hashes", + "--no-deps", + "--no-build-isolation", + "--only-binary=:all:", + "--no-binary=pyyaml,markupsafe", + "--platform=any", + "--implementation=cp", + "--abi=none", + f"--python-version={lock['info']['python']}", + "--dest", + str(destination), + "-r", + str(_ROOT_DIR / "eng" / "playground-requirements.txt"), + ], + env=env, + check=True, + ) + + +def extract_source(archive_path, destination): + """Extract only regular files and directories from a hash-verified source archive.""" + with tarfile.open(archive_path, "r:gz") as archive: + for member in archive: + path = PurePosixPath(member.name) + if ( + path.is_absolute() + or ".." in path.parts + or "\\" in member.name + or any(":" in part for part in path.parts) + or not (member.isfile() or member.isdir()) + ): + raise RuntimeError(f"Unsafe source archive entry: {member.name}") + target = destination.joinpath(*path.parts) + if member.isdir(): + target.mkdir(parents=True, exist_ok=True) + else: + target.parent.mkdir(parents=True, exist_ok=True) + with archive.extractfile(member) as source, target.open("wb") as output: + shutil.copyfileobj(source, output) + + +def build_pure_sources(context, downloads, browser, workspace): + for package in ("pyyaml", "markupsafe"): + archives = [p for p in downloads.glob("*.tar.gz") if p.name.lower().startswith(package + "-")] + if len(archives) != 1: + raise RuntimeError(f"Expected exactly one {package} source archive, found {len(archives)}.") + source_root = workspace / package + source_root.mkdir() + extract_source(archives[0], source_root) + roots = list(source_root.iterdir()) + if len(roots) != 1 or not roots[0].is_dir(): + raise RuntimeError(f"Unexpected {package} source archive layout.") + source = roots[0] + env = os.environ.copy() + if package == "pyyaml": + env["PYYAML_FORCE_LIBYAML"] = "0" + else: + # Use upstream setup.cfg metadata without its optional native extension. + (source / "setup.py").write_text("from setuptools import setup\nsetup()\n") + subprocess.run( + [ + context.env_exe, + "-m", + "build", + "--wheel", + "--no-isolation", + "--skip-dependency-check", + "--outdir", + str(browser), + ], + cwd=source, + env=env, + check=True, + ) + + +def check_pure_wheels(browser): + for wheel in browser.glob("*.whl"): + if not wheel.name.endswith("-none-any.whl"): + raise RuntimeError(f"Unsupported browser wheel: {wheel.name}") + with zipfile.ZipFile(wheel) as archive: + if any(name.endswith((".so", ".pyd", ".dll", ".wasm")) for name in archive.namelist()): + raise RuntimeError(f"Unsupported browser wheel contents: {wheel.name}") + + +def build_browser_wheels(context): + dist = _ROOT_DIR / "generator" / "dist" + wheel = next(dist.glob("pygen-*.whl"), None) + if wheel is None: + raise RuntimeError(f"Generator wheel not found in {dist}. Build the generator before preparing browser wheels.") + lock = load_pyodide_lock() + browser = dist / "browser" + with tempfile.TemporaryDirectory(dir=dist) as directory: + workspace = Path(directory) + downloads = workspace / "downloads" + downloads.mkdir() + output = workspace / "browser" + output.mkdir() + download_browser_requirements(context, downloads, lock) + for source in downloads.glob("*.whl"): + shutil.copyfile(source, output / source.name) + build_pure_sources(context, downloads, output, workspace) + shutil.copyfile(wheel, output / wheel.name) + check_pure_wheels(output) + subprocess.run( + [ + context.env_exe, + "-m", + "pip", + "install", + "--dry-run", + "--ignore-installed", + "--no-index", + "--only-binary=:all:", + "--find-links", + str(output), + "--platform=any", + "--implementation=cp", + "--abi=none", + f"--python-version={lock['info']['python']}", + str(output / wheel.name), + ], + check=True, + ) + names = sorted(p.name for p in output.glob("*.whl")) + (output / "manifest.json").write_text(json.dumps({"wheels": names}, indent=2) + "\n") + if browser.exists(): + shutil.rmtree(browser) + shutil.move(str(output), browser) + - install_packages(["build"], venv_context) +def main(playground=False): + venv_context = create_venv_with_package_manager(_ROOT_DIR / "venv_build_wheel") + packages = ["build", "wheel", "setuptools==83.0.0", "pip==26.2.2"] if playground else ["build"] + install_packages(packages, venv_context) python_run(venv_context, "build", ["--wheel"], additional_dir="generator") + if playground: + build_browser_wheels(venv_context) if __name__ == "__main__": - main() + parser = argparse.ArgumentParser() + parser.add_argument( + "--playground", action="store_true", help="Prepare browser dependency wheels for the playground" + ) + args = parser.parse_args() + main(playground=args.playground) diff --git a/packages/http-client-python/generator/setup.py b/packages/http-client-python/generator/setup.py index 73da843c888..fc090efd205 100644 --- a/packages/http-client-python/generator/setup.py +++ b/packages/http-client-python/generator/setup.py @@ -51,7 +51,7 @@ "black==26.3.1", "docutils>=0.20.1", "Jinja2==3.1.6", - "PyYAML==6.0.1", + "PyYAML==6.0.3", "tomli==2.0.1", "setuptools==83.0.0", ], diff --git a/packages/http-client-python/package.json b/packages/http-client-python/package.json index 3fc20d26a49..d4ba83f2902 100644 --- a/packages/http-client-python/package.json +++ b/packages/http-client-python/package.json @@ -35,6 +35,7 @@ "scripts": { "clean": "rimraf ./dist ./temp ./venv ./tests/generated", "build": "tsc -p ./emitter/tsconfig.build.json && tsx ./eng/scripts/setup/build.ts", + "build:playground": "npm run build -- --playground", "watch": "tsc -p ./emitter/tsconfig.build.json --watch", "install": "tsx ./eng/scripts/setup/install.ts", "prepare": "tsx ./eng/scripts/setup/prepare.ts", @@ -86,6 +87,14 @@ { "path": "generator/dist/pygen-*.whl", "contentType": "application/zip" + }, + { + "path": "generator/dist/browser/*.whl", + "contentType": "application/zip" + }, + { + "path": "generator/dist/browser/manifest.json", + "contentType": "application/json" } ], "bundlePeerDependencies": [ diff --git a/packages/http-client-python/tests/unit/test_browser_feed.py b/packages/http-client-python/tests/unit/test_browser_feed.py new file mode 100644 index 00000000000..d6ed3618537 --- /dev/null +++ b/packages/http-client-python/tests/unit/test_browser_feed.py @@ -0,0 +1,122 @@ +import importlib.util +import io +import json +from pathlib import Path +import subprocess +import tarfile +from types import SimpleNamespace +import sys +import tempfile +import unittest +from unittest.mock import patch +import zipfile + +SETUP = Path(__file__).resolve().parents[2] / "eng" / "scripts" / "setup" +sys.path.insert(0, str(SETUP)) +spec = importlib.util.spec_from_file_location("build_pygen_wheel_feed", SETUP / "build_pygen_wheel.py") +builder = importlib.util.module_from_spec(spec) +spec.loader.exec_module(builder) +sys.path.remove(str(SETUP)) + + +class BrowserFeedTests(unittest.TestCase): + def test_reads_pinned_pyodide_lock_from_hoisted_npm_package(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) / "packages" / "emitter" + root.mkdir(parents=True) + (root / "package.json").write_text(json.dumps({"dependencies": {"pyodide": "0.26.2"}})) + package = Path(directory) / "node_modules" / "pyodide" + package.mkdir(parents=True) + (package / "package.json").write_text(json.dumps({"version": "0.26.2"})) + (package / "pyodide-lock.json").write_text(json.dumps({"info": {"python": "3.12.1"}})) + with patch.object(builder, "_ROOT_DIR", root): + self.assertEqual(builder.load_pyodide_lock()["info"]["python"], "3.12.1") + (package / "package.json").write_text(json.dumps({"version": "0.27.0"})) + with self.assertRaisesRegex(RuntimeError, "does not match pinned version"): + builder.load_pyodide_lock() + + def test_downloads_only_hash_pinned_packages_from_configured_feed(self): + context = SimpleNamespace(env_exe="python") + with tempfile.TemporaryDirectory() as directory: + with patch.object(builder.subprocess, "run") as run: + builder.download_browser_requirements(context, Path(directory), {"info": {"python": "3.12.1"}}) + command = run.call_args.args[0] + self.assertIn("--require-hashes", command) + self.assertIn("--no-deps", command) + self.assertIn("--no-binary=pyyaml,markupsafe", command) + self.assertNotIn("cdn.jsdelivr.net", " ".join(command)) + self.assertTrue(run.call_args.kwargs["check"]) + + def test_rejects_non_universal_wheels(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "unsafe-1.0-cp312-cp312-linux_x86_64.whl" + path.touch() + with self.assertRaisesRegex(RuntimeError, "Unsupported browser wheel"): + builder.check_pure_wheels(Path(directory)) + + def test_rejects_native_content_in_a_pure_wheel(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "unsafe-1.0-py3-none-any.whl" + with zipfile.ZipFile(path, "w") as archive: + archive.writestr("unsafe/native.so", b"native") + with self.assertRaisesRegex(RuntimeError, "Unsupported browser wheel contents"): + builder.check_pure_wheels(Path(directory)) + + def test_download_failure_preserves_existing_bundle(self): + context = SimpleNamespace(env_exe="python") + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + dist = root / "generator" / "dist" + browser = dist / "browser" + browser.mkdir(parents=True) + (dist / "pygen-0.1.0-py3-none-any.whl").touch() + (browser / "manifest.json").write_text("previous") + with ( + patch.object(builder, "_ROOT_DIR", root), + patch.object(builder, "load_pyodide_lock", return_value={"info": {"python": "3.12.1"}}), + patch.object( + builder, "download_browser_requirements", side_effect=subprocess.CalledProcessError(1, "pip") + ), + ): + with self.assertRaises(subprocess.CalledProcessError): + builder.build_browser_wheels(context) + self.assertEqual((browser / "manifest.json").read_text(), "previous") + + def test_rejects_unsafe_source_archive_paths(self): + for name in ("../escape", "package/../../escape", "package\\..\\escape", "C:/escape"): + with self.subTest(name=name), tempfile.TemporaryDirectory() as directory: + root = Path(directory) + archive = root / "package.tar.gz" + with tarfile.open(archive, "w:gz") as tar: + member = tarfile.TarInfo(name) + member.size = 1 + tar.addfile(member, io.BytesIO(b"x")) + with self.assertRaisesRegex(RuntimeError, "Unsafe source archive"): + builder.extract_source(archive, root / "output") + + def test_playground_build_installs_wheel_build_tools(self): + context = SimpleNamespace(env_exe="python") + with ( + patch.object(builder, "create_venv_with_package_manager", return_value=context), + patch.object(builder, "install_packages") as install, + patch.object(builder, "python_run"), + patch.object(builder, "build_browser_wheels") as browser, + ): + builder.main(playground=True) + install.assert_called_once_with(["build", "wheel", "setuptools==83.0.0", "pip==26.2.2"], context) + browser.assert_called_once_with(context) + + def test_regular_build_does_not_download_browser_dependencies(self): + context = SimpleNamespace(env_exe="python") + with ( + patch.object(builder, "create_venv_with_package_manager", return_value=context), + patch.object(builder, "install_packages"), + patch.object(builder, "python_run"), + patch.object(builder, "build_browser_wheels") as browser, + ): + builder.main() + browser.assert_not_called() + + +if __name__ == "__main__": + unittest.main()