-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexecuter.py
94 lines (72 loc) · 2.8 KB
/
executer.py
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
import subprocess
import sys
import time
import re
def c_cpp_code_to_fileio(inp, out_name, isplus):
if not isplus:
out_code = '#include <stdio.h>\n'
else:
out_code = '#include <cstdio>\n'
file_io_lines = "int main(int argc, char const *argv[]) {"
file_io_lines += '\n\tfreopen(argv[1], "r", stdin);'
file_io_lines += '\n\tfreopen(argv[2], "w", stdout);'
main_func_pattern = r'int[\s]*main[\s]*\([^\)]*\)[\s]*{'
with open(inp, 'r') as in_f:
out_code += in_f.read()
out_code = re.sub(main_func_pattern, file_io_lines, out_code)
file_ext = ".c" if not isplus else ".cpp"
with open(out_name + file_ext, 'w') as op_f:
op_f.write(out_code)
return out_name + file_ext
def py_code_to_fileio(inp, out_name):
out_code = 'import sys\n'
out_code += "fin = open(sys.argv[1], 'r')\n"
out_code += "fout = open(sys.argv[2], 'w+')\n"
with open(inp, 'r') as in_f:
out_code += in_f.read()
inp_pattern = r'input[\s]*\([\s]*\)([\s]*\.[\s]*[lr]?strip[\s]*\([\s]*\))*'
out_pattern = r'print[^\S\r\n]*\((.*)\)'
out_code = re.sub(inp_pattern, 'fin.readline().strip()', out_code)
out_code = re.sub(out_pattern, 'print(str(\\1), file=fout)', out_code)
with open(out_name + ".py", 'w') as op_f:
op_f.write(out_code)
return out_name + ".py"
def compile_c_code(inp, out_bin):
try:
start_time = time.time()
subprocess.check_call([r"/usr/bin/gcc", "--std=c11", "-Wall",
"-o", out_bin, inp + ".c"])
end_time = time.time()
return end_time - start_time
except subprocess.CalledProcessError as _:
print("Compilation Error in file : " + inp)
sys.exit(1)
def compile_cpp_code(inp, out_bin):
try:
start_time = time.time()
subprocess.check_call([r"/usr/bin/g++", "--std=c++14", "-Wall",
"-o", out_bin, inp + ".cpp"])
end_time = time.time()
return end_time - start_time
except subprocess.CalledProcessError as _:
print("Compilation Error in file : " + inp)
sys.exit(1)
def run_c_cpp_bin(binary, in_tc, out_res):
try:
start_time = time.time()
subprocess.check_call(["./" + binary, in_tc, out_res])
end_time = time.time()
return end_time - start_time
except subprocess.CalledProcessError as _:
print("Runtime Error in binary_file : " + binary)
sys.exit(1)
def run_py_code(inp, in_tc, out_res):
try:
inp = inp + ".py"
start_time = time.time()
subprocess.check_call([r"/usr/bin/python3", inp, in_tc, out_res])
end_time = time.time()
return end_time - start_time
except subprocess.CalledProcessError as _:
print("Runtime Error in python_file : " + inp)
sys.exit(1)