From 17a84f8069df0684756311f66bdd92165805c47c Mon Sep 17 00:00:00 2001 From: Amr Ragaey Date: Sun, 14 Aug 2016 20:46:19 +0200 Subject: [PATCH 1/2] add python-runner functions 'run', and 'writeFiles' --- src/python_runner.js | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/python_runner.js diff --git a/src/python_runner.js b/src/python_runner.js new file mode 100644 index 00000000..4c3a8b23 --- /dev/null +++ b/src/python_runner.js @@ -0,0 +1,61 @@ +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); + console.log("done"); + }); +} + +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; + +} + + +module.exports = pythonRunner; \ No newline at end of file From b2d811848fad8232d3944568de8d8f4808246a61 Mon Sep 17 00:00:00 2001 From: Amr Ragaey Date: Tue, 16 Aug 2016 01:38:11 +0200 Subject: [PATCH 2/2] add executeScript and killScript functions --- src/python_runner.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/python_runner.js b/src/python_runner.js index 4c3a8b23..6a525ad3 100644 --- a/src/python_runner.js +++ b/src/python_runner.js @@ -13,7 +13,6 @@ pythonRunner.prototype.run = function(callback) myRunner.writeFiles( function(){ myRunner.executeScript(callback); - console.log("done"); }); } @@ -35,8 +34,8 @@ pythonRunner.prototype.writeFiles = function(callback) else { console.log("Python file saved!"); - fs.writeFile(myRunner.path + myRunner.folder + "/" + "inputFile", myRunner.stdin,function(err) { - console.log(myRunner.path + myRunner.folder + "/" + "inputFile"); + fs.writeFile(myRunner.path + myRunner.folder + "/inputFile", myRunner.stdin,function(err) { + console.log(myRunner.path + myRunner.folder + "/inputFile"); if (err) { console.log(err); } @@ -55,7 +54,35 @@ 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; \ No newline at end of file