From 29d8fa5d38f02c3c394d037aa0601368e5b3453d Mon Sep 17 00:00:00 2001 From: Torantulino Date: Sat, 1 Apr 2023 14:37:50 +0100 Subject: [PATCH] First imeplementation of execute_code. This is too limited by it's sandboxing by RestrictedPython. --- scripts/execute_code.py | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 scripts/execute_code.py diff --git a/scripts/execute_code.py b/scripts/execute_code.py new file mode 100644 index 0000000000..e6fc544322 --- /dev/null +++ b/scripts/execute_code.py @@ -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, '', '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)}" \ No newline at end of file