-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (86 loc) · 3.35 KB
/
app.js
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
const express = require('express')
const path = require('path')
const fs = require('fs')
const { promisify } = require('util')
const { spawn } = require('child_process')
const debug = require('debug')('app: py-node')
const cacheMiddleware = require('./cacheMiddleware')
const app = express()
const port = 4000
app.set('views', './views');
app.set('view engine', 'ejs');
const readdir = promisify(fs.readdir);
app.get('/', cacheMiddleware, (req, res) => {
(async function doStuff() {
const dir = path.join(__dirname + '/' + 'scripts');
const files = await readdir(dir)
let dat = [];
let str, py;
for (let file of files) {
try {
if (path.extname(file) === '.py') {
py = spawn(
'python',
['-u', path.join(__dirname, 'scripts', file),
'--foo', 'some value']);
} else if (path.extname(file) === '.js') {
py = spawn('node', [path.join(__dirname, 'scripts', file)]);
} else if (path.extname(file) === '.php') {
py = spawn('php', [path.join(__dirname, 'scripts', file)]);
} else {
console.log(`${path.basename(file)} file type with ext ${path.extname(file)} is not supported`)
}
py.stdout.on('data', function (data) {
//console.log('Pipe data from python file ...');
str = data.toString('utf8');
const res = str.split(' ');
const language = `${res[14]}`
let status;
if (language == 'javascript' || language == 'JavaScript'|| language == 'Javascript' || language == 'javaScript'
|| language == 'python'|| language == 'Python' || language == 'php' || language == 'PHP' || language == 'nodejs'
|| language == 'NodeJs' || language == 'nodeJs' || language == 'Golang') {
status = 'pass'
} else {
status = 'fail'
}
dat.push({
file: path.basename(file),
output: str,
name: `${res[4]} ${res[5]}`,
id: `${res[9]}`,
email: `${res[12]}`,
language: `${res[14]}`,
status: status
})
});
py.stderr.on('data', (code) => {
console.log(`child process have an error with code ${code}`);
})
py.on('exit', () => {
//console.log('foobar exiting: ', dat)
})
} catch (err) {
console.log(`${err}`)
}
}
py.stdout.on('end', () => {
//console.log('foobar ending: ', dat)
let pass = 0
let fail = 0
for (let i of dat) {
if (i.status === 'pass'){
pass = pass + 1
}
if (i.status === 'fail'){
fail = fail+ 1
}
}
let passPercentage = (pass/dat.length) * 100
passPercentage = passPercentage.toFixed(2)
let failPercentage = (fail/dat.length) * 100
failPercentage = failPercentage.toFixed(2)
res.render('index', {da: dat, pa: pass, fa: fail, pap: passPercentage, fap: failPercentage})
})
})();
});
app.listen(port, () => console.log(`App listening on port ${port}!`))