Feat/better log: Add colorize function and TermColor enum for text coloring (#5410)

Co-authored-by: Engel Nyst <enyst@users.noreply.github.com>
This commit is contained in:
Cheng Yang
2024-12-08 05:30:40 +08:00
committed by GitHub
parent 6972f4806f
commit 424cdf121a
4 changed files with 51 additions and 24 deletions

View File

@@ -9,13 +9,13 @@ class RuntimeBuilder(abc.ABC):
tags: list[str],
platform: str | None = None,
) -> str:
"""
Build the runtime image.
"""Build the runtime image.
Args:
path (str): The path to the runtime image's build directory.
tags (list[str]): The tags to apply to the runtime image (e.g., ["repo:my-repo", "sha:my-sha"]).
platform (str, optional): The target platform for the build. Defaults to None.
Returns:
str: The name:tag of the runtime image after build (e.g., "repo:sha").
This can be different from the tags input if the builder chooses to mutate the tags (e.g., adding a
@@ -28,8 +28,7 @@ class RuntimeBuilder(abc.ABC):
@abc.abstractmethod
def image_exists(self, image_name: str, pull_from_repo: bool = True) -> bool:
"""
Check if the runtime image exists.
"""Check if the runtime image exists.
Args:
image_name (str): The name of the runtime image (e.g., "repo:sha").

View File

@@ -9,6 +9,7 @@ from openhands import __version__ as oh_version
from openhands.core.logger import RollingLogger
from openhands.core.logger import openhands_logger as logger
from openhands.runtime.builder.base import RuntimeBuilder
from openhands.utils.term_color import TermColor, colorize
class DockerRuntimeBuilder(RuntimeBuilder):
@@ -187,7 +188,9 @@ class DockerRuntimeBuilder(RuntimeBuilder):
return True
except docker.errors.ImageNotFound:
if not pull_from_repo:
logger.debug(f'Image {image_name} not found locally')
logger.debug(
f'Image {image_name} {colorize("not found", TermColor.WARNING)} locally'
)
return False
try:
logger.debug(
@@ -214,7 +217,7 @@ class DockerRuntimeBuilder(RuntimeBuilder):
logger.debug('Could not find image locally or in registry.')
return False
except Exception as e:
msg = 'Image could not be pulled: '
msg = f'Image {colorize("could not be pulled", TermColor.ERROR)}: '
ex_msg = str(e)
if 'Not Found' in ex_msg:
msg += 'image not found in registry.'
@@ -286,8 +289,7 @@ class DockerRuntimeBuilder(RuntimeBuilder):
logger.debug(current_line['status'])
def _prune_old_cache_files(self, cache_dir: str, max_age_days: int = 7) -> None:
"""
Prune cache files older than the specified number of days.
"""Prune cache files older than the specified number of days.
Args:
cache_dir (str): The path to the cache directory.
@@ -311,8 +313,7 @@ class DockerRuntimeBuilder(RuntimeBuilder):
logger.warning(f'Error during build cache pruning: {e}')
def _is_cache_usable(self, cache_dir: str) -> bool:
"""
Check if the cache directory is usable (exists and is writable).
"""Check if the cache directory is usable (exists and is writable).
Args:
cache_dir (str): The path to the cache directory.