Skip to content

Commit e4f8d7d

Browse files
alexeaglefhanau
authored andcommitted
feat(bazel): migrate pyodide to bzlmod
1 parent de01549 commit e4f8d7d

File tree

5 files changed

+111
-37
lines changed

5 files changed

+111
-37
lines changed

WORKSPACE

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
11
workspace(name = "workerd")
2-
3-
load("//build/deps:dep_pyodide.bzl", "dep_pyodide")
4-
5-
dep_pyodide()

build/deps/dep_pyodide.bzl

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,6 @@ def _capnp_bundle(id = None, integrity = None, **_kwds):
5353
url = PYODIDE_CAPN_BIN + "pyodide_%s.capnp.bin" % id,
5454
)
5555

56-
def _snapshot_http_file(bundle_name, folder, snapshot, integrity, hash, r2_base = PYODIDE_CAPN_BIN):
57-
if not snapshot:
58-
return
59-
if not integrity:
60-
fail("Snapshot %s from bundle %s has missing integrity" % (snapshot, bundle_name))
61-
if folder == "baseline-snapshot/":
62-
key = hash
63-
else:
64-
key = snapshot
65-
http_file(
66-
name = "pyodide-snapshot-" + snapshot,
67-
integrity = integrity,
68-
url = r2_base + folder + key,
69-
)
70-
71-
def _snapshot_http_files(
72-
name,
73-
baseline_snapshot = None,
74-
baseline_snapshot_hash = None,
75-
baseline_snapshot_integrity = None,
76-
numpy_snapshot = None,
77-
numpy_snapshot_integrity = None,
78-
fastapi_snapshot = None,
79-
fastapi_snapshot_integrity = None,
80-
dedicated_fastapi_snapshot = None,
81-
dedicated_fastapi_snapshot_integrity = None,
82-
**_kwds):
83-
_snapshot_http_file(name, "baseline-snapshot/", baseline_snapshot, baseline_snapshot_integrity, baseline_snapshot_hash)
84-
_snapshot_http_file(name, "test-snapshot/", numpy_snapshot, numpy_snapshot_integrity, None)
85-
_snapshot_http_file(name, "test-snapshot/", fastapi_snapshot, fastapi_snapshot_integrity, None)
86-
_snapshot_http_file(name, "", dedicated_fastapi_snapshot, dedicated_fastapi_snapshot_integrity, None, VENDOR_R2)
87-
8856
def dep_pyodide():
8957
for info in PYODIDE_VERSIONS:
9058
_pyodide_core(**info)
@@ -97,5 +65,50 @@ def dep_pyodide():
9765
for info in PYTHON_LOCKFILES:
9866
_pyodide_packages(**info)
9967

68+
# Accumulator to de-duplicate generated http_file rules.
69+
gen_http_file = {}
70+
71+
def _snapshot_http_files(
72+
name,
73+
baseline_snapshot = None,
74+
baseline_snapshot_hash = None,
75+
baseline_snapshot_integrity = None,
76+
numpy_snapshot = None,
77+
numpy_snapshot_integrity = None,
78+
fastapi_snapshot = None,
79+
fastapi_snapshot_integrity = None,
80+
dedicated_fastapi_snapshot = None,
81+
dedicated_fastapi_snapshot_integrity = None,
82+
**_kwds):
83+
def _snapshot_http_file(bundle_name, folder, snapshot, integrity, hash, r2_base = PYODIDE_CAPN_BIN):
84+
if not snapshot:
85+
return
86+
if not integrity:
87+
fail("Snapshot %s from bundle %s has missing integrity" % (snapshot, bundle_name))
88+
if folder == "baseline-snapshot/":
89+
key = hash
90+
else:
91+
key = snapshot
92+
gen_http_file[snapshot] = struct(
93+
integrity = integrity,
94+
url = r2_base + folder + key,
95+
)
96+
97+
_snapshot_http_file(name, "baseline-snapshot/", baseline_snapshot, baseline_snapshot_integrity, baseline_snapshot_hash)
98+
_snapshot_http_file(name, "test-snapshot/", numpy_snapshot, numpy_snapshot_integrity, None)
99+
_snapshot_http_file(name, "test-snapshot/", fastapi_snapshot, fastapi_snapshot_integrity, None)
100+
_snapshot_http_file(name, "", dedicated_fastapi_snapshot, dedicated_fastapi_snapshot_integrity, None, VENDOR_R2)
101+
100102
for ver in BUNDLE_VERSION_INFO.values():
101103
_snapshot_http_files(**ver)
104+
for snapshot, info in gen_http_file.items():
105+
http_file(
106+
name = "pyodide-snapshot-" + snapshot,
107+
integrity = info.integrity,
108+
url = info.url,
109+
)
110+
111+
def _impl(module_ctx):
112+
dep_pyodide()
113+
114+
pyodide = module_extension(implementation = _impl)

