1818from pathlib import Path
1919import tarfile
2020from types import GeneratorType
21- from typing import Optional
21+ from typing import Optional , Union
2222
2323from docker .utils import compare_version
2424from retry import retry
3131 force_remove_container ,
3232 BuildRunnerContainerError ,
3333)
34- from buildrunner .loggers import ContainerLogger , DockerPullProgress
34+ from buildrunner .loggers import ConsoleLogger , ContainerLogger , DockerPullProgress
3535from buildrunner .utils import (
3636 acquire_flock_open_read_binary ,
3737 acquire_flock_open_write_binary ,
@@ -75,7 +75,13 @@ def __init__(self, image_name, pull_image=True, platform=None):
7575 self .pull_image = pull_image
7676 self .platform = platform
7777
78- def __init__ (self , image_config , dockerd_url = None , log = None ):
78+ def __init__ (
79+ self ,
80+ image_config ,
81+ dockerd_url = None ,
82+ log : Union [ContainerLogger , None ] = None ,
83+ run_log_debug : bool = False ,
84+ ):
7985 image_name = image_config .image_name
8086 pull_image = image_config .pull_image
8187 image_platform = image_config .platform
@@ -91,6 +97,7 @@ def __init__(self, image_config, dockerd_url=None, log=None):
9197 # Disable timeouts for running commands
9298 timeout = 0 ,
9399 )
100+ self .run_log_debug = run_log_debug
94101 self .container = None
95102 self .shell = None
96103 self .committed_image = None
@@ -131,6 +138,21 @@ def __init__(self, image_config, dockerd_url=None, log=None):
131138 if log :
132139 log .write ("\n Image pulled successfully\n " )
133140
141+ def _run_log (
142+ self ,
143+ log : Union [ConsoleLogger , ContainerLogger , None ],
144+ output : Union [str , bytes ],
145+ ):
146+ """
147+ Log a message to the given log stream at a debug or info level depending on configuration.
148+ """
149+ if log :
150+ for line in log .clean_output (output ).split ("\n " ):
151+ if self .run_log_debug :
152+ log .debug (line )
153+ else :
154+ log .info (line )
155+
134156 def start (
135157 self ,
136158 shell = "/bin/sh" ,
@@ -154,7 +176,7 @@ def start(
154176 cap_add = None ,
155177 privileged = False ,
156178 network = None ,
157- ): # pylint: disable=too-many-arguments,too-many-locals
179+ ):
158180 """
159181 Kwargs:
160182 volumes (dict): mount the local dir (key) to the given container
@@ -549,7 +571,6 @@ def save_caches(
549571 f"It will not be saved again to `{ local_cache_archive_file } `\n "
550572 )
551573
552- # pylint: disable=too-many-branches,too-many-arguments
553574 def run (self , cmd , console = None , stream = True , log = None , workdir = None ):
554575 """
555576 Run the given command in the container.
@@ -577,8 +598,7 @@ def run(self, cmd, console=None, stream=True, log=None, workdir=None):
577598 "Cannot call run if container cmd not shell"
578599 )
579600
580- if log :
581- log .write (f"Executing: { cmdv } \n " )
601+ self ._run_log (log , f"Executing: { cmdv } " )
582602
583603 create_res = self .docker_client .exec_create (
584604 self .container ["Id" ],
@@ -591,17 +611,13 @@ def run(self, cmd, console=None, stream=True, log=None, workdir=None):
591611 stream = stream ,
592612 )
593613 if isinstance (output_buffer , (bytes , str )):
594- if console :
595- console .write (output_buffer )
596- if log :
597- log .write (output_buffer )
614+ self ._run_log (console , output_buffer )
615+ self ._run_log (log , output_buffer )
598616 elif hasattr (output_buffer , "next" ) or isinstance (output_buffer , GeneratorType ):
599617 try :
600618 for line in output_buffer :
601- if console :
602- console .write (line )
603- if log :
604- log .write (line )
619+ self ._run_log (console , line )
620+ self ._run_log (log , line )
605621 except socket .timeout :
606622 # Ignore timeouts since we check for the exit code anyways at the end
607623 pass
0 commit comments