-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathlivepython
More file actions
executable file
·85 lines (68 loc) · 1.86 KB
/
livepython
File metadata and controls
executable file
·85 lines (68 loc) · 1.86 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
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
const fs = require("fs");
var electronPath = require("electron");
const net = require("net");
var buffer = [];
var electronWindowOpened = false;
var socket;
net.createServer((s) => {
socket = s;
const pythonLineStream = byline.createStream(socket);
pythonLineStream.on("data", line => {
line = line.toString();
if (!line.length) return;
if (electronWindowOpened) {
electronProcess.send(line);
} else {
buffer.push(line);
}
});
}).listen(4387)
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);
pythonProcess.stdout.on("data", data => {
process.stdout.write(data.toString());
});
pythonProcess.stderr.on("data", data => {
process.stdout.write(data.toString())
})
pythonProcess.on("exit", (code) => {
electronProcess.kill('SIGINT')
process.exit();
})
electronProcess.on("message", msg => {
if (msg.type === 'connected') {
electronWindowOpened = true;
buffer.forEach(msg => {
electronProcess.send(msg);
});
} else if (msg.type === "toggle_running_state") {
if (msg.value) {
pythonProcess.kill("SIGSTOP")
} else {
pythonProcess.kill("SIGCONT");
}
} else {
socket.write(JSON.stringify(msg))
}
})
function killSubprocesses (e) {
electronProcess.kill("SIGINT");
pythonProcess.kill("SIGINT");
process.exit();
}
process.on('exit', killSubprocesses)
process.on('SIGINT', killSubprocesses)
process.on("SIGUSR1", killSubprocesses)
process.on("SIGUSR2", killSubprocesses)
process.on("uncaughtException", killSubprocesses)