-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlanguages.py
198 lines (163 loc) · 5.4 KB
/
languages.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import logging
import subprocess
import os
from abc import ABCMeta
from typing import List, Dict
import config
from models import JobVerdict
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logging.info("Starting up")
class Language(metaclass=ABCMeta):
@classmethod
def compile(
cls,
source_code: str,
workdir: str,
executable_name: str,
time_limit: float = config.COMPILATION_TIME_LIMIT,
) -> str:
raise NotImplementedError()
@classmethod
def get_command(cls, workdir: str, executable_name: str) -> List[str]:
raise NotImplementedError()
@classmethod
def get_allowed_files(cls, workdir: str, executable_name: str):
raise NotImplementedError()
@classmethod
def get_allowed_file_prefixes(cls, workdir: str, executable_name: str):
raise NotImplementedError()
class CXX(Language):
@classmethod
def compile(
cls,
source_code: str,
workdir: str,
executable_name: str,
time_limit: float = config.COMPILATION_TIME_LIMIT,
) -> str:
source_file_path = os.path.join(workdir, "source.cpp")
with open(source_file_path, "wb") as source_file:
source_file.write(source_code.encode("utf-8"))
executable_file_path = os.path.join(workdir, executable_name)
try:
subprocess.check_call(
["g++", "--std=c++1y", "-o", executable_file_path, source_file_path],
timeout=config.COMPILATION_TIME_LIMIT,
)
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
return None
return executable_name
@classmethod
def get_command(cls, workdir: str, executable_name: str) -> List[str]:
return [os.path.join(workdir, executable_name)]
@classmethod
def get_allowed_files(cls, workdir: str, executable_name: str):
return []
@classmethod
def get_allowed_file_prefixes(cls, workdir: str, executable_name: str):
return []
class Python(Language):
language_name = "python"
interpreter_name = "python"
@classmethod
def compile(
cls,
source_code: str,
workdir: str,
executable_name: str,
time_limit: float = config.COMPILATION_TIME_LIMIT,
) -> str:
executable_name += ".py"
executable_path = os.path.join(workdir, executable_name)
with open(executable_path, "wb") as executable_file:
executable_file.write(source_code.encode("utf-8"))
"""try:
subprocess.check_call([cls.interpreter_name, '-m', 'py_compile', executable_name],
timeout=config.COMPILATION_TIME_LIMIT)
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
return None"""
return executable_name
@classmethod
def get_command(cls, workdir: str, executable_name: str) -> List[str]:
return [
os.path.join("/usr/bin", cls.interpreter_name),
"-s",
"-S",
os.path.join(workdir, executable_name),
]
@classmethod
def get_allowed_files(cls, workdir: str, executable_name: str):
return [
"/etc/nsswitch.conf",
"/etc/passwd",
"/dev/urandom", # TODO: come up with random policy
"/tmp",
"/bin/Modules/Setup",
workdir,
os.path.join(workdir, executable_name),
]
@classmethod
def get_allowed_file_prefixes(cls, workdir: str, executable_name: str):
return []
class Java(Language):
@classmethod
def compile(
cls,
source_code: str,
workdir: str,
executable_name: str,
time_limit: float = config.COMPILATION_TIME_LIMIT,
) -> str:
source_file_path = os.path.join(workdir, "Main.java")
with open(source_file_path, "wb") as source_file:
source_file.write(source_code.encode("utf-8"))
executable_file_path = os.path.join(workdir, "Main")
try:
subprocess.check_call(
["javac", "-d", workdir, source_file_path],
timeout=config.COMPILATION_TIME_LIMIT,
)
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
return None
return "Main"
@classmethod
def get_command(cls, workdir: str, executable_name: str) -> List[str]:
return [
"/usr/bin/java",
"-XX:-UsePerfData",
"-XX:+DisableAttachMechanism",
"-Xmx256m",
"-Xrs",
"-cp",
workdir,
executable_name,
]
@classmethod
def get_allowed_files(cls, workdir: str, executable_name: str):
return [
"/etc/nsswitch.conf",
"/etc/passwd",
"/tmp",
workdir,
os.path.join(workdir, executable_name + ".class"),
]
@classmethod
def get_allowed_file_prefixes(cls, workdir: str, executable_name: str):
return [
"/etc/java-7-openjdk/",
"/tmp/.java_pid",
"/tmp/",
]
class Python2(Python):
language_name = "python2"
interpreter_name = "python2.7"
class Python3(Python):
language_name = "python3"
interpreter_name = "python3.5"
languages = {
"cxx": CXX,
"python2": Python2,
"python3": Python3,
"java": Java,
} # type: Dict[str, Language]