Spaces:
Runtime error
Runtime error
Muennighoff
commited on
Commit
•
318210e
1
Parent(s):
ae00acd
Rust w/o temp faster?
Browse files- execute.py +36 -32
execute.py
CHANGED
@@ -225,43 +225,47 @@ def unsafe_execute_js(check_program, result, timeout):
|
|
225 |
|
226 |
def unsafe_execute_rust(check_program, result, timeout, cargo_string):
|
227 |
|
228 |
-
with create_tempdir():
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
|
|
245 |
open(f"{RUST_DIR}/Cargo.toml", 'w').write(cargo_string)
|
246 |
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
|
253 |
-
|
254 |
-
|
255 |
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
else:
|
262 |
-
result.append("failed: execution error: " + exec_result.stderr.decode())
|
263 |
else:
|
264 |
-
result.append("failed:
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
|
267 |
@contextlib.contextmanager
|
|
|
225 |
|
226 |
def unsafe_execute_rust(check_program, result, timeout, cargo_string):
|
227 |
|
228 |
+
#with create_tempdir():
|
229 |
+
|
230 |
+
WD: str = os.getcwd()
|
231 |
+
RUST_DIR: str = os.path.join(WD, "rust")
|
232 |
+
RUST_SRC: str = os.path.join(RUST_DIR, "src")
|
233 |
+
RUST_BIN: str = os.path.join(RUST_SRC, "bin")
|
234 |
+
RUST_TMP_DIR: str = os.path.join(RUST_DIR, "tmp")
|
235 |
+
RUST_LOGS: str = os.path.join(RUST_TMP_DIR, "logs")
|
236 |
+
RUST_EXT: str = ".rs"
|
237 |
+
|
238 |
+
# Create mandatory tmp directories
|
239 |
+
os.makedirs(RUST_TMP_DIR, exist_ok=True)
|
240 |
+
os.makedirs(RUST_LOGS, exist_ok=True)
|
241 |
+
os.makedirs(RUST_SRC, exist_ok=True)
|
242 |
+
os.makedirs(RUST_BIN, exist_ok=True)
|
243 |
+
|
244 |
+
# Create Cargo.toml file
|
245 |
+
if not os.path.exists(f"{RUST_DIR}/Cargo.toml"):
|
246 |
open(f"{RUST_DIR}/Cargo.toml", 'w').write(cargo_string)
|
247 |
|
248 |
+
with tempfile.NamedTemporaryFile(dir = RUST_BIN, delete=False) as f:
|
249 |
+
file_name: str = "test" + RUST_EXT
|
250 |
+
os.rename(f.name, os.path.join(RUST_BIN, file_name))
|
251 |
+
# Dump the rust source code in the target temporal file
|
252 |
+
f.write(check_program.encode('utf-8'))
|
253 |
|
254 |
+
# Proceed towards Rust binaries compilation. Therefore move to Rust module root dir.
|
255 |
+
os.chdir(RUST_DIR)
|
256 |
|
257 |
+
compilation_result = subprocess.run(["cargo", "check", "--bin", "test", "--message-format", "json"], timeout=timeout, capture_output=True)
|
258 |
+
if compilation_result.returncode == 0:
|
259 |
+
exec_result = subprocess.run(["cargo", "test", "--bin", "test", "--message-format", "json"], timeout=timeout, capture_output=True)
|
260 |
+
if exec_result.returncode == 0:
|
261 |
+
result.append("passed")
|
|
|
|
|
262 |
else:
|
263 |
+
result.append("failed: execution error: " + exec_result.stderr.decode())
|
264 |
+
else:
|
265 |
+
result.append("failed: compilation error: " + compilation_result.stderr.decode())
|
266 |
+
|
267 |
+
# Move back to the original working directory
|
268 |
+
os.chdir(WD)
|
269 |
|
270 |
|
271 |
@contextlib.contextmanager
|