-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_environment.py
More file actions
67 lines (54 loc) · 2.09 KB
/
Copy pathpython_environment.py
File metadata and controls
67 lines (54 loc) · 2.09 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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import subprocess
import os
import json
import sys
app = FastAPI(title="VM Python Execution Environment")
# Directory for storing Python scripts
CODE_DIR = "./code_repository"
os.makedirs(CODE_DIR, exist_ok=True)
class CodeUpdateRequest(BaseModel):
code: str
class ExecutionRequest(BaseModel):
input_value: int
@app.get("/files/{filename}")
def get_code(filename: str):
"""Fetch the current code from the repository"""
filepath = os.path.join(CODE_DIR, filename)
if not os.path.exists(filepath):
raise HTTPException(status_code=404, detail="File not found")
with open(filepath, "r") as f:
return {"filename": filename, "code": f.read()}
@app.put("/files/{filename}")
def update_code(filename: str, request: CodeUpdateRequest):
"""Update the Python script in the repository"""
filepath = os.path.join(CODE_DIR, filename)
with open(filepath, "w") as f:
f.write(request.code)
return {"message": "Code updated successfully", "filename": filename}
@app.post("/execute/{filename}")
def execute_code(filename: str):
"""Execute a script and capture all printed output"""
filepath = os.path.join(CODE_DIR, filename)
if not os.path.exists(filepath):
raise HTTPException(status_code=404, detail="File not found")
sys.path.insert(0, CODE_DIR) # Ensure Python looks in the correct directory
module_name = filename[:-3] # Remove .py extension
try:
# Execute the module and capture all printed output
result = subprocess.run([
"python", "-c",
f"import sys; sys.path.insert(0, '{CODE_DIR}'); import {module_name}"
], capture_output=True, text=True, timeout=5)
return {
"stdout": result.stdout.strip(),
"stderr": result.stderr.strip()
}
except subprocess.TimeoutExpired:
return {"error": "Execution timed out"}
except Exception as e:
return {"error": str(e)}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9888)