diff mbox

[03/13] autobuild-run, python3: bytes<>str, decode()

Message ID 1424899050-24932-4-git-send-email-dywi@mailerd.de
State Accepted
Headers show

Commit Message

André Erdmann Feb. 25, 2015, 9:17 p.m. UTC
urlopen.read[lines]() returns bytes in py3 and text in py2.
Decode the bytes if necessary.

Py2k compatibility: no-op decode_bytes(), decode_byte_list() functions

Note: The Py2k variant of the decode_byte_list() function returns the
      input list, whereas the python3 variant creates a new list.

Note2: This commit does not add encode() functions, which could be necessary
       when writing non-ascii chars to files -- in Python3 only.

Signed-off-by: André Erdmann <dywi@mailerd.de>
---
 scripts/autobuild-run | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/scripts/autobuild-run b/scripts/autobuild-run
index be2f482..3a3b8de 100755
--- a/scripts/autobuild-run
+++ b/scripts/autobuild-run
@@ -79,6 +79,19 @@  else:
 
 urlopen = _urllib.urlopen
 
+if sys.hexversion >= 0x3000000:
+    def decode_bytes(b):
+        return b.decode()
+
+    def decode_byte_list(bl):
+        return [b.decode() for b in bl]
+else:
+    def _identity(e):
+        return e
+
+    decode_bytes = _identity
+    decode_byte_list = _identity
+
 MAX_DURATION = 60 * 60 * 4
 VERSION = 1
 
@@ -88,7 +101,7 @@  def log_write(logf, msg):
 
 def check_version():
     r = urlopen('http://autobuild.buildroot.org/version')
-    version = int(r.readline().strip())
+    version = int(decode_bytes(r.readline()).strip())
     if version > VERSION:
         print("ERROR: script version too old, please upgrade.")
         sys.exit(1)
@@ -143,7 +156,7 @@  def get_toolchain_configs():
     """
 
     r = urlopen('http://autobuild.buildroot.org/toolchains/configs/toolchain-configs.csv')
-    l = r.readlines()
+    l = decode_byte_list(r.readlines())
     configs = []
     for row in csv.reader(l):
         config = {}
@@ -158,7 +171,7 @@  def get_toolchain_configs():
             continue
         config["libc"] = row[2]
         r = urlopen(config["url"])
-        config["contents"] = r.readlines()
+        config["contents"] = decode_byte_list(r.readlines())
         configs.append(config)
     return configs