forked from c9/cloud9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (113 loc) · 4.07 KB
/
Copy pathindex.js
File metadata and controls
137 lines (113 loc) · 4.07 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Debugger Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
var Path = require("path"),
Spawn = require("child_process").spawn,
Plugin = require("cloud9/plugin"),
sys = require("sys"),
netutil = require("cloud9/netutil");
var PythonRuntimePlugin = module.exports = function(ide) {
this.ide = ide;
this.hooks = ["command"];
this.name = "debugger";
};
sys.inherits(PythonRuntimePlugin, Plugin);
(function() {
this.init = function() {
var _self = this;
this.ide.getExt("state").on("statechange", function(state) {
state.processRunning = !!_self.child;
});
};
this.PYTHON_DEBUG_PORT = 7984;
this.command = function(user, message, client) {
if (!(/py/.test(message.runner)))
return false;
var _self = this;
var cmd = (message.command || "").toLowerCase(),
res = true;
switch (cmd) {
case "run": case "rundebug": case "rundebugbrk": // We don't debug python just yet.
this.$run(message, client);
break;
case "kill":
this.$kill();
break;
default:
res = false;
break;
}
return res;
};
this.$kill = function() {
var child = this.child;
if (!child)
return;
try {
child.kill();
// check after 2sec if the process is really dead
// If not kill it harder
setTimeout(function() {
if (child.pid > 0)
child.kill("SIGKILL");
}, 2000)
}
catch(e) {}
};
this.$run = function(message, client) {
var _self = this;
if (this.child)
return _self.ide.error("Child process already running!", 1, message);
var file = _self.ide.workspaceDir + "/" + message.file;
Path.exists(file, function(exists) {
if (!exists)
return _self.ide.error("File does not exist: " + message.file, 2, message);
var cwd = _self.ide.workspaceDir + "/" + (message.cwd || "");
Path.exists(cwd, function(exists) {
if (!exists)
return _self.ide.error("cwd does not exist: " + message.cwd, 3, message);
// lets check what we need to run
var args = [].concat(file).concat(message.args || []);
_self.$runProc('python', args, cwd, message.env || {}, message.debug || false);
});
});
};
this.$runProc = function(proc, args, cwd, env, debug) {
var _self = this;
// mixin process env
for (var key in process.env) {
if (!(key in env))
env[key] = process.env[key];
}
console.log("Executing python "+proc+" "+args.join(" ")+" "+cwd);
var child = _self.child = Spawn(proc, args, {cwd: cwd, env: env});
_self.debugClient = args.join(" ").search(/(?:^|\b)\-\-debug\b/) != -1;
_self.ide.getExt("state").publishState();
_self.ide.broadcast(JSON.stringify({"type": "node-start"}), _self.name);
child.stdout.on("data", sender("stdout"));
child.stderr.on("data", sender("stderr"));
function sender(stream) {
return function(data) {
var message = {
"type": "node-data",
"stream": stream,
"data": data.toString("utf8")
};
_self.ide.broadcast(JSON.stringify(message), _self.name);
};
}
child.on("exit", function(code) {
_self.ide.broadcast(JSON.stringify({"type": "node-exit"}), _self.name);
_self.debugClient = false;
delete _self.child;
});
return child;
};
this.dispose = function(callback) {
this.$kill();
callback();
};
}).call(PythonRuntimePlugin.prototype);