2026-04-06 21:14:27 +02:00

41 lines
1.3 KiB
Python

import subprocess
from pathlib import Path
RUN_TIMEOUT = 30 # seconds
class ExecutionEngine:
def __init__(self):
pass
def run_code(self, active_file: Path) -> dict:
suffix = active_file.suffix
current_dir = active_file.parent.resolve()
if suffix == ".py":
cmd = ["py", active_file.name]
elif suffix == ".tex":
cmd = [
"pdflatex",
"-interaction=nonstopmode",
f"-output-directory={current_dir}",
active_file.name,
]
else:
return {"stdout": "", "stderr": f"Unsupported file type: {suffix}", "rc": 1}
try:
proc = subprocess.run(
cmd,
cwd=current_dir,
capture_output=True,
text=True,
timeout=RUN_TIMEOUT,
)
return {"stdout": proc.stdout, "stderr": proc.stderr, "rc": proc.returncode}
except subprocess.TimeoutExpired:
return {"stdout": "", "stderr": f"Timed out after {RUN_TIMEOUT}s", "rc": -1}
except FileNotFoundError as e:
return {"stdout": "", "stderr": str(e), "rc": -1}
except Exception as e:
return {"stdout": "", "stderr": str(e), "rc": -1}