[X2Go-Commits] [python-x2go] 06/07: Correctly handle IO objects (BytesIO in Python3, StringIO in Python2).

git-admin at x2go.org git-admin at x2go.org
Tue Sep 18 18:45:39 CEST 2018


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository python-x2go.

commit 3fbee6ad5c04d3c49f73b4232f580248ba24714a
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date:   Tue Sep 18 16:26:40 2018 +0000

    Correctly handle IO objects (BytesIO in Python3, StringIO in Python2).
---
 x2go/backends/control/plain.py  | 57 ++++++++++++++++++++++++++--------------
 x2go/backends/terminal/plain.py | 58 +++++++++++++++++++++++++++++++----------
 x2go/inifiles.py                |  5 +++-
 3 files changed, 86 insertions(+), 34 deletions(-)

diff --git a/x2go/backends/control/plain.py b/x2go/backends/control/plain.py
index 104a25b..40c4a0f 100644
--- a/x2go/backends/control/plain.py
+++ b/x2go/backends/control/plain.py
@@ -473,7 +473,10 @@ class X2GoControlSession(paramiko.SSHClient):
 
         if self.session_died:
             self.logger("control session seams to be dead, not executing command ,,%s'' on X2Go server %s" % (_rerewrite_blanks(cmd), self.profile_name,), loglevel=loglevel)
-            return (io.StringIO(), io.StringIO(), io.StringIO(u'failed to execute command'))
+            if sys.version_info[0] >= 3:
+                return (io.BytesIO(), io.BytesIO(), io.BytesIO(b'failed to execute command'))
+            else:
+                return (io.StringIO(), io.StringIO(), io.StringIO(u'failed to execute command'))
 
         self._transport_lock.acquire()
 
@@ -531,13 +534,13 @@ class X2GoControlSession(paramiko.SSHClient):
         if self._transport_lock.locked():
             self._transport_lock.release()
 
-        # sanitized X2Go relevant data, protect against data injection via .bashrc files
-        (_stdin, _stdout, _stderr) = _retval
-        raw_stdout = _stdout.read()
-
         sanitized_stdout = u''
         is_x2go_data = False
 
+        # sanitized X2Go relevant data, protect against data injection via .bashrc files
+        (stdin, stdout, stderr) = _retval
+        raw_stdout = stdout.read()
+
         # Python 3 needs a decoding from bytestring to string
         if sys.version_info[0] >= 3:
             raw_stdout = raw_stdout.decode()
@@ -553,9 +556,12 @@ class X2GoControlSession(paramiko.SSHClient):
             if line.startswith('X2GODATAEND:'+cmd_uuid): break
             sanitized_stdout += line + "\n"
 
-        _stdout_new = io.StringIO(sanitized_stdout)
+        if sys.version_info[0] >= 3:
+            _stdout_new = io.BytesIO(sanitized_stdout.encode())
+        else:
+            _stdout_new = io.StringIO(sanitized_stdout)
 
-        _retval = (_stdin, _stdout_new, _stderr)
+        _retval = (stdin, _stdout_new, stderr)
         return _retval
 
     @property
@@ -602,7 +608,10 @@ class X2GoControlSession(paramiko.SSHClient):
         """
         if self._server_features is None:
             (stdin, stdout, stderr) = self._x2go_exec_command('which x2gofeaturelist >/dev/null && x2gofeaturelist')
-            self._server_features = stdout.read().split('\n')
+            _stdout = stdout.read()
+            if sys.version_info[0] >= 3:
+                _stdout = _stdout.decode()
+            self._server_features = _stdout.split('\n')
             self._server_features = [ f for f in self._server_features if f ]
             self._server_features.sort()
             self.logger('server-side X2Go features are: %s' % self._server_features, loglevel=log.loglevel_DEBUG)
@@ -632,9 +641,11 @@ class X2GoControlSession(paramiko.SSHClient):
         """
         if self._remote_home is None:
             (stdin, stdout, stderr) = self._x2go_exec_command('echo $HOME')
-            stdout_r = stdout.read()
-            if stdout_r:
-                self._remote_home = stdout_r.split()[0]
+            _stdout = stdout.read()
+            if sys.version_info[0] >= 3:
+                _stdout = _stdout.decode()
+            if _stdout:
+                self._remote_home = _stdout.split()[0]
                 self.logger('remote user\' home directory: %s' % self._remote_home, loglevel=log.loglevel_DEBUG)
             return self._remote_home
         else:
