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