mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 14:04:27 +01:00
First imeplementation of execute_code.
This is too limited by it's sandboxing by RestrictedPython.
This commit is contained in:
47
scripts/execute_code.py
Normal file
47
scripts/execute_code.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
from io import StringIO
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
from RestrictedPython import compile_restricted, safe_globals
|
||||||
|
|
||||||
|
|
||||||
|
def execute_python_file(file):
|
||||||
|
workspace_folder = "auto_gpt_workspace"
|
||||||
|
|
||||||
|
if not file.endswith(".py"):
|
||||||
|
return "Error: Invalid file type. Only .py files are allowed."
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Prepend the workspace folder to the provided file name
|
||||||
|
file_path = os.path.join(workspace_folder, file)
|
||||||
|
|
||||||
|
# Check if the file exists
|
||||||
|
if not os.path.isfile(file_path):
|
||||||
|
return f"Error: File '{file}' does not exist."
|
||||||
|
|
||||||
|
# Read the content of the file
|
||||||
|
with open(file_path, 'r') as f:
|
||||||
|
code = f.read()
|
||||||
|
|
||||||
|
# Capture stdout and stderr
|
||||||
|
original_stdout = sys.stdout
|
||||||
|
original_stderr = sys.stderr
|
||||||
|
sys.stdout = StringIO()
|
||||||
|
sys.stderr = StringIO()
|
||||||
|
|
||||||
|
# Compile and execute the code in a restricted environment
|
||||||
|
try:
|
||||||
|
restricted_code = compile_restricted(code, '<inline>', 'exec')
|
||||||
|
exec(restricted_code, safe_globals)
|
||||||
|
except Exception as e:
|
||||||
|
result = f"Error while executing code:\n{traceback.format_exc()}"
|
||||||
|
else:
|
||||||
|
result = sys.stdout.getvalue()
|
||||||
|
|
||||||
|
# Restore original stdout and stderr
|
||||||
|
sys.stdout = original_stdout
|
||||||
|
sys.stderr = original_stderr
|
||||||
|
|
||||||
|
return result
|
||||||
|
except Exception as e:
|
||||||
|
return f"Error: {str(e)}"
|
||||||
Reference in New Issue
Block a user