mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 14:04:27 +01:00
* Pi's message. * Fix most everything. * Blacked * Add Typing, Docstrings everywhere, organize the code a bit. * Black * fix import * Update message, dedupe. * Increase backoff time. * bump up retries
126 lines
3.7 KiB
Python
126 lines
3.7 KiB
Python
"""Execute code in a Docker container"""
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
import docker
|
|
from docker.errors import ImageNotFound
|
|
|
|
WORKING_DIRECTORY = Path(__file__).parent.parent / "auto_gpt_workspace"
|
|
|
|
|
|
def execute_python_file(file: str):
|
|
"""Execute a Python file in a Docker container and return the output
|
|
|
|
Args:
|
|
file (str): The name of the file to execute
|
|
|
|
Returns:
|
|
str: The output of the file
|
|
"""
|
|
|
|
print(f"Executing file '{file}' in workspace '{WORKING_DIRECTORY}'")
|
|
|
|
if not file.endswith(".py"):
|
|
return "Error: Invalid file type. Only .py files are allowed."
|
|
|
|
file_path = os.path.join(WORKING_DIRECTORY, file)
|
|
|
|
if not os.path.isfile(file_path):
|
|
return f"Error: File '{file}' does not exist."
|
|
|
|
if we_are_running_in_a_docker_container():
|
|
result = subprocess.run(
|
|
f"python {file_path}", capture_output=True, encoding="utf8", shell=True
|
|
)
|
|
if result.returncode == 0:
|
|
return result.stdout
|
|
else:
|
|
return f"Error: {result.stderr}"
|
|
|
|
try:
|
|
client = docker.from_env()
|
|
|
|
image_name = "python:3.10"
|
|
try:
|
|
client.images.get(image_name)
|
|
print(f"Image '{image_name}' found locally")
|
|
except ImageNotFound:
|
|
print(f"Image '{image_name}' not found locally, pulling from Docker Hub")
|
|
# Use the low-level API to stream the pull response
|
|
low_level_client = docker.APIClient()
|
|
for line in low_level_client.pull(image_name, stream=True, decode=True):
|
|
# Print the status and progress, if available
|
|
status = line.get("status")
|
|
progress = line.get("progress")
|
|
if status and progress:
|
|
print(f"{status}: {progress}")
|
|
elif status:
|
|
print(status)
|
|
|
|
# You can replace 'python:3.8' with the desired Python image/version
|
|
# You can find available Python images on Docker Hub:
|
|
# https://hub.docker.com/_/python
|
|
container = client.containers.run(
|
|
image_name,
|
|
f"python {file}",
|
|
volumes={
|
|
os.path.abspath(WORKING_DIRECTORY): {
|
|
"bind": "/workspace",
|
|
"mode": "ro",
|
|
}
|
|
},
|
|
working_dir="/workspace",
|
|
stderr=True,
|
|
stdout=True,
|
|
detach=True,
|
|
)
|
|
|
|
container.wait()
|
|
logs = container.logs().decode("utf-8")
|
|
container.remove()
|
|
|
|
# print(f"Execution complete. Output: {output}")
|
|
# print(f"Logs: {logs}")
|
|
|
|
return logs
|
|
|
|
except Exception as e:
|
|
return f"Error: {str(e)}"
|
|
|
|
|
|
def execute_shell(command_line: str) -> str:
|
|
"""Execute a shell command and return the output
|
|
|
|
Args:
|
|
command_line (str): The command line to execute
|
|
|
|
Returns:
|
|
str: The output of the command
|
|
"""
|
|
current_dir = os.getcwd()
|
|
|
|
if WORKING_DIRECTORY not in current_dir: # Change dir into workspace if necessary
|
|
work_dir = os.path.join(os.getcwd(), WORKING_DIRECTORY)
|
|
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() -> bool:
|
|
"""Check if we are running in a Docker container
|
|
|
|
Returns:
|
|
bool: True if we are running in a Docker container, False otherwise
|
|
"""
|
|
return os.path.exists("/.dockerenv")
|