Fix deploy_logs unicode handling for Python 2 & 3 86/15886/1 v0.25.0
authorEric Ball <eball@linuxfoundation.org>
Thu, 13 Jun 2019 18:25:39 +0000 (11:25 -0700)
committerEric Ball <eball@linuxfoundation.org>
Thu, 13 Jun 2019 19:13:41 +0000 (12:13 -0700)
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 <eball@linuxfoundation.org>
lftools/deploy.py
releasenotes/notes/no-encode-py3-44307e6fd97c2d0c.yaml [new file with mode: 0644]

index 0addcc7..06ce721 100644 (file)
@@ -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 (file)
index 0000000..e9f2c60
--- /dev/null
@@ -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.