comparison sat/tools/common/async_process.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 181735d1b062
children 0d55a52056f7
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT: a jabber client 4 # SAT: a jabber client
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
54 return self.name 54 return self.name
55 elif self.command is not None: 55 elif self.command is not None:
56 return os.path.splitext(os.path.basename(self.command))[0].decode('utf-8', 56 return os.path.splitext(os.path.basename(self.command))[0].decode('utf-8',
57 'ignore') 57 'ignore')
58 else: 58 else:
59 return u'' 59 return ''
60 60
61 def connectionMade(self): 61 def connectionMade(self):
62 if self._stdin is not None: 62 if self._stdin is not None:
63 self.transport.write(self._stdin) 63 self.transport.write(self._stdin)
64 self.transport.closeStdin() 64 self.transport.closeStdin()
72 if self.log: 72 if self.log:
73 log.warning(data.decode('utf-8', 'replace')) 73 log.warning(data.decode('utf-8', 'replace'))
74 self.err_data.append(data) 74 self.err_data.append(data)
75 75
76 def processEnded(self, reason): 76 def processEnded(self, reason):
77 data = ''.join(self.data) 77 data = b''.join(self.data)
78 if (reason.value.exitCode == 0): 78 if (reason.value.exitCode == 0):
79 log.debug(_(u'{name} command succeed').format(name=self.command_name)) 79 log.debug(_('{name} command succeed').format(name=self.command_name))
80 # we don't use "replace" on purpose, we want an exception if decoding 80 # we don't use "replace" on purpose, we want an exception if decoding
81 # is not working properly 81 # is not working properly
82 self._deferred.callback(data.encode('utf-8')) 82 self._deferred.callback(data)
83 else: 83 else:
84 err_data = u''.join(self.err_data) 84 err_data = b''.join(self.err_data)
85 85
86 msg = (_(u"Can't complete {name} command (error code: {code}):\n" 86 msg = (_("Can't complete {name} command (error code: {code}):\n"
87 u"stderr:\n{stderr}\n{stdout}\n") 87 "stderr:\n{stderr}\n{stdout}\n")
88 .format(name = self.command_name, 88 .format(name = self.command_name,
89 code = reason.value.exitCode, 89 code = reason.value.exitCode,
90 stderr= err_data.encode('utf-8', 'replace'), 90 stderr= err_data.encode('utf-8', 'replace'),
91 stdout = "stdout: " + data.encode('utf-8', 'replace') 91 stdout = "stdout: " + data.decode('utf-8', 'replace')
92 if data else u'', 92 if data else '',
93 )) 93 ))
94 self._deferred.errback(Failure(exceptions.CommandException( 94 self._deferred.errback(Failure(exceptions.CommandException(
95 msg, data, err_data))) 95 msg, data, err_data)))
96 96
97 @classmethod 97 @classmethod
112 """ 112 """
113 stdin = kwargs.pop('stdin', None) 113 stdin = kwargs.pop('stdin', None)
114 if stdin is not None: 114 if stdin is not None:
115 stdin = stdin.encode('utf-8') 115 stdin = stdin.encode('utf-8')
116 verbose = kwargs.pop('verbose', False) 116 verbose = kwargs.pop('verbose', False)
117 if u'path' in kwargs: 117 if 'path' in kwargs:
118 kwargs[u'path'] = kwargs[u'path'].encode('utf-8') 118 kwargs['path'] = kwargs['path'].encode('utf-8')
119 args = [a.encode('utf-8') for a in args] 119 args = [a.encode('utf-8') for a in args]
120 kwargs = {k:v.encode('utf-8') for k,v in kwargs.items()} 120 kwargs = {k:v.encode('utf-8') for k,v in list(kwargs.items())}
121 d = defer.Deferred() 121 d = defer.Deferred()
122 prot = cls(d, stdin=stdin) 122 prot = cls(d, stdin=stdin)
123 if verbose: 123 if verbose:
124 prot.log = True 124 prot.log = True
125 if cls.command is None: 125 if cls.command is None:
126 if not args: 126 if not args:
127 raise ValueError( 127 raise ValueError(
128 u"You must either specify cls.command or use a full path to command " 128 "You must either specify cls.command or use a full path to command "
129 u"to execute as first argument") 129 "to execute as first argument")
130 command = args.pop(0) 130 command = args.pop(0)
131 if prot.name is None: 131 if prot.name is None:
132 name = os.path.splitext(os.path.basename(command))[0] 132 name = os.path.splitext(os.path.basename(command))[0]
133 prot.name = name.encode(u'utf-8', u'ignore') 133 prot.name = name
134 else: 134 else:
135 command = cls.command 135 command = cls.command
136 cmd_args = [os.path.basename(command)] + args 136 cmd_args = [os.path.basename(command)] + args
137 reactor.spawnProcess(prot, 137 reactor.spawnProcess(prot,
138 command, 138 command,