From 144ace119bc320927371f0bad7fc41fb566def12 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Sun, 17 Jun 2018 15:22:25 +0100 Subject: [PATCH] Improve unicode support Printed job urls with unicode are now clickable Avoids unicode decode errors in logging due on systems, likely python27 systems where default encoding is ascii. Change-Id: I17041de05a4388c7b95a9ca04477043424458c67 --- jenkins_jobs/builder.py | 4 +++- jenkins_jobs/cli/entry.py | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/jenkins_jobs/builder.py b/jenkins_jobs/builder.py index bb2ae6e89..7beab9400 100644 --- a/jenkins_jobs/builder.py +++ b/jenkins_jobs/builder.py @@ -22,6 +22,7 @@ import logging import os from pprint import pformat import re +from six.moves.urllib.parse import quote import time import xml.etree.ElementTree as XML @@ -116,7 +117,8 @@ class JenkinsManager(object): # returns job name or url based on config option if self._jjb_config.builder['print_job_urls']: return self._jjb_config.jenkins['url'] + \ - '/job/' + '/job/'.join(job_name.split('/')) + '/' + '/job/' + quote( + '/job/'.join(job_name.split('/')).encode('utf8')) + '/' else: return job_name diff --git a/jenkins_jobs/cli/entry.py b/jenkins_jobs/cli/entry.py index 62e7b804e..7ba9618b4 100644 --- a/jenkins_jobs/cli/entry.py +++ b/jenkins_jobs/cli/entry.py @@ -17,7 +17,6 @@ import io import os import logging import platform -import sys from stevedore import extension import yaml @@ -141,6 +140,18 @@ class JenkinsJobs(object): def main(): + + # utf-8 workaround for avoiding unicode errors in stdout/stderr + # see https://stackoverflow.com/a/2001767/99834 + import codecs + import sys + + reload(sys) + sys.setdefaultencoding('utf-8') + sys.stdout = codecs.getwriter('utf8')(sys.stdout) + sys.stderr = codecs.getwriter('utf8')(sys.stderr) + # end of workaround + argv = sys.argv[1:] jjb = JenkinsJobs(argv) jjb.execute()