Skip to content

Commit 618bd25

Browse files
authored
Merge pull request #209 from shanejbrown/log-byte-chunks
Fix logging byte chunks
2 parents 6dd2448 + 9c9bf68 commit 618bd25

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

buildrunner/docker/runner.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,27 @@ def run(self, cmd, console=None, stream=True, log=None, workdir=None):
615615
self._run_log(log, output_buffer)
616616
elif hasattr(output_buffer, "next") or isinstance(output_buffer, GeneratorType):
617617
try:
618-
for line in output_buffer:
619-
self._run_log(console, line)
620-
self._run_log(log, line)
618+
# Buffer chunks into complete lines
619+
line_buffer = b""
620+
for chunk in output_buffer:
621+
if isinstance(chunk, str):
622+
chunk = chunk.encode("utf-8")
623+
line_buffer += chunk
624+
625+
# Process complete lines
626+
while b"\n" in line_buffer:
627+
line, line_buffer = line_buffer.split(b"\n", 1)
628+
if (
629+
line or line_buffer
630+
): # Don't log empty lines unless there's more content
631+
line_with_newline = line + b"\n"
632+
self._run_log(console, line_with_newline)
633+
self._run_log(log, line_with_newline)
634+
635+
# Process any remaining content in buffer
636+
if line_buffer:
637+
self._run_log(console, line_buffer)
638+
self._run_log(log, line_buffer)
621639
except socket.timeout:
622640
# Ignore timeouts since we check for the exit code anyways at the end
623641
pass

0 commit comments

Comments
 (0)