-
-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathnoxfile.py
More file actions
211 lines (183 loc) · 7.78 KB
/
Copy pathnoxfile.py
File metadata and controls
211 lines (183 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "nox",
# ]
# ///
import json
import os
import re
import sys
import urllib.request
from pathlib import Path
import nox
import tomllib
PYODIDE_VERSION = os.getenv("PYODIDE_VERSION", "0.29.0")
GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS")
GITHUB_ENV = os.getenv("GITHUB_ENV")
MSRV = tomllib.loads(Path("Cargo.toml").read_text())["package"]["rust-version"]
def append_to_github_env(name: str, value: str):
if not GITHUB_ACTIONS or not GITHUB_ENV:
return
with open(GITHUB_ENV, "a") as f:
f.write(f"{name}={value}\n")
def get_latest_pyo3_version():
url = "https://crates.io/api/v1/crates/pyo3"
with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "maturin-nox-update"})) as response:
data = json.loads(response.read().decode())
return data["crate"]["max_stable_version"]
@nox.session(name="update-pyo3", python=False)
def update_pyo3(session: nox.Session):
latest_version = get_latest_pyo3_version()
session.log(f"Latest pyo3 version: {latest_version}")
crates = ["pyo3", "pyo3-ffi", "pyo3-build-config"]
# Update test crates and root Cargo.toml
cargo_tomls = [p for p in Path(".").glob("**/Cargo.toml") if "pyo3-no-extension-module" not in str(p)]
# Update templates
templates = list(Path("src/templates").glob("*.j2"))
for path in cargo_tomls + templates:
content = path.read_text()
changed = False
for crate in crates:
# Replace crate = "version"
new_content = re.sub(
rf'^(\s*{crate}\s*=\s*)"[0-9.]+"',
rf'\1"{latest_version}"',
content,
flags=re.MULTILINE,
)
if new_content != content:
content = new_content
changed = True
# Replace crate = { version = "version"
new_content = re.sub(
rf'^(\s*{crate}\s*=\s*\{{.*?version\s*=\s*)"[0-9.]+"',
rf'\1"{latest_version}"',
content,
flags=re.MULTILINE,
)
if new_content != content:
content = new_content
changed = True
if changed:
session.log(f"Updating {path}")
path.write_text(content)
test_crate_dir = Path("./test-crates").resolve()
crates_to_update = ["pyo3", "pyo3-ffi", "python3-dll-a"]
for root, _, files in os.walk(test_crate_dir):
if "pyo3-no-extension-module" in root:
continue
if "Cargo.lock" in files:
cargo_lock_path = Path(root) / "Cargo.lock"
with open(cargo_lock_path, "r") as lock_file:
content = lock_file.read()
for crate in crates_to_update:
if f'name = "{crate}"' in content:
with session.chdir(root):
session.run("cargo", f"+{MSRV}", "update", "-p", crate, external=True)
def _resolve_pyodide_platform_inputs(info: dict) -> dict:
"""Map a `pyodide-lock.json` `info` block to the env vars and expected
wheel platform tag that maturin should produce when targeting that
Pyodide release.
Mirrors the cascade in `src/target/platform_tag.rs::emscripten_platform_tag`;
keep the two in sync.
Pyodide encodes platform metadata across (overlapping) fields:
* `info.platform` is `emscripten_<X>_<Y>_<Z>` and contains the emcc
version that built the runtime (used by `setup-emsdk`).
* `info.abi_version` (Pyodide >= 0.28) is `<year>_<patch>` and drives the
PEP 783 `pyemscripten_<year>_<patch>_wasm32` tag through maturin's
historical `PYODIDE_ABI_VERSION` env var.
* A future `info.pyemscripten_platform_version` drives
the PEP 783 `pyemscripten_<year>_<patch>_wasm32` wheel tag.
See https://peps.python.org/pep-0783/.
"""
platform = info.get("platform", "")
if platform.startswith("emscripten_"):
emscripten_version = platform.removeprefix("emscripten_").replace("_", ".")
else:
emscripten_version = info.get("emscripten_version", "")
pyemscripten = info.get("pyemscripten_platform_version", "")
abi_version = info.get("abi_version", "")
# Only export `EMSCRIPTEN_VERSION` when we actually resolved one — an
# empty value would clobber a previously-set `EMSCRIPTEN_VERSION` in
# `$GITHUB_ENV` and break `setup-emsdk`.
env: dict[str, str] = {}
if emscripten_version:
env["EMSCRIPTEN_VERSION"] = emscripten_version
if pyemscripten:
env["PYEMSCRIPTEN_PLATFORM_VERSION"] = pyemscripten
env["EXPECTED_PLATFORM_TAG"] = f"pyemscripten_{pyemscripten}_wasm32"
elif abi_version:
env["PYODIDE_ABI_VERSION"] = abi_version
env["EXPECTED_PLATFORM_TAG"] = f"pyemscripten_{abi_version}_wasm32"
elif emscripten_version:
legacy = emscripten_version.replace(".", "_").replace("-", "_")
env["EXPECTED_PLATFORM_TAG"] = f"emscripten_{legacy}_wasm32"
return env
@nox.session(name="setup-pyodide", python=False)
def setup_pyodide(session: nox.Session):
tests_dir = Path("./tests").resolve()
with session.chdir(tests_dir):
session.run(
"npm",
"i",
"--no-save",
f"pyodide@{PYODIDE_VERSION}",
"prettier",
external=True,
)
with session.chdir(tests_dir / "node_modules" / "pyodide"):
# Pyodide ships its Emscripten output as a single very long line
# named `pyodide.asm.js` (Pyodide <= 0.29) or `pyodide.asm.mjs`
# (Pyodide >= 314.0.0a1). Prettifying it makes Node-side errors
# readable. Run prettier on whichever of the two exists.
asm_file = next(Path(".").glob("pyodide.asm.*js"), None)
if asm_file is not None:
session.run(
"node",
"../prettier/bin/prettier.cjs",
"-w",
asm_file.name,
external=True,
)
with open("pyodide-lock.json") as f:
info = json.load(f)["info"]
for name, value in _resolve_pyodide_platform_inputs(info).items():
session.log(f"Pyodide {PYODIDE_VERSION}: {name}={value}")
append_to_github_env(name, value)
@nox.session(name="test-emscripten", python=False)
def test_emscripten(session: nox.Session):
tests_dir = Path("./tests").resolve()
expected_tag = os.getenv("EXPECTED_PLATFORM_TAG")
test_crates = [
"test-crates/pyo3-pure",
"test-crates/pyo3-mixed",
]
for crate in test_crates:
crate = Path(crate).resolve()
ver = sys.version_info
session.run("cargo", "build", external=True)
session.run(
tests_dir.parent / "target" / "debug" / "maturin",
"build",
"-m",
str(crate / "Cargo.toml"),
"--target",
"wasm32-unknown-emscripten",
"-i",
f"python{ver.major}.{ver.minor}",
env={"RUSTUP_TOOLCHAIN": "nightly"},
external=True,
)
if expected_tag:
wheels_dir = crate / "target" / "wheels"
wheels = sorted(wheels_dir.glob("*.whl"))
if not wheels:
session.error(f"No wheel produced in {wheels_dir}")
mismatched = [w for w in wheels if expected_tag not in w.name]
if mismatched:
names = ", ".join(w.name for w in mismatched)
session.error(f"Expected platform tag {expected_tag} in wheel name(s): {names}")
session.log(f"Verified {len(wheels)} wheel(s) carry platform tag {expected_tag}")
with session.chdir(tests_dir):
session.run("node", "emscripten_runner.js", str(crate), external=True)