aboutsummaryrefslogtreecommitdiff
path: root/src/compileAndRun/index.js
blob: 6f89e4486c457a46e525871a81621c48ee7cf6a3 (plain) (blame)
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
let EventEmitter = require('events');
let fse = require("fs-extra");
let Process = require("./process");
let pythonSupportFiles = __dirname + "/pythonSupportFiles/";

class Python extends EventEmitter {

    constructor(profileFolder, usrDataFolder) {
        super();
        this.profileFolder = profileFolder;
        this.usrDataFolder = usrDataFolder;

        this.stdout = "";
        this.stderr = "";
    }

    status() {
        return {};
    }

    compile() {
        try {
            fse.removeSync(this.profileFolder + "/build");
            fse.copySync(pythonSupportFiles, this.profileFolder + "/build/");
            fse.copySync(this.profileFolder + "/script.py", this.profileFolder + "/build/script.py");
        } catch (err) {
            console.log(err);
        }
    }

    run() {
        // Setup paths and commands
        let entryPath = this.profileFolder + "/build/entry.py";

        // Spawn the new process
        this.pyProc = new Process("python", [
            "-u",               // This makes us able to get real-time output
            entryPath,          // This is where the entry-point is located
            this.usrDataFolder  // This is required for the python-script to now where our config-files are
        ]);
        this.pyProc.start();

        this.pyProc.proc.stdout.on("data", (_stdout) => {
            this.emit("stdout::data", "" + _stdout);
        });
        this.pyProc.proc.stdout.on("end", () => {
            this.emit("stdout::end");
        });
        this.pyProc.proc.stderr.on("data", (_stderr) => {
            this.emit("stderr::data", _stderr);
        });
        this.pyProc.proc.stderr.on("end", () => {
            this.emit("stderr:end");
        });
        this.pyProc.proc.on("close", (_code) => {
            this.emit("close", _code);
        });

    }

    compileAndRun() {
        this.compile();
        this.run();
    }

    stop() {
        this.pyProc.proc.kill("SIGINT");
    }

}

module.exports = {
    Python: Python,
    Process: Process
};