-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
51 lines (43 loc) · 1.49 KB
/
runner.py
File metadata and controls
51 lines (43 loc) · 1.49 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
import sys
import io
import json
import contextlib
import traceback
import pandas as pd
import numpy as np
import os
def execute_script(script_content):
# Create a string buffer to capture stdout
stdout_buffer = io.StringIO()
try:
# Context manager to trap stdout
with contextlib.redirect_stdout(stdout_buffer):
# Execute the string as Python code in the global scope
exec(script_content, globals())
# Check if main exists
if 'main' not in globals():
return {
"error": "Function 'main()' not found in script.",
"success": False
}
# Execute main and get result
result = main()
# Verify result is serializable (basic check) or a dict/list as implied by "JSON"
# The prompt implies main returns a JSON (dict/list in python terms)
return {
"result": result,
"stdout": stdout_buffer.getvalue(),
"success": True
}
except Exception as e:
return {
"error": str(e),
"traceback": traceback.format_exc(),
"success": False
}
if __name__ == "__main__":
# Read the script from stdin (passed by the parent process)
script_input = sys.stdin.read()
output = execute_script(script_input)
# Print the final result as a JSON string to stdout for the parent app to read
print(json.dumps(output))