Path: blob/main/python/python-wasm/src/node-terminal.ts
1067 views
/*1python-wasm [--no-bundle] [--worker] ...2*/34import { resolve } from "path";5import { asyncPython, syncPython } from "./node";6import { supportsPosix } from "@cowasm/kernel";7import { Options } from "./common";89async function main() {10const PYTHONEXECUTABLE = resolve(process.argv[1]);11const { noBundle, worker } = processArgs(process.argv);12if (process.platform == "win32") {13console.log("Press enter a few times.");14}15const options: Options = { env: { PYTHONEXECUTABLE }, interactive: true };16if (!noBundle) {17options.fs = "everything";18}19const Python = worker ? asyncPython : syncPython;20const python = await Python(options);21const argv = [PYTHONEXECUTABLE].concat(process.argv.slice(2));22let r = 0;23try {24// in async mode the worker thread itself just gets killed, e.g., when python runs25// import sys; sys.exit(1) and that triggers "this.worker?.on("exit", () => {" in26// packages/kernel/src/wasm/import.ts. We don't get back an exit code, and I don't27// yet know how to get it. This is another drawback of using a worker thread with node.28r = await python.terminal(argv);29} catch (_err) {}30if (argv.includes("-h")) {31console.log("\npython-wasm [--worker] [--no-bundle] ...");32console.log(33"--worker : use a worker thread, instead of single threaded version (NOTE: exit code is always 0)"34);35console.log(36"--no-bundle : do not use the python bundle archive (only for development)"37);38}39process.exit(r);40}4142function processArgs(argv: string[]): { noBundle: boolean; worker: boolean } {43const i = argv.indexOf("--no-bundle");44const noBundle = i != -1;45if (noBundle) {46argv.splice(i, 1);47}48const j = argv.indexOf("--worker");49if (j != -1) {50argv.splice(j, 1);51}52let worker = false;53if (!supportsPosix()) {54worker = true;55} else {56worker = j != -1;57}58return { noBundle, worker };59}6061main();626364