import os import subprocess import docker from docker.errors import ImageNotFound import sys WORKSPACE_FOLDER = "auto_gpt_workspace" def execute_python_file(file): if not file.endswith(".py"): return "Error: Invalid file type. Only .py files are allowed." file_path = os.path.join(WORKSPACE_FOLDER, file) if not os.path.isfile(file_path): return f"Error: File '{file}' does not exist." result = subprocess.run([sys.executable, file_path], capture_output=True, encoding="utf8") if result.returncode == 0: return result.stdout else: return f"Error: {result.stderr}" def execute_shell(command_line): current_dir = os.getcwd() if WORKSPACE_FOLDER not in current_dir: # Change dir into workspace if necessary work_dir = os.path.join(os.getcwd(), WORKSPACE_FOLDER) os.chdir(work_dir) print(f"Executing command '{command_line}' in working directory '{os.getcwd()}'") result = subprocess.run(command_line, capture_output=True, shell=True) output = f"STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" # Change back to whatever the prior working dir was os.chdir(current_dir) return output def we_are_running_in_a_docker_container(): os.path.exists("/.dockerenv")