From: Eric Ball Date: Thu, 13 Jun 2019 18:25:39 +0000 (-0700) Subject: Fix deploy_logs unicode handling for Python 2 & 3 X-Git-Tag: v0.25.0^0 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=8e4cfd42d3fbe974c98aebb52d491c8d84050e03;p=releng%2Flftools.git Fix deploy_logs unicode handling for Python 2 & 3 six.text_type will always return a utf-8 encoded string, while str.encode will return a bytes object in Python 3. io.open is used to ensure that the file is being opened for utf-8 writing in Python 2. decode('utf-8') is used on the requests response due to how requests encodes unicode responses. Change-Id: If58f8a3b9617f0be4effc58414d22f0cf09946cb Signed-off-by: Eric Ball --- diff --git a/lftools/deploy.py b/lftools/deploy.py index 0addcc7e..06ce721c 100644 --- a/lftools/deploy.py +++ b/lftools/deploy.py @@ -13,6 +13,7 @@ from datetime import timedelta import errno import gzip +import io import logging import multiprocessing from multiprocessing.dummy import Pool as ThreadPool @@ -28,6 +29,7 @@ import zipfile from defusedxml.minidom import parseString import glob2 # Switch to glob when Python < 3.5 support is dropped import requests +import six log = logging.getLogger(__name__) @@ -332,12 +334,12 @@ def deploy_logs(nexus_url, nexus_path, build_url): log.info(MAGIC_STRING) resp = requests.get('{}/consoleText'.format(_format_url(build_url))) - with open('console.log', 'w+') as f: - f.write(str(resp.text.split(MAGIC_STRING)[0].encode('utf-8'))) + with io.open('console.log', 'w+', encoding='utf-8') as f: + f.write(six.text_type(resp.content.decode('utf-8').split(MAGIC_STRING)[0])) resp = requests.get('{}/timestamps?time=HH:mm:ss&appendLog'.format(_format_url(build_url))) - with open('console-timestamp.log', 'w+') as f: - f.write(str(resp.text.split(MAGIC_STRING)[0].encode('utf-8'))) + with io.open('console-timestamp.log', 'w+', encoding='utf-8') as f: + f.write(six.text_type(resp.content.decode('utf-8').split(MAGIC_STRING)[0])) _compress_text(work_dir) diff --git a/releasenotes/notes/no-encode-py3-44307e6fd97c2d0c.yaml b/releasenotes/notes/no-encode-py3-44307e6fd97c2d0c.yaml new file mode 100644 index 00000000..e9f2c608 --- /dev/null +++ b/releasenotes/notes/no-encode-py3-44307e6fd97c2d0c.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Unicode compatibility in deploy_logs for Python 2 and 3 was improved in + several ways. The former method to pull and write log files did not work + properly in Python 3, and was not very robust for Python 2. Both reading + and writing logs is now handled in a unicode-safe, 2/3 compatible way.