replace 50+ occurrences of print() with logger (#3056)

Co-authored-by: James Collins <collijk@uw.edu>
Co-authored-by: Luke Kyohere <lkyohere@mfsafrica.com>
Co-authored-by: k-boikov <64261260+k-boikov@users.noreply.github.com>
Co-authored-by: Media <12145726+rihp@users.noreply.github.com>
Co-authored-by: Nicholas Tindle <nick@ntindle.com>
This commit is contained in:
Richard Beales
2023-04-30 05:40:57 +01:00
committed by GitHub
parent 6997bb0bdd
commit 06ae4684c8
20 changed files with 134 additions and 139 deletions

View File

@@ -8,6 +8,7 @@ from docker.errors import ImageNotFound
from autogpt.commands.command import command
from autogpt.config import Config
from autogpt.logs import logger
CFG = Config()
@@ -22,7 +23,7 @@ def execute_python_file(filename: str) -> str:
Returns:
str: The output of the file
"""
print(f"Executing file '{filename}'")
logger.info(f"Executing file '{filename}'")
if not filename.endswith(".py"):
return "Error: Invalid file type. Only .py files are allowed."
@@ -47,9 +48,11 @@ def execute_python_file(filename: str) -> str:
image_name = "python:3-alpine"
try:
client.images.get(image_name)
print(f"Image '{image_name}' found locally")
logger.warn(f"Image '{image_name}' found locally")
except ImageNotFound:
print(f"Image '{image_name}' not found locally, pulling from Docker Hub")
logger.info(
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):
@@ -57,9 +60,9 @@ def execute_python_file(filename: str) -> str:
status = line.get("status")
progress = line.get("progress")
if status and progress:
print(f"{status}: {progress}")
logger.info(f"{status}: {progress}")
elif status:
print(status)
logger.info(status)
container = client.containers.run(
image_name,
f"python {Path(filename).relative_to(CFG.workspace_path)}",
@@ -85,7 +88,7 @@ def execute_python_file(filename: str) -> str:
return logs
except docker.errors.DockerException as e:
print(
logger.warn(
"Could not run the script in a container. If you haven't already, please install Docker https://docs.docker.com/get-docker/"
)
return f"Error: {str(e)}"
@@ -118,7 +121,9 @@ def execute_shell(command_line: str) -> str:
if not current_dir.is_relative_to(CFG.workspace_path):
os.chdir(CFG.workspace_path)
print(f"Executing command '{command_line}' in working directory '{os.getcwd()}'")
logger.info(
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}"
@@ -154,7 +159,9 @@ def execute_shell_popen(command_line) -> str:
if CFG.workspace_path not in current_dir:
os.chdir(CFG.workspace_path)
print(f"Executing command '{command_line}' in working directory '{os.getcwd()}'")
logger.info(
f"Executing command '{command_line}' in working directory '{os.getcwd()}'"
)
do_not_show_output = subprocess.DEVNULL
process = subprocess.Popen(