import { asyncKernel, FileSystemSpec } from "@cowasm/kernel";
import { Options, PythonWasmAsync } from "./common";
export type { Options, PythonWasmAsync };
import { fetchPackages } from "./packages";
import debug from "debug";
const log = debug("python-wasm");
import wasmUrl from "./python.wasm";
import pythonFull from "./python-stdlib.zip";
import pythonMinimal from "./python-minimal.zip";
import pythonReadline from "./python-readline.zip";
const PYTHONEXECUTABLE = "/usr/lib/python.wasm";
export default async function asyncPython(
opts?: Options
): Promise<PythonWasmAsync> {
log("creating async CoWasm kernel...");
const fs = getFilesystem(opts);
log("fs = ", fs);
const kernel = await asyncKernel({
env: {
PYTHONHOME: "/usr",
PYTHONEXECUTABLE,
...opts?.env,
},
fs,
});
log("done");
log("fetching ", PYTHONEXECUTABLE);
await Promise.all([
kernel.waitUntilFsLoaded(),
kernel.fetch(wasmUrl, PYTHONEXECUTABLE),
fetchPackages(kernel),
]);
log("initializing python");
const python = new PythonWasmAsync(kernel, PYTHONEXECUTABLE);
await python.init();
log("done");
return python;
}
function getFilesystem(opts?: Options): FileSystemSpec[] {
return [
{
type: "zipurl",
zipurl: opts?.noReadline ? pythonMinimal : pythonReadline,
mountpoint: "/usr/lib/python3.11",
},
{ type: "dev" },
{
type: "zipurl",
async: true,
zipurl: pythonFull,
mountpoint: "/usr/lib/python3.11",
},
];
}