Skip to content

Commit

Permalink
Add a new subcommand to workerd for making a Python snapshot
Browse files Browse the repository at this point in the history
For now this only makes baseline snapshots but I'll extend it to make
package snapshots when I need it.
  • Loading branch information
hoodmane committed Jan 20, 2025
1 parent 5e538f6 commit 37c3a50
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/workerd/server/workerd.c++
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ class CliMain final: public SchemaFileImpl::ErrorReporter {
.addSubCommand("test", KJ_BIND_METHOD(*this, getTest), "run unit tests")
.addSubCommand("pyodide-lock", KJ_BIND_METHOD(*this, getPyodideLock),
"outputs the package lock file used by Pyodide")
.addSubCommand("make-pyodide-baseline-snapshot",
KJ_BIND_METHOD(*this, getMakePyodideBaselineSnapshot),
"Make a Pyodide baseline memory snapshot")
.build();
// TODO(someday):
// "validate": Loads the config and parses all the code to report errors, but then exits
Expand Down Expand Up @@ -899,6 +902,18 @@ class CliMain final: public SchemaFileImpl::ErrorReporter {
.build();
}

kj::MainFunc getMakePyodideBaselineSnapshot() {
server->allowExperimental();
server->setPythonCreateBaselineSnapshot();
auto builder =
kj::MainBuilder(context, getVersionString(), "Make a Pyodide baseline memory snapshot", "");
setPyodideDiskCacheDir(".");
return builder.expectArg("<python-version>", CLI_METHOD(parsePythonCompatFlag))
.expectArg("<output-directory>", CLI_METHOD(setPackageDiskCacheDir))
.callAfterParsing(CLI_METHOD(test))
.build();
}

void addImportPath(kj::StringPtr pathStr) {
auto path = fs->getCurrentPath().evalNative(pathStr);
if (fs->getRoot().tryOpenSubdir(path) != kj::none) {
Expand Down Expand Up @@ -1023,6 +1038,24 @@ class CliMain final: public SchemaFileImpl::ErrorReporter {
server->setPyodideDiskCacheRoot(kj::mv(dir));
}

void parsePythonCompatFlag(kj::StringPtr compatFlagStr) {
auto builder = kj::heap<capnp::MallocMessageBuilder>();
auto configBuilder = builder->initRoot<config::Config>();
auto service = configBuilder.initServices(1)[0];
service.setName("main");
auto worker = service.initWorker();
worker.setCompatibilityDate("2023-12-18");
auto flags = worker.initCompatibilityFlags(2);
flags.set(0, compatFlagStr);
flags.set(1, "python_workers");
auto mod = worker.initModules(1)[0];
mod.setName("main.py");
mod.setPythonModule("def test():\n pass");
config = configBuilder.asReader();
configOwner = kj::mv(builder);
util::Autogate::initAutogate(getConfig().getAutogates());
}

void watch() {
#if _WIN32
auto& w = watcher.emplace(io.win32EventPort);
Expand Down

0 comments on commit 37c3a50

Please sign in to comment.