-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
108 lines (94 loc) · 3.75 KB
/
compiler.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import subprocess
import random
import time
import string
temp_folder_name = 'COMPILER_TEMP'
current_directory = os.getcwd()
temp_folder_path = os.path.join(current_directory, temp_folder_name)
if not os.path.exists(temp_folder_path):
os.mkdir(temp_folder_path)
def compile_cpp(code):
# Generate a random file name within the temporary folder
random_file_name = ''.join(random.choice(string.ascii_letters) for _ in range(10))
cpp_file_name = os.path.join(temp_folder_path, f"{random_file_name}.cpp")
# Save the C++ code to the random file
with open(cpp_file_name, "w") as cpp_file:
cpp_file.write(code)
# Compile the C++ code to an executable within the temporary folder
compile_command = f"g++ {cpp_file_name} -o {os.path.join(temp_folder_path, random_file_name)}.exe"
try:
subprocess.check_output(compile_command, shell=True, stderr=subprocess.STDOUT, text=True, cwd=temp_folder_path)
except subprocess.CalledProcessError as e:
try:
os.remove(cpp_file_name)
except:
pass
return ("Runtime Error: " + e.output,0)
# Execute the compiled program within the temporary folder
executable_name = os.path.join(temp_folder_path, f"{random_file_name}.exe")
try:
start_time = time.time()
result = subprocess.check_output(executable_name, shell=True, stderr=subprocess.STDOUT, text=True, cwd=temp_folder_path)
end_time = time.time()
runtime = end_time-start_time
except subprocess.CalledProcessError as e:
runtime = 0
result = "Compile Time Error" + e.output
try:
os.remove(cpp_file_name)
except:
pass
try:
os.remove(executable_name)
except:
pass
return (result,runtime)
def compile_python(code):
code = code.replace('\t', ' ')
try:
start_time = time.time()
result = subprocess.check_output(['python', '-c',code], stderr=subprocess.STDOUT, text=True)
end_time = time.time()
runtime = end_time-start_time
except subprocess.CalledProcessError as e:
result = "Compile Time Error" + e.output
runtime = 0
return (result,runtime)
def compile_java(code):
# Generate a random file name within the temporary folder
random_file_name = ''.join(random.choice(string.ascii_letters) for _ in range(10))
code = code.replace("Solution",random_file_name)
java_file_name = os.path.join(temp_folder_path, f"{random_file_name}.java")
# Save the Java code to the random file
with open(java_file_name, "w") as java_file:
java_file.write(code)
# Compile the Java code to a class file within the temporary folder
compile_command = f"javac {java_file_name}"
try:
subprocess.check_output(compile_command, shell=True, stderr=subprocess.STDOUT, text=True, cwd=temp_folder_path)
except subprocess.CalledProcessError as e:
try:
os.remove(java_file_name)
except:
pass
return ("Runtime Error: " + e.output, 0)
# Execute the compiled Java program within the temporary folder
class_name = random_file_name
try:
start_time = time.time()
result = subprocess.check_output(f"java {class_name}", shell=True, stderr=subprocess.STDOUT, text=True, cwd=temp_folder_path)
end_time = time.time()
runtime = end_time - start_time
except subprocess.CalledProcessError as e:
runtime = 0
result = e.output
try:
os.remove(java_file_name)
except:
pass
try:
os.remove(f"{class_name}.class")
except:
pass
return (result, runtime)