Spaces:
Runtime error
Runtime error
Create executor.py
Browse files- executor.py +73 -0
executor.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess, tempfile, shutil, traceback, importlib.util
|
2 |
+
import concurrent.futures
|
3 |
+
import os
|
4 |
+
|
5 |
+
class CodeExecutor:
|
6 |
+
def __init__(self):
|
7 |
+
self.temp_dir = tempfile.TemporaryDirectory()
|
8 |
+
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
|
9 |
+
|
10 |
+
def _install_packages(self, packages):
|
11 |
+
for package in packages:
|
12 |
+
package = package.strip()
|
13 |
+
if not package:
|
14 |
+
continue
|
15 |
+
try:
|
16 |
+
spec = importlib.util.find_spec(package)
|
17 |
+
if spec is None:
|
18 |
+
subprocess.check_call(["pip", "install", package], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=15)
|
19 |
+
except subprocess.CalledProcessError as e:
|
20 |
+
raise Exception(f"❌ Error installing package {package}: {e}")
|
21 |
+
except subprocess.TimeoutExpired:
|
22 |
+
raise Exception(f"⏰ Timed out installing package {package}")
|
23 |
+
|
24 |
+
def _execute_code(self, code, inputs):
|
25 |
+
temp_file = f"{self.temp_dir.name}/user_code.py"
|
26 |
+
output_file = f"{self.temp_dir.name}/stdout.txt"
|
27 |
+
error_file = f"{self.temp_dir.name}/stderr.txt"
|
28 |
+
|
29 |
+
with open(temp_file, "w") as f:
|
30 |
+
f.write(code)
|
31 |
+
|
32 |
+
with open(output_file, "w") as out, open(error_file, "w") as err:
|
33 |
+
try:
|
34 |
+
process = subprocess.Popen(
|
35 |
+
["python", temp_file],
|
36 |
+
stdin=subprocess.PIPE,
|
37 |
+
stdout=out,
|
38 |
+
stderr=err,
|
39 |
+
text=True
|
40 |
+
)
|
41 |
+
if inputs:
|
42 |
+
for line in inputs:
|
43 |
+
process.stdin.write(line + "\n")
|
44 |
+
process.stdin.close()
|
45 |
+
process.wait(timeout=15)
|
46 |
+
except subprocess.TimeoutExpired:
|
47 |
+
process.kill()
|
48 |
+
err.write("⏰ Execution timed out.\n")
|
49 |
+
except Exception:
|
50 |
+
err.write(traceback.format_exc())
|
51 |
+
|
52 |
+
with open(output_file, "r") as out, open(error_file, "r") as err:
|
53 |
+
output_text = out.read().strip()
|
54 |
+
error_text = err.read().strip()
|
55 |
+
|
56 |
+
if error_text:
|
57 |
+
return f"⚠️ Error:\n{error_text}"
|
58 |
+
return f"✅ Output:\n{output_text}"
|
59 |
+
|
60 |
+
def execute(self, code, inputs, packages):
|
61 |
+
if packages:
|
62 |
+
self._install_packages(packages.split(","))
|
63 |
+
future = self.executor.submit(self._execute_code, code, inputs)
|
64 |
+
try:
|
65 |
+
return future.result(timeout=20)
|
66 |
+
except concurrent.futures.TimeoutError:
|
67 |
+
return "⏳ Code execution exceeded time limit."
|
68 |
+
except Exception as e:
|
69 |
+
return f"❌ Execution Error: {e}"
|
70 |
+
|
71 |
+
def __del__(self):
|
72 |
+
self.executor.shutdown(wait=False)
|
73 |
+
shutil.rmtree(self.temp_dir.name)
|