@@ -1261,8 +1272,10 @@ class X2GoControlSession(paramiko.SSHClient):
         :rtype: ``bool``
 
         """
-        (_stdin, _stdout, _stderr) = self._x2go_exec_command('stat -tL "%s"' % self._x2go_remote_home, loglevel=log.loglevel_DEBUG)
-        if _stdout.read():
+        (stdin, stdout, stderr) = self._x2go_exec_command('stat -tL "%s"' % self._x2go_remote_home, loglevel=log.loglevel_DEBUG)
+        _stdout = stdout.read()
+        print (_stdout)
+        if _stdout:
             return True
         return False
 
@@ -1653,8 +1666,10 @@ class X2GoControlSession(paramiko.SSHClient):
             timeout.start()
             try:
                 (stdin, stdout, stderr) = self._x2go_exec_command("export HOSTNAME && x2golistdesktops")
-                _stdout_read = stdout.read()
-                _listdesktops = _stdout_read.split('\n')
+                _stdout = stdout.read()
+                if sys.version_info[0] >= 3:
+                    _stdout = _stdout.decode()
+                _listdesktops = _stdout.split('\n')
             except gevent.timeout.Timeout:
                 # if we do not get a reply here after <maxwait> seconds we will raise a time out, we have to
                 # make sure that we catch this at places where we want to ignore timeouts (e.g. in the
@@ -1698,8 +1713,10 @@ class X2GoControlSession(paramiko.SSHClient):
             timeout.start()
             try:
                 (stdin, stdout, stderr) = self._x2go_exec_command("export HOSTNAME && x2golistmounts %s" % session_name)
-                _stdout_read = stdout.read()
-                _listmounts = {session_name: [ line for line in _stdout_read.split('\n') if line ] }
+                _stdout = stdout.read()
+                if sys.version_info[0] >= 3:
+                    _stdout = _stdout.decode()
+                _listmounts = {session_name: [ line for line in _stdout.split('\n') if line ] }
             except gevent.timeout.Timeout:
                 # if we do not get a reply here after <maxwait> seconds we will raise a time out, we have to
                 # make sure that we catch this at places where we want to ignore timeouts
@@ -1749,8 +1766,10 @@ class X2GoControlSession(paramiko.SSHClient):
                         (stdin, stdout, stderr) = self._x2go_exec_command("export HOSTNAME && { x2golistsessions; x2golistshadowsessions; }")
                     else:
                         (stdin, stdout, stderr) = self._x2go_exec_command("export HOSTNAME && x2golistsessions")
-                    _stdout_read = stdout.read()
-                    _listsessions = self._list_backend(_stdout_read, info_backend=self._info_backend).sessions
+                    _stdout = stdout.read()
+                    if sys.version_info[0] >= 3:
+                        _stdout = _stdout.decode()
+                    _listsessions = self._list_backend(_stdout, info_backend=self._info_backend).sessions
                     _success = True
                 except KeyError:
                     gevent.sleep(1)
diff --git a/x2go/backends/terminal/plain.py b/x2go/backends/terminal/plain.py
index df143d0..7717be6 100644
--- a/x2go/backends/terminal/plain.py
+++ b/x2go/backends/terminal/plain.py
@@ -892,13 +892,19 @@ class X2GoTerminalSession(object):
         _auth_rsakey = self.control_session._x2go_session_auth_rsakey
         _host_rsakey = defaults.RSAHostKey
 
-        _tmp_io_object = io.StringIO()
+        if sys.version_info[0] >= 3:
+            _tmp_io_object = io.BytesIO()
+        else:
+            _tmp_io_object = io.StringIO()
         _auth_rsakey.write_private_key(_tmp_io_object)
-        _tmp_io_object.write(u'----BEGIN RSA IDENTITY----')
-        _tmp_io_object.write(u'%s %s' % (_host_rsakey.get_name(),_host_rsakey.get_base64(),))
-
+        if sys.version_info[0] >= 3:
+            _tmp_io_object.write(b'----BEGIN RSA IDENTITY----')
+            _tmp_io_object.write(b'%b %b' % (_host_rsakey.get_name().encode(),_host_rsakey.get_base64().encode(),))
+        else:
+            _tmp_io_object.write(u'----BEGIN RSA IDENTITY----')
+            _tmp_io_object.write(u'%s %s' % (_host_rsakey.get_name(),_host_rsakey.get_base64(),))
         # _x2go_key_fname must be a UniX path
-        _x2go_key_fname = u'%s/%s/%s' % (os.path.dirname(self.session_info.remote_container), 'ssh', 'key.z%s' % self.session_info.agent_pid)
+        _x2go_key_fname = '%s/%s/%s' % (os.path.dirname(self.session_info.remote_container), 'ssh', 'key.z%s' % self.session_info.agent_pid)
         _x2go_key_bundle = _tmp_io_object.getvalue()
 
         # if there is another call to this method currently being processed, wait for that one to finish
@@ -968,10 +974,15 @@ class X2GoTerminalSession(object):
                            ]
 
             (stdin, stdout, stderr) = self.control_session._x2go_exec_command(cmd_line)
-            _stdout = stdout.read().split('\n')
+            _stdout = stdout.read()
+            _stderr = stderr.read()
+            if sys.version_info[0] >= 3:
+                _stdout = _stdout.decode()
+                _stderr = _stderr.decode()
+            _stdout = _stdout.split('\n')
             if _stdout[0]:
                 self.logger('x2gomountdirs stdout is: %s' % _stdout, log.loglevel_NOTICE)
-            _stderr = stderr.read().split('\n')
+            _stderr = _stderr.split('\n')
             if _stderr[0]:
                 self.logger('x2gomountdirs stderr is: %s' % _stderr, log.loglevel_WARN)
 
@@ -1002,7 +1013,10 @@ class X2GoTerminalSession(object):
                    ]
 
         (stdin, stdout, stderr) = self.control_session._x2go_exec_command(cmd_line)
-        if not stderr.read():
+        _stderr = stderr.read()
+        if sys.version_info[0] >= 3:
+            _stderr = _stderr.decode()
+        if not _stderr:
             self.logger('x2goumount-session (all mounts) for session %s has been successful' % self.session_info, log.loglevel_NOTICE)
             return True
         else:
@@ -1028,7 +1042,10 @@ class X2GoTerminalSession(object):
                    ]
 
         (stdin, stdout, stderr) = self.control_session._x2go_exec_command(cmd_line)
-        if not stderr.read():
+        _stderr = stderr.read()
+        if sys.version_info[0] >= 3:
+            _stderr = _stderr.decode()
+        if not _stderr:
             self.logger('x2goumount-session (%s) for session %s has been successful' % (local_path, self.session_info, ), log.loglevel_NOTICE)
             return True
         else:
@@ -1273,6 +1290,8 @@ class X2GoTerminalSession(object):
         if test_cmd:
             (stdin, stdout, stderr) = self.control_session._x2go_exec_command([test_cmd])
             _stdout = stdout.read()
+            if sys.version_info[0] >= 3:
+                _stdout = _stdout.decode()
             return _stdout.find('OK') != -1
         else:
             return False
@@ -1342,7 +1361,10 @@ class X2GoTerminalSession(object):
         if self.params.kbtype not in ('null/null', 'auto') and (self.params.kblayout not in ('null', '') or self.params.kbvariant not in ('null', '')):
             self.set_keyboard(layout=self.params.kblayout, variant=self.params.kbvariant)
 
-        return stdout.read(), stderr.read()
+        if sys.version_info[0] >= 3:
+            return stdout.read().decode(), stderr.read().decode()
+        else:
+            return stdout.read(), stderr.read()
 
     def is_desktop_session(self):
         """\
