From 05dc61059c6669f7796a6cb32e580fab4bebd501 Mon Sep 17 00:00:00 2001 From: SamTheisens <1911436+SamTheisens@users.noreply.github.com> Date: Mon, 15 May 2023 13:31:56 +0800 Subject: [PATCH] [TECH-272] Decode logs with original encoding branch: feature/TECH-272-upgrade-monorepo --- src/mpyl/utilities/jenkins/runner.py | 33 +++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/mpyl/utilities/jenkins/runner.py b/src/mpyl/utilities/jenkins/runner.py index 8a73420b5..9e4da06d8 100644 --- a/src/mpyl/utilities/jenkins/runner.py +++ b/src/mpyl/utilities/jenkins/runner.py @@ -8,6 +8,7 @@ import requests from jenkinsapi.build import Build +from jenkinsapi.custom_exceptions import JenkinsAPIException from jenkinsapi.jenkins import Jenkins from jenkinsapi.job import Job from rich.errors import MarkupError @@ -19,6 +20,36 @@ from . import Pipeline +def stream_utf_8_logs(self, interval=0): + """ + Return generator which streams parts of text console. + Workaround for https://github.com/pycontribs/jenkinsapi/pull/843 + """ + url = f"{self.baseurl}/logText/progressiveText" + size = 0 + more_data = True + while more_data: + resp = self.job.jenkins.requester.get_url( + url, params={"start": size} + ) + content = resp.content + if content: + if isinstance(content, str): + yield content + elif isinstance(content, bytes): + yield content.decode(resp.encoding) + else: + raise JenkinsAPIException( + "Unknown content type for console" + ) + size = resp.headers["X-Text-Size"] + more_data = resp.headers.get("X-More-Data") + time.sleep(interval) + + +Build.stream_utf_8_logs = stream_utf_8_logs + + @dataclass class JenkinsRunner: pipeline: Pipeline @@ -79,7 +110,7 @@ def cancel_handler(_sig, _frame): build_to_follow.stop() signal.signal(signal.SIGINT, cancel_handler) - for line in build_to_follow.stream_logs(): + for line in build_to_follow.stream_utf_8_logs(): current_time = time.time() elapsed_time = current_time - start_time lines = line.rstrip().split('\n')