Skip to content

Commit

Permalink
Add Python development compatibility flag for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Aug 20, 2024
1 parent a47804f commit daa0378
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/workerd/io/compatibility-date.c++
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,11 @@ kj::Maybe<PythonSnapshotRelease::Reader> getPythonSnapshotRelease(
return result;
}

kj::String getPythonBundleName(PythonSnapshotRelease::Reader pyodideRelease) {
if (pyodideRelease.getPyodide() == "dev") {
return kj::str("dev");
}
return kj::str(pyodideRelease.getPyodide(), "_", pyodideRelease.getPyodideRevision(), "_", pyodideRelease.getBackport());
}

} // namespace workerd
10 changes: 9 additions & 1 deletion src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,20 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
pythonWorkers @43 :Bool
$compatEnableFlag("python_workers")
$pythonSnapshotRelease(pyodide = "0.26.0a2", pyodideRevision = "2024-03-01",
packages = "2024-03-01", backport = 0);
packages = "2024-03-01", backport = 0)
$impliedByAfterDate(name = "pythonWorkersDevPyodide", date = "2000-01-01");
# Enables Python Workers. Access to this flag is not restricted, instead bundles containing
# Python modules are restricted in EWC.
#
# WARNING: Python Workers are still an experimental feature and thus subject to change.

pythonWorkersDevPyodide @58 :Bool
$compatEnableFlag("python_workers_development")
$pythonSnapshotRelease(pyodide = "dev", pyodideRevision = "dev",
packages = "2024-03-01", backport = 0)
$experimental;
# Enables Python Workers and uses the bundle from the Pyodide source directory directly. For testing only.

fetcherNoGetPutDelete @44 :Bool
$compatEnableFlag("fetcher_no_get_put_delete")
$compatDisableFlag("fetcher_has_get_put_delete")
Expand Down
1 change: 1 addition & 0 deletions src/workerd/io/compatibility-date.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ kj::String currentDateStr();

kj::Maybe<PythonSnapshotRelease::Reader> getPythonSnapshotRelease(
CompatibilityFlags::Reader featureFlags);
kj::String getPythonBundleName(PythonSnapshotRelease::Reader pyodideRelease);

// These values come from src/workerd/io/compatibility-date.capnp
static constexpr uint64_t COMPAT_ENABLE_FLAG_ANNOTATION_ID = 0xb6dabbc87cd1b03eull;
Expand Down
6 changes: 3 additions & 3 deletions src/workerd/server/tests/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ load("//src/workerd/server/tests/python:py_wd_test.bzl", "py_wd_test")

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")

# pyodide-dev.capnp.bin represents a custom pyodide version "dev" that is generated
# pyodide_dev.capnp.bin represents a custom pyodide version "dev" that is generated
# at build time using the latest contents of the src/pyodide directory.
# This is used to run tests to ensure that they are always run against the latest build of
# the Pyodide bundle.
copy_file(
name = "pyodide-dev.capnp.bin@rule",
name = "pyodide_dev.capnp.bin@rule",
src = "//src/pyodide:pyodide.capnp.bin",
out = "pyodide-bundle-cache/pyodide-dev.capnp.bin"
out = "pyodide-bundle-cache/pyodide_dev.capnp.bin"
)

py_wd_test(
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/server/tests/python/env-param/env.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const unitTests :Workerd.Config = (
),
],
compatibilityDate = "2024-01-15",
compatibilityFlags = ["python_workers"],
compatibilityFlags = ["python_workers_development"],
)
),
],
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/server/tests/python/hello/hello.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const unitTests :Workerd.Config = (
(name = "worker.py", pythonModule = embed "worker.py")
],
compatibilityDate = "2024-01-15",
compatibilityFlags = ["python_workers"],
compatibilityFlags = ["python_workers_development"],
)
),
],
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/server/tests/python/py_wd_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def py_wd_test(
args = [],
**kwargs
):
data += ["pyodide-dev.capnp.bin@rule"]
args += ["--pyodide-bundle-disk-cache-dir", "$(location pyodide-dev.capnp.bin@rule)/.."]
data += ["pyodide_dev.capnp.bin@rule"]
args += ["--pyodide-bundle-disk-cache-dir", "$(location pyodide_dev.capnp.bin@rule)/.."]

wd_test(
src = src,
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/server/tests/python/random/random.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const unitTests :Workerd.Config = (
(name = "worker.py", pythonModule = embed "worker.py")
],
compatibilityDate = "2024-01-15",
compatibilityFlags = ["python_workers"],
compatibilityFlags = ["python_workers_development"],
)
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const unitTests :Workerd.Config = (
(name = "subdir/a.py", pythonModule = embed "./subdir/a.py"),
],
compatibilityDate = "2023-12-18",
compatibilityFlags = ["python_workers"],
compatibilityFlags = ["python_workers_development"],
)
),
],
Expand Down
20 changes: 6 additions & 14 deletions src/workerd/server/workerd-api.c++
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ kj::Maybe<jsg::ModuleRegistry::ModuleInfo> WorkerdApi::tryCompileModule(

namespace {
kj::Path getPyodideBundleFileName(kj::StringPtr version) {
return kj::Path(kj::str("pyodide-", version, ".capnp.bin"));
return kj::Path(kj::str("pyodide_", version, ".capnp.bin"));
}

kj::Maybe<kj::Own<const kj::ReadableFile>> getPyodideBundleFile(
Expand Down Expand Up @@ -506,7 +506,7 @@ kj::Maybe<jsg::Bundle::Reader> fetchPyodideBundle(const api::pyodide::PythonConf

kj::HttpHeaders headers(table);

kj::String url = kj::str("https://pyodide.runtime-playground.workers.dev/pyodide-capnp-bin/pyodide-", version, ".capnp.bin");
kj::String url = kj::str("https://pyodide.runtime-playground.workers.dev/pyodide-capnp-bin/pyodide_", version, ".capnp.bin");

auto req = client->request(kj::HttpMethod::GET, kj::StringPtr(url), headers);

Expand Down Expand Up @@ -541,18 +541,10 @@ void WorkerdApi::compileModules(
"The python_workers compatibility flag is required to use Python.");
// Inject Pyodide bundle
if(util::Autogate::isEnabled(util::AutogateKey::PYODIDE_LOAD_EXTERNAL)) {
KJ_IF_SOME(bundle, fetchPyodideBundle(impl->pythonConfig, "dev"_kj)) {
modules->addBuiltinBundle(bundle, kj::none);
} else {
auto pythonRelease = KJ_ASSERT_NONNULL(getPythonSnapshotRelease(featureFlags));
auto pyodide = pythonRelease.getPyodide();
auto pyodideRevision = pythonRelease.getPyodideRevision();
auto backport = pythonRelease.getBackport();

auto version = kj::str(pyodide, "_", pyodideRevision, "_", backport);
auto bundle = KJ_ASSERT_NONNULL(fetchPyodideBundle(impl->pythonConfig, version), "Failed to get Pyodide bundle");
modules->addBuiltinBundle(bundle, kj::none);
}
auto pythonRelease = KJ_ASSERT_NONNULL(getPythonSnapshotRelease(featureFlags));
auto version = getPythonBundleName(pythonRelease);
auto bundle = KJ_ASSERT_NONNULL(fetchPyodideBundle(impl->pythonConfig, version), "Failed to get Pyodide bundle");
modules->addBuiltinBundle(bundle, kj::none);
}
// Inject pyodide bootstrap module (TODO: load this from the capnproto bundle?)
{
Expand Down

0 comments on commit daa0378

Please sign in to comment.