-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompile_run.py
40 lines (31 loc) · 1.1 KB
/
compile_run.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
import subprocess
from color_print import cprint
# Compile cpp, c, java file
def compile(file_path):
lang = file_path.split(".")[-1]
file = ".".join(file_path.split(".")[:-1])
if(lang == "cpp" or lang == "c"):
cmd = "g++ -std=c++17 -DONLINE_JUDGE \"{}\" -o \"{}.exe\"".format(file_path, file)
elif(lang == "java"):
cmd = "javac \"{}\"".format(file_path)
else:
return -1
# print(cmd)
print('\nCompiling ', end = '')
cprint(file_path, clr = "purple")
subprocess.run(cmd, check = True, shell = True)
print('Compilation done')
# Run file and return output
def run(file_path, std_input = None):
lang = file_path.split(".")[-1]
file = ".".join(file_path.split(".")[:-1])
if(lang == "cpp" or lang == "c"):
cmd = "\"{}.exe\"".format(file)
elif(lang == "java"):
cmd = "java Main"
elif(lang == "py"):
cmd = "python \"{}\"".format(file_path)
else:
return -1
# print(cmd)
return subprocess.run(cmd, input = std_input, capture_output = True, shell = True, text = True, check = True)