-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
318 lines (278 loc) · 9.53 KB
/
Copy pathexecutor.py
File metadata and controls
318 lines (278 loc) · 9.53 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from __future__ import annotations
import io
import os
import subprocess
import sys
import tarfile
import tempfile
import time
from typing import Any
import docker
from docker.errors import APIError, DockerException
from requests.exceptions import ReadTimeout
MAX_OUTPUT_CHARS = 20_000
DEFAULT_SANDBOX_IMAGE = "codezen-sandbox:latest"
SANDBOX_IMAGE = os.getenv("SANDBOX_IMAGE", DEFAULT_SANDBOX_IMAGE)
class DockerUnavailable(RuntimeError):
pass
LANGUAGE_ALIASES = {"js": "javascript"}
DOCKER_LANGUAGE_CONFIG: dict[str, dict[str, str]] = {
"python": {
"image": SANDBOX_IMAGE,
"source_file": "main.py",
"command": "python3 main.py < stdin.txt",
},
"javascript": {
"image": SANDBOX_IMAGE,
"source_file": "main.js",
"command": "node main.js < stdin.txt",
},
"java": {
"image": SANDBOX_IMAGE,
"source_file": "Main.java",
"command": "javac Main.java && java Main < stdin.txt",
},
"c": {
"image": SANDBOX_IMAGE,
"source_file": "main.c",
"command": "gcc main.c -O2 -std=c11 -o main && ./main < stdin.txt",
},
"cpp": {
"image": SANDBOX_IMAGE,
"source_file": "main.cpp",
"command": "g++ main.cpp -O2 -std=c++17 -o main && ./main < stdin.txt",
},
}
def _shell_command(command: str) -> list[str]:
if os.name == "nt":
return ["cmd", "/c", command]
return ["sh", "-lc", command]
LOCAL_LANGUAGE_CONFIG: dict[str, dict[str, Any]] = {
"python": {
"source_file": "main.py",
"command": [sys.executable, "main.py"],
},
"javascript": {
"source_file": "main.js",
"command": ["node", "main.js"],
},
"java": {
"source_file": "Main.java",
"command": _shell_command("javac Main.java && java Main"),
},
"c": {
"source_file": "main.c",
"command": _shell_command("gcc main.c -O2 -std=c11 -o main && ./main"),
},
"cpp": {
"source_file": "main.cpp",
"command": _shell_command("g++ main.cpp -O2 -std=c++17 -o main && ./main"),
},
}
def normalize_language(language: str) -> str:
normalized = language.strip().lower()
return LANGUAGE_ALIASES.get(normalized, normalized)
def _truncate_output(text: str) -> str:
if len(text) <= MAX_OUTPUT_CHARS:
return text
return text[:MAX_OUTPUT_CHARS] + "\n\n[output truncated]"
def _build_archive(source_file: str, code: str, stdin: str) -> bytes:
buffer = io.BytesIO()
with tarfile.open(fileobj=buffer, mode="w") as archive:
for file_name, content in ((source_file, code), ("stdin.txt", stdin)):
encoded = content.encode("utf-8")
tar_info = tarfile.TarInfo(name=file_name)
tar_info.size = len(encoded)
tar_info.mtime = int(time.time())
archive.addfile(tar_info, io.BytesIO(encoded))
buffer.seek(0)
return buffer.read()
def _collect_logs(container: docker.models.containers.Container) -> tuple[str, str]:
stdout_parts: list[str] = []
stderr_parts: list[str] = []
try:
log_stream = container.logs(stream=True, stdout=True, stderr=True, demux=True)
for stdout_chunk, stderr_chunk in log_stream:
if stdout_chunk:
stdout_parts.append(stdout_chunk.decode("utf-8", errors="replace"))
if stderr_chunk:
stderr_parts.append(stderr_chunk.decode("utf-8", errors="replace"))
except TypeError:
stdout_fallback = container.logs(stdout=True, stderr=False)
stderr_fallback = container.logs(stdout=False, stderr=True)
return (
stdout_fallback.decode("utf-8", errors="replace"),
stderr_fallback.decode("utf-8", errors="replace"),
)
return "".join(stdout_parts), "".join(stderr_parts)
def _execute_local(
normalized_language: str,
code: str,
stdin: str,
timeout_seconds: int,
) -> dict[str, Any]:
if normalized_language not in LOCAL_LANGUAGE_CONFIG:
supported = ", ".join(sorted(LOCAL_LANGUAGE_CONFIG))
raise ValueError(
f"Unsupported language '{normalized_language}'. Supported values: {supported}."
)
config = LOCAL_LANGUAGE_CONFIG[normalized_language]
start_time = time.perf_counter()
timed_out = False
status_code = 1
stdout = ""
stderr = ""
try:
with tempfile.TemporaryDirectory() as workdir:
source_path = os.path.join(workdir, config["source_file"])
with open(source_path, "w", encoding="utf-8") as handle:
handle.write(code)
try:
completed = subprocess.run(
config["command"],
cwd=workdir,
input=stdin,
capture_output=True,
text=True,
timeout=timeout_seconds,
)
stdout = completed.stdout
stderr = completed.stderr
status_code = completed.returncode
except subprocess.TimeoutExpired as exc:
timed_out = True
status_code = 124
stdout = exc.stdout or ""
stderr = exc.stderr or ""
except FileNotFoundError as exc:
stderr = f"Local execution error: {exc}"
status_code = 127
except Exception as exc:
stderr = f"Local execution error: {exc}"
status_code = 1
if timed_out:
timeout_message = f"Execution timed out after {timeout_seconds} seconds."
stderr = f"{stderr}\n{timeout_message}".strip()
duration_ms = int((time.perf_counter() - start_time) * 1000)
return {
"stdout": _truncate_output(stdout),
"stderr": _truncate_output(stderr),
"exit_code": status_code,
"timed_out": timed_out,
"duration_ms": duration_ms,
}
def _execute_docker(
normalized_language: str,
code: str,
stdin: str,
timeout_seconds: int,
) -> dict[str, Any]:
if normalized_language not in DOCKER_LANGUAGE_CONFIG:
supported = ", ".join(sorted(DOCKER_LANGUAGE_CONFIG))
raise ValueError(
f"Unsupported language '{normalized_language}'. Supported values: {supported}."
)
config = DOCKER_LANGUAGE_CONFIG[normalized_language]
sandbox_image = config["image"]
start_time = time.perf_counter()
client: docker.DockerClient | None = None
container: docker.models.containers.Container | None = None
timed_out = False
status_code = 1
stdout = ""
stderr = ""
try:
try:
client = docker.from_env()
client.ping()
except (DockerException, FileNotFoundError) as exc:
raise DockerUnavailable(str(exc)) from exc
container = client.containers.create(
image=sandbox_image,
command=["sh", "-lc", config["command"]],
detach=True,
network_disabled=True,
working_dir="/sandbox",
mem_limit="256m",
nano_cpus=500_000_000,
pids_limit=64,
cap_drop=["ALL"],
security_opt=["no-new-privileges"],
user="nobody",
)
archive = _build_archive(config["source_file"], code, stdin)
copied = container.put_archive("/sandbox", archive)
if not copied:
raise RuntimeError("Failed to copy files into sandbox container.")
container.start()
try:
wait_result = container.wait(timeout=timeout_seconds)
status_code = int(wait_result.get("StatusCode", 1))
except ReadTimeout:
timed_out = True
status_code = 124
container.kill()
stdout, stderr = _collect_logs(container)
except DockerUnavailable:
raise
except (APIError, DockerException, RuntimeError) as exc:
stderr = (
"Docker sandbox error: "
f"{exc}\nSet EXECUTION_BACKEND=local to run without Docker."
)
status_code = 1
finally:
if container is not None:
try:
container.remove(force=True)
except Exception:
pass
if client is not None:
client.close()
if timed_out:
timeout_message = f"Execution timed out after {timeout_seconds} seconds."
stderr = f"{stderr}\n{timeout_message}".strip()
duration_ms = int((time.perf_counter() - start_time) * 1000)
return {
"stdout": _truncate_output(stdout),
"stderr": _truncate_output(stderr),
"exit_code": status_code,
"timed_out": timed_out,
"duration_ms": duration_ms,
}
def execute_code(
language: str,
code: str,
stdin: str,
timeout_seconds: int = 5,
) -> dict[str, Any]:
normalized_language = normalize_language(language)
backend = os.getenv("EXECUTION_BACKEND", "local").strip().lower()
if backend == "local":
return _execute_local(
normalized_language,
code,
stdin,
timeout_seconds,
)
if backend != "docker":
raise ValueError(
"Unsupported EXECUTION_BACKEND. Use 'docker' or 'local'."
)
try:
return _execute_docker(
normalized_language,
code,
stdin,
timeout_seconds,
)
except DockerUnavailable:
fallback = os.getenv("EXECUTION_DOCKER_FALLBACK", "local").strip().lower()
if fallback == "local":
return _execute_local(
normalized_language,
code,
stdin,
timeout_seconds,
)
raise