Skip to content

Commit

Permalink
[TECH-272] Decode logs with original encoding
Browse files Browse the repository at this point in the history
branch: feature/TECH-272-upgrade-monorepo
  • Loading branch information
SamTheisens committed May 15, 2023
1 parent 274dbfd commit 05dc610
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/mpyl/utilities/jenkins/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 05dc610

Please sign in to comment.