|
| 1 | +"""Build the MNE wheel for the JupyterLite browser kernel. |
| 2 | +
|
| 3 | +Run this once before building the docs, either in CI or locally:: |
| 4 | +
|
| 5 | + python doc/sphinxext/build_lite_wheel.py |
| 6 | +
|
| 7 | +The wheel is written to ``doc/pypi``, where the jupyterlite-pyodide-kernel |
| 8 | +PipliteAddon discovers, copies and indexes it (adding it to ``pipliteUrls`` in |
| 9 | +``jupyter-lite.json``), so the browser kernel installs the MNE the surrounding |
| 10 | +pages are built from rather than the last release on PyPI. That is the |
| 11 | +development version on ``main`` and that release's code on a ``maint/*`` |
| 12 | +branch, since the docs build from whichever branch it is running on. See |
| 13 | +https://jupyterlite.readthedocs.io/en/latest/howto/pyodide/wheels.html |
| 14 | +
|
| 15 | +Both functions are importable, so a docs build can reuse a wheel that is already |
| 16 | +present rather than building one on every invocation:: |
| 17 | +
|
| 18 | + from build_lite_wheel import build_wheel, find_wheels |
| 19 | +
|
| 20 | + wheels = find_wheels() or build_wheel() |
| 21 | +""" |
| 22 | + |
| 23 | +# Authors: The MNE-Python contributors. |
| 24 | +# License: BSD-3-Clause |
| 25 | +# Copyright the MNE-Python contributors. |
| 26 | + |
| 27 | +import json |
| 28 | +import shutil |
| 29 | +import subprocess |
| 30 | +import sys |
| 31 | +import urllib.request |
| 32 | +from pathlib import Path |
| 33 | + |
| 34 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 35 | +PYPI_WHEELS_DIR = REPO_ROOT / "doc" / "pypi" |
| 36 | + |
| 37 | + |
| 38 | +def find_wheels(): |
| 39 | + """Return the MNE wheels already present in ``doc/pypi``. |
| 40 | +
|
| 41 | + Returns |
| 42 | + ------- |
| 43 | + wheels : list of Path |
| 44 | + Paths of the MNE wheels found, empty if there are none. |
| 45 | + """ |
| 46 | + return sorted(PYPI_WHEELS_DIR.glob("mne-*.whl")) |
| 47 | + |
| 48 | + |
| 49 | +def _latest_pypi_version(): |
| 50 | + """Return the newest MNE version on PyPI, or None if it cannot be reached. |
| 51 | +
|
| 52 | + Returns |
| 53 | + ------- |
| 54 | + version : str | None |
| 55 | + The version string, or None if PyPI could not be queried. |
| 56 | + """ |
| 57 | + # Broad on purpose: this only ever runs while raising, so a network problem |
| 58 | + # here must not replace the real error with a less useful one. |
| 59 | + try: |
| 60 | + url = "https://pypi.org/pypi/mne/json" |
| 61 | + with urllib.request.urlopen(url, timeout=10) as response: |
| 62 | + return json.load(response)["info"]["version"] |
| 63 | + except Exception: |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +def build_wheel(): |
| 68 | + """Build the MNE wheel for the browser kernel into ``doc/pypi``. |
| 69 | +
|
| 70 | + Returns |
| 71 | + ------- |
| 72 | + wheels : list of Path |
| 73 | + Paths of the MNE wheels that were built. |
| 74 | + """ |
| 75 | + # This directory is the piplite index, so it should hold the wheel this |
| 76 | + # build produced and nothing else, including anything left behind by an |
| 77 | + # earlier build or a manual pip wheel. The version is left to hatch-vcs: |
| 78 | + # piplite serves this index exclusively rather than merging it with PyPI |
| 79 | + # (_query_package returns as soon as the package is found here), so a |
| 80 | + # development version has nothing to lose a resolution against. |
| 81 | + shutil.rmtree(PYPI_WHEELS_DIR, ignore_errors=True) |
| 82 | + PYPI_WHEELS_DIR.mkdir(parents=True, exist_ok=True) |
| 83 | + |
| 84 | + # The wheel is built from pyproject.toml as it stands: Pyodide 314 ships |
| 85 | + # matplotlib 3.10.8, scipy 1.18.0 and numpy 2.4.3, all of which satisfy the |
| 86 | + # minimums MNE declares, so none of them needs relaxing for the browser. |
| 87 | + # NB: build isolation is left ON (the default). MNE uses the hatchling build |
| 88 | + # backend, so pip must create an isolated build env to install |
| 89 | + # hatchling/hatch-vcs; --no-build-isolation fails with "Cannot import |
| 90 | + # 'hatchling.build'" on CI, where those build deps are not in the base |
| 91 | + # environment. |
| 92 | + subprocess.run( |
| 93 | + [ |
| 94 | + sys.executable, |
| 95 | + "-m", |
| 96 | + "pip", |
| 97 | + "wheel", |
| 98 | + REPO_ROOT, |
| 99 | + "--no-deps", |
| 100 | + "-w", |
| 101 | + PYPI_WHEELS_DIR, |
| 102 | + ], |
| 103 | + check=True, |
| 104 | + ) |
| 105 | + |
| 106 | + # Fail loudly rather than silently letting the browser kernel fall back to |
| 107 | + # the released MNE from PyPI. |
| 108 | + wheels = find_wheels() |
| 109 | + if not wheels: |
| 110 | + latest = _latest_pypi_version() |
| 111 | + fallback = f"MNE {latest}" if latest else "the latest MNE release" |
| 112 | + raise RuntimeError( |
| 113 | + f"JupyterLite: no MNE wheel was built into {PYPI_WHEELS_DIR}; the " |
| 114 | + f"browser kernel would fall back to {fallback} from PyPI. Check the " |
| 115 | + "'pip wheel' output above." |
| 116 | + ) |
| 117 | + return wheels |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + # Reuse a wheel that is already there, so repeat `make html` runs do not |
| 122 | + # rebuild it. Remove doc/pypi (or `make clean`) to force a fresh one. |
| 123 | + existing = find_wheels() |
| 124 | + wheels = ", ".join(str(wheel) for wheel in (existing or build_wheel())) |
| 125 | + verb = "Reusing" if existing else "Built" |
| 126 | + print(f"[JupyterLite] {verb} MNE wheel(s) for the browser kernel: {wheels}") |
0 commit comments