build/deps/python.MODULE.bazel

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,57 @@ pip.parse(
1818
requirements_lock = "//build/deps:requirements.txt",
1919
)
2020
use_repo(pip, "py_deps", "v8_python_deps")
21+
22+
pyodide = use_extension("//build/deps:dep_pyodide.bzl", "pyodide")
23+
24+
[
25+
use_repo(
26+
pyodide,
27+
repo.format(version = version),
28+
)
29+
for repo in [
30+
"beautifulsoup4_src_{version}",
31+
"fastapi_src_{version}",
32+
"pyodide-{version}",
33+
"python-workers-runtime-sdk_src_{version}",
34+
"shapely_src_{version}",
35+
]
36+
for version in [
37+
"0.26.0a2",
38+
"0.28.2",
39+
"development",
40+
]
41+
# Exclusions: some packages are absent in some versions
42+
if repo.format(version = version) not in [
43+
"pyodide-development",
44+
"shapely_src_0.26.0a2",
45+
]
46+
]
47+
48+
[
49+
use_repo(
50+
pyodide,
51+
repo.format(version = version),
52+
)
53+
for repo in [
54+
"all_pyodide_wheels_{version}",
55+
"pyodide-lock_{version}.json",
56+
]
57+
for version in [
58+
"20240829.4",
59+
"20250808",
60+
]
61+
]
62+
63+
use_repo(
64+
pyodide,
65+
"pyodide-snapshot-baseline-4569679fb.bin",
66+
"pyodide-snapshot-baseline-61eedf943.bin",
67+
"pyodide-snapshot-ew-py-package-snapshot_fastapi-v2.bin",
68+
"pyodide-snapshot-ew-py-package-snapshot_numpy-v2.bin",
69+
"pyodide-snapshot-package_snapshot_fastapi-a6ccb56fe.bin",
70+
"pyodide-snapshot-package_snapshot_numpy-60c9cb28e.bin",
71+
"pyodide-snapshot-snapshot_a6b652a95810783f5078b9a5dbd4a07c30718acb4ff724e82c25db7353dd7f2d.bin",
72+
"pyodide_0.26.0a2_2024-03-01_75.capnp.bin",
73+
"pyodide_0.28.2_2025-01-16_6.capnp.bin",
74+
)

build/is_pyodide_bzlmod.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# workerd uses bzlmod for pyodide (and everything else)
2+
is_pyodide_bzlmod = True

src/workerd/server/tests/python/py_wd_test.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@//:build/is_pyodide_bzlmod.bzl", "is_pyodide_bzlmod")
12
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
23
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
34
load("//:build/python_metadata.bzl", "BUNDLE_VERSION_INFO")
@@ -32,7 +33,15 @@ def _py_wd_test_helper(
3233

3334
pkg_tag = BUNDLE_VERSION_INFO[python_flag]["packages"]
3435
data = data + ["@all_pyodide_wheels_%s//:whls" % pkg_tag]
35-
args = args + ["--pyodide-package-disk-cache-dir", "../all_pyodide_wheels_%s" % pkg_tag]
36+
args = args + ["--pyodide-package-disk-cache-dir"]
37+
38+
# TODO(cleanup): We support both mangled and non-mangled wheels paths for now, clean up once
39+
# downstream repo fully uses bzlmod
40+
if is_pyodide_bzlmod:
41+
# +pyodide+ is a bzlmod canonical repository name
42+
args.append("../+pyodide+all_pyodide_wheels_%s" % pkg_tag)
43+
else:
44+
args.append("../all_pyodide_wheels_%s" % pkg_tag)
3645

3746
load_snapshot = None
3847
pyodide_version = BUNDLE_VERSION_INFO[python_flag]["real_pyodide_version"]

0 commit comments

Comments
 (0)