Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/python_runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var pythonRunner = function(path, folder, fileName, code, stdin)
{
this.path=path;
this.folder=folder;
this.fileName=fileName;
this.code = code;
this.stdin=stdin;
}

pythonRunner.prototype.run = function(callback)
{
var myRunner = this;

myRunner.writeFiles( function(){
myRunner.executeScript(callback);
});
}

pythonRunner.prototype.writeFiles = function(callback)
{
var myRunner = this;

var exec = require('child_process').exec;
var fs = require('fs');

console.log("mkdir "+ myRunner.path + myRunner.folder);
exec("mkdir "+ myRunner.path + myRunner.folder + "&& chmod 777 "+ myRunner.path + myRunner.folder, function(st)
{
fs.writeFile(myRunner.path + myRunner.folder + "/" + myRunner.fileName, myRunner.code,function(err) {
console.log(myRunner.path + myRunner.folder + "/" + myRunner.fileName);
if (err) {
console.log(err);
}
else {
console.log("Python file saved!");

fs.writeFile(myRunner.path + myRunner.folder + "/inputFile", myRunner.stdin,function(err) {
console.log(myRunner.path + myRunner.folder + "/inputFile");
if (err) {
console.log(err);
}
else {
console.log("Input file saved!");
callback();
}
});
}
});

});
}

pythonRunner.prototype.executeScript = function(callback)
{
var myRunner = this;

var exec = require('child_process').exec;
var fs = require('fs');

//execute ppython script -> write stdout or stderr to output.text
//return output.text contents in callback
c = exec("python " + myRunner.path + myRunner.folder + "/" + myRunner.fileName + " -< " + myRunner.path + myRunner.folder + "/inputFile " + "2>&1 | tee -a " + myRunner.path + myRunner.folder + "/output.text", function(st){
fs.readFile(myRunner.path + myRunner.folder + '/output.text', 'utf8', function(err, data){
if (!data) {
console.log= "error reading output file";
}
else {
callback(data);
}
});
});

console.log(c.pid);
}

pythonRunner.prototype.killScript = function(callback)
{
var myRunner = this;
var exec = require('child_process').exec;

//remove temporary folder and kill python process
exec("rm -rf "+ myRunner.path + myRunner.folder, function(st){
c.kill();
callback("process terminated");
});
}

module.exports = pythonRunner;