-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
55 lines (43 loc) · 1.45 KB
/
Copy pathexecutor.py
File metadata and controls
55 lines (43 loc) · 1.45 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
import runpy
import sys
import json
RESULT_PREFIX = "___RESULT_JSON___:"
def main():
"""
Wrapper executed inside nsjail.
- Loads the user script from the given path.
- Retrieves and calls main().
- Ensures the return value is JSON-serializable.
- Prints a single RESULT_PREFIX + JSON line to stdout.
"""
if len(sys.argv) != 2:
print("Usage: executor.py <path_to_script>", file=sys.stderr)
sys.exit(1)
script_path = sys.argv[1]
# Execute the user's script in its own global namespace
try:
script_globals = runpy.run_path(script_path)
except Exception as e:
print(f"Error while executing user script: {e}", file=sys.stderr)
sys.exit(1)
# Get the main() function
user_main = script_globals.get("main")
if not callable(user_main):
print("Error: Script must define a callable main()", file=sys.stderr)
sys.exit(1)
# Call main()
try:
result = user_main()
except Exception as e:
print(f"Error while running main(): {e}", file=sys.stderr)
sys.exit(1)
# Ensure it's JSON-serializable
try:
json_str = json.dumps(result)
except TypeError as e:
print(f"Error: main() must return JSON-serializable object: {e}", file=sys.stderr)
sys.exit(1)
# Print the result with the defined prefix to parse it in the endpoint
print(f"{RESULT_PREFIX}{json_str}")
if __name__ == "__main__":
main()