@@ -1400,12 +1422,12 @@ class X2GoTerminalSession(object):
 
         (stdin, stdout, stderr) = self.control_session._x2go_exec_command(cmd_line)
         _stderr = stderr.read()
+        if sys.version_info[0] >= 3:
+            _stderr.decode()
         if not _stderr:
             self.logger('setting keyboard layout ,,%s\'\' and variant ,,%s\'\' for session %s has been successful' % (layout, variant, self.session_info), log.loglevel_NOTICE)
             return True
         else:
-            if sys.version_info[0] >= 3:
-               _stderr = _stderr.decode()
             self.logger('setting keyboard layout ,,%s\'\' and variant ,,%s\'\' for session %s failed: %s' % (layout, variant, self.session_info, _stderr.replace('\n', ' ')), log.loglevel_ERROR)
             return False
 
@@ -1566,9 +1588,13 @@ class X2GoTerminalSession(object):
         _stdout = stdout.read()
         _stderr = stderr.read()
 
+        if sys.version_info[0] >= 3:
+            _stdout = _stdout.decode()
+            _stderr = _stderr.decode()
+
         # if the first line of stdout is a "DEN(Y)" string then we will presume that
         # we tried to use X2Go desktop sharing and the sharing was rejected
-        if b"ACCESS DENIED" in _stderr and b"XSHAD" in _stderr:
+        if "ACCESS DENIED" in _stderr and "XSHAD" in _stderr:
             raise x2go_exceptions.X2GoDesktopSharingDenied('X2Go desktop sharing has been denied by the remote user')
 
         try:
@@ -1651,8 +1677,12 @@ class X2GoTerminalSession(object):
 
         (stdin, stdout, stderr) = self.control_session._x2go_exec_command(cmd_line)
 
+        _stdout = stdout.read()
+        if sys.version_info[0] >= 3:
+            _stdout = _stdout.decode()
+
         # re-allocate (if needed) server-side ports for graphics, sound and sshfs
-        for stdout_line in stdout.read():
+        for stdout_line in _stdout.split('\n'):
             try:
                 _new_value = stdout_line.split("=")[1].strip()
                 if 'gr_port=' in stdout_line and _new_value != str(self.session_info.graphics_port):
diff --git a/x2go/inifiles.py b/x2go/inifiles.py
index f549d5c..244602c 100644
--- a/x2go/inifiles.py
+++ b/x2go/inifiles.py
@@ -286,7 +286,10 @@ class X2GoIniFile(object):
 
 
         """
-        stdout = io.StringIO()
+        if sys.version_info[0] >= 3:
+            stdout = io.BytesIO()
+        else:
+            stdout = io.StringIO()
         self.iniConfig.write(stdout)
         _ret_val = stdout.getvalue()
         stdout.close()

--
Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on /srv/git/code.x2go.org/python-x2go.git


More information about the x2go-commits mailing list