forked from agermanidis/livepython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivepython
More file actions
executable file
·74 lines (59 loc) · 1.61 KB
/
Copy pathlivepython
File metadata and controls
executable file
·74 lines (59 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
const fs = require("fs");
var electronPath = require("electron");
const { spawn } = require("child_process");
const byline = require("byline")
var args = process.argv.slice(2)
if (!args.length) {
console.log("Usage: livepython [program] [..args]")
process.exit()
}
args.unshift(__dirname + "/../tracer.py")
const electronProcess = spawn(electronPath, [__dirname + "/../"], {
stdio: ["pipe", "pipe", "pipe", "ipc"]
})
const pythonProcess = spawn("python", args);
const pythonLineStream = byline.createStream(pythonProcess.stdout);
var buffer = []
var electronWindowOpened = false
pythonLineStream.on("data", line => {
line = line.toString();
if (!line.length) return;
if (line.startsWith("[LIVEPYTHON_TRACER]")) {
const msg = line
.split(" ")
.slice(1)
.join(" ")
if (electronWindowOpened) {
electronProcess.send(msg);
} else {
buffer.push(msg);
}
} else {
process.stdout.write(line + "\n");
}
});
pythonProcess.stderr.on("data", data => {
process.stdout.write(data.toString())
})
pythonProcess.on("exit", (code) => {
if (code !== 0) {
electronProcess.kill('SIGINT')
process.exit();
}
})
electronProcess.on("message", data => {
electronWindowOpened = true
buffer.forEach((msg) => {
electronProcess.send(msg)
})
})
function killSubprocesses () {
// electronProcess.kill("SIGINT")
// pythonProcess.kill("SIGINT")
}
process.on('exit', killSubprocesses)
process.on('SIGINT', killSubprocesses)
process.on("SIGUSR1", killSubprocesses)
process.on("SIGUSR2", killSubprocesses)
process.on("uncaughtException", killSubprocesses)