-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (50 loc) · 1.69 KB
/
main.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
from tempfile import NamedTemporaryFile
import subprocess
from threading import Timer
from os import getcwd
from io import StringIO
import sys
TIMEOUT = 10
suffix_map = {
"python": ".py",
"javascript": ".js"
}
def run_in_contianer(cmd):
proc = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)
timer = Timer(TIMEOUT, proc.kill)
try:
timer.start()
stdout = proc.communicate()[0]
finally:
timer.cancel()
print(stdout.decode("utf-8"))
def run(question_name, user_input, lang):
lang_suffix = suffix_map[lang]
with open(f"solvers/{lang}_soln.jinja", 'r') as f:
test = f.read().replace('{{ user_soln }}', user_input)
with NamedTemporaryFile('w+', suffix=lang_suffix) as f:
print(f"running code: {test}")
f.write(test)
f.seek(0)
print(f.name)
question = f"-v {getcwd()}/questions/{question_name}.json:/app/question.json"
soln = f"-v {f.name}:/app/run{lang_suffix}"
call = f"{lang}_runtime run{lang_suffix}"
cmd = f"docker run {question} {soln} {call}"
# TODO: use runsc from gvisor when running docker
with Capturing() as output:
run_in_contianer(cmd)
return output
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout