-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.py
76 lines (64 loc) · 2.26 KB
/
container.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
import uuid
import os
import shutil
import subprocess
class CodeResult():
# ccr = compiler result, cr = code result
def __init__(self, ccr, cr, uuid, chroot, cc):
self.ccr = ccr
self.cr = cr
self.uuid = uuid
self.chroot = chroot
self.cc = cc
class StubSubprocess():
def __init__(self, stdout):
self.stdout = stdout
def run_code(lang, code):
#TODO: handle a naming conflict (two containers with the same UUID)
cuuid = str(uuid.uuid4())
lang = lang.lower()
#Setup container folder
try:
os.mkdir(cuuid)
except OSError:
print ("Creation of the directory %s failed" % cuuid)
#Find lang extenstion
lang_ext = ""
if lang == "c":
lang_ext = ".c"
elif lang == "cxx" or lang == "cpp" or lang == "c++":
lang_ext = ".cpp"
#Write code to run
f = open(cuuid + "/code" + lang_ext, "w")
f.write(code)
f.close()
#Compile the code
#TODO: pass compiler output to Discord
ccr_result = None
if lang == "c":
ccr_result = subprocess.run(['gcc', "-static", "-o", cuuid + "/a.out", cuuid + "/code" + lang_ext], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
elif lang == "cxx" or lang == "cpp" or lang == "c++":
ccr_result = subprocess.run(['g++', "-static", "-o", cuuid + "/a.out", cuuid + "/code" + lang_ext], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cc = None
if lang == "c":
cc = "gcc"
elif lang == "cxx" or lang == "cpp" or lang == "c++":
cc = "g++"
cr_result = None
try:
cr_result = subprocess.run(["sudo", "chroot", cuuid, "/a.out"], stdout=subprocess.PIPE)
except:
if not os.path.exists(cuuid + "/a.out"):
cr_result = StubSubprocess(b"[Compiler did not produce a binary]")
else:
cr_result = StubSubprocess(b"[HiggsBot internal error]")
#print(ccr_result.stdout)
#print(ccr_result.stderr)
#print(cr_result.stdout)
results = CodeResult(ccr_result.stderr.decode("utf-8").replace(cuuid+"/", ""), cr_result.stdout.decode("utf-8"), cuuid, True, cc)
#Cleanup container folder
try:
shutil.rmtree(cuuid)
except OSError:
print ("Deletion of the directory %s failed" % cuuid)
return results