-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
101 lines (89 loc) · 2.34 KB
/
util.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
94
95
96
97
98
99
100
101
const fs = require("fs");
const uuid = require("uuid").v1;
const path = require("path");
const getStatusObject = () => {
return {
output: "",
memory: "",
error: false,
error_msg: "",
input_file_path: "",
object_file_path: ""
};
};
const makeOptions = (params, cur_dir) => {
const options = {
input: "",
timeout: 2000,
cwd: pathBaseDir(cur_dir)
};
if (!params) return options;
if (params.input) options.input = params.input;
if (params.time) options.timeout = params.timeout;
return options;
};
const checkSignalTermination = signal => {
if (!signal) return null;
if (signal === "SIGSEGV")
return { error: true, error_msg: "segmentation fault" };
if (signal === "SIGTERM")
return { error: true, error_msg: "process timeout" };
if (signal == "SIGFPE")
return { error: true, error_msg: "floating point exception" };
return { error: true, error_msg: `error ${signal}` };
};
const getLangExtension = lang => {
lang = lang.toLowerCase();
switch (lang) {
case "cpp":
return ".cpp";
break;
case "python":
return ".py";
case "java":
return ".java";
break;
case "c":
return ".c";
default:
throw new Error(`${lang} not found`);
break;
}
};
/**
* Make a temporary in ./tmps/tmpsDir
* This will be cleaned up after executing the command
*/
const makeTmpDir = () => {
// outer tmp dir
const istmpsExists = fs.existsSync("tmps");
if (!istmpsExists) fs.mkdirSync("tmps");
const inner_tmp_path = `./tmps/${uuid()}`;
const inner_tmp = fs.mkdirSync(inner_tmp_path);
return path.join(__dirname, inner_tmp_path);
};
/**
*
* @param {String} tmp_file_dir_path
* @param {String} extension
* @param {String} content
*/
const makeTmpFileWithGivenData = (tmp_file_dir_path, extension, content) => {
//check if tmp_file_dir_path exists
const tmp_file_path = `${tmp_file_dir_path}/${uuid()}${extension}`;
fs.writeFileSync(tmp_file_path, content);
return tmp_file_path;
};
const pathBaseName = (file_name, args) => path.basename(file_name, args);
const pathExtName = file_name => path.extname(file_name);
const pathBaseDir = file_path => path.dirname(file_path);
module.exports = {
makeTmpDir,
makeTmpFileWithGivenData,
getLangExtension,
getStatusObject,
checkSignalTermination,
makeOptions,
pathBaseName,
pathExtName
};