[elbe-devel] [PATCH 4/5] Python3: convert bytes to unicode

Christian Teklenborg chris at linutronix.de
Fri Dec 6 14:08:48 CET 2019


changed the byte strings to unicode strings and vice-versa to make it Python2
and Python3 compatible

Signed-off-by: Christian Teklenborg <chris at linutronix.de>
---
 elbepack/commands/init.py      |  6 +++---
 elbepack/daemons/soap/esoap.py | 10 +++++-----
 elbepack/initvmaction.py       |  2 +-
 elbepack/kvm.py                |  1 +
 elbepack/log.py                |  2 +-
 elbepack/shellhelper.py        |  2 +-
 elbepack/soapclient.py         |  4 ++--
 7 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/elbepack/commands/init.py b/elbepack/commands/init.py
index 46cd8dcd..90429943 100644
--- a/elbepack/commands/init.py
+++ b/elbepack/commands/init.py
@@ -150,9 +150,9 @@ def run_command(argv):
 
         # this is a workaround for
         # http://lists.linutronix.de/pipermail/elbe-devel/2017-July/000541.html
-        _, virt = command_out('test -x /usr/bin/systemd-detect-virt && '
-                              '/usr/bin/systemd-detect-virt')
-        _, dist = command_out('lsb_release -cs')
+        virt = command_out('test -x /usr/bin/systemd-detect-virt && '
+                              '/usr/bin/systemd-detect-virt')[1].decode()
+        dist = command_out('lsb_release -cs')[1].decode()
 
         if 'vmware' in virt and 'stretch' in dist:
             machine_type = 'pc-i440fx-2.6'
diff --git a/elbepack/daemons/soap/esoap.py b/elbepack/daemons/soap/esoap.py
index 468df8a5..450abf0e 100644
--- a/elbepack/daemons/soap/esoap.py
+++ b/elbepack/daemons/soap/esoap.py
@@ -160,7 +160,7 @@ class ESoap (ServiceBase):
             return -2
 
         with open(fn, 'a') as fp:
-            fp.write(binascii.a2b_base64(blob))
+            fp.write(binascii.a2b_base64(blob).decode())
 
         return part + 1
 
@@ -265,7 +265,7 @@ class ESoap (ServiceBase):
 
         # Now append data to cdrom_file
         fp = open(cdrom_fname, "a")
-        fp.write(binascii.a2b_base64(data))
+        fp.write(binascii.a2b_base64(data).decode())
         fp.close()
 
     @rpc(String)
@@ -298,7 +298,7 @@ class ESoap (ServiceBase):
 
         # Now write empty File
         fp = open(pdebuild_fname, "a")
-        fp.write(binascii.a2b_base64(data))
+        fp.write(binascii.a2b_base64(data).decode())
         fp.close()
 
     @rpc(String, Integer, String)
@@ -332,7 +332,7 @@ class ESoap (ServiceBase):
 
         # Now append to File
         fp = open(orig_fname, "a")
-        fp.write(binascii.a2b_base64(data))
+        fp.write(binascii.a2b_base64(data).decode())
         fp.close()
 
     @rpc(String)
@@ -362,7 +362,7 @@ class ESoap (ServiceBase):
     @soap_faults
     def create_project(self, uid, xml, url_validation):
         with NamedTemporaryFile() as fp:
-            fp.write(binascii.a2b_base64(xml))
+            fp.write(binascii.a2b_base64(xml).decode())
             fp.flush()
             prjid = self.app.pm.create_project(
                 uid, fp.name, url_validation=url_validation)
diff --git a/elbepack/initvmaction.py b/elbepack/initvmaction.py
index f8572402..75fa81a6 100644
--- a/elbepack/initvmaction.py
+++ b/elbepack/initvmaction.py
@@ -234,7 +234,7 @@ def submit_and_dl_result(xmlfile, cdrom, opt):
                 print("Giving up", file=sys.stderr)
                 sys.exit(20)
 
-            prjdir = prjdir.strip()
+            prjdir = prjdir.strip().decode()
 
             cmd = '%s control set_xml %s %s' % (elbe_exe, prjdir, xmlfile)
             ret, _, err = command_out_stderr(cmd)
diff --git a/elbepack/kvm.py b/elbepack/kvm.py
index 62f2ddeb..58a17530 100644
--- a/elbepack/kvm.py
+++ b/elbepack/kvm.py
@@ -24,6 +24,7 @@ def find_kvm_exe():
             cmd = subprocess.Popen(
                 fname + ' --version',
                 shell=True,
+                universal_newlines=True,
                 stdout=subprocess.PIPE)
             for line in cmd.stdout:
                 if "version" in line:
diff --git a/elbepack/log.py b/elbepack/log.py
index 43e04985..67bac91f 100644
--- a/elbepack/log.py
+++ b/elbepack/log.py
@@ -241,7 +241,7 @@ class AsyncLogging(object):
 
     def run(self):
         alive = True
-        rest = ""
+        rest = b""
         while alive:
             events = self.epoll.poll()
             for _, event in events:
diff --git a/elbepack/shellhelper.py b/elbepack/shellhelper.py
index 0895c0b0..955abb0b 100644
--- a/elbepack/shellhelper.py
+++ b/elbepack/shellhelper.py
@@ -106,7 +106,7 @@ def do(cmd, allow_fail=False, stdin=None, env_add=None):
     if stdin is None:
         p = Popen(cmd, shell=True, stdout=w, stderr=STDOUT, env=new_env)
     else:
-        p = Popen(cmd, shell=True, stdin=PIPE, stdout=w, stderr=STDOUT, env=new_env)
+        p = Popen(cmd, shell=True, stdin=PIPE, stdout=w, stderr=STDOUT, env=new_env, universal_newlines=True)
 
     async_logging(r, w, soap, log)
     p.communicate(input=stdin)
diff --git a/elbepack/soapclient.py b/elbepack/soapclient.py
index 62324cd0..c64926ef 100644
--- a/elbepack/soapclient.py
+++ b/elbepack/soapclient.py
@@ -328,7 +328,7 @@ class SetXmlAction(ClientAction):
         part = 0
         with open(filename, "rb") as fp:
             while True:
-                xml_base64 = binascii.b2a_base64(fp.read(size))
+                xml_base64 = binascii.b2a_base64(fp.read(size)).decode()
                 # finish upload
                 if len(xml_base64) == 1:
                     part = client.service.upload_file(builddir,
@@ -519,7 +519,7 @@ class DumpFileAction(ClientAction):
             if ret == "EndOfFile":
                 return
 
-            sys.stdout.write(binascii.a2b_base64(ret))
+            sys.stdout.write(binascii.a2b_base64(ret).decode())
             part = part + 1
 
 
-- 
2.20.1




More information about the elbe-devel mailing list