comparison sat_frontends/jp/cmd_pipe.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 003b8b4b56a7
children fee60f17ebac
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
24 from sat.core.i18n import _ 24 from sat.core.i18n import _
25 from sat_frontends.tools import jid 25 from sat_frontends.tools import jid
26 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI 26 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
27 from functools import partial 27 from functools import partial
28 import socket 28 import socket
29 import SocketServer 29 import socketserver
30 import errno 30 import errno
31 31
32 __commands__ = ["Pipe"] 32 __commands__ = ["Pipe"]
33 33
34 START_PORT = 9999 34 START_PORT = 9999
39 super(PipeOut, self).__init__(host, "out", help=_("send a pipe a stream")) 39 super(PipeOut, self).__init__(host, "out", help=_("send a pipe a stream"))
40 self.need_loop = True 40 self.need_loop = True
41 41
42 def add_parser_options(self): 42 def add_parser_options(self):
43 self.parser.add_argument( 43 self.parser.add_argument(
44 "jid", type=base.unicode_decoder, help=_("the destination jid") 44 "jid", help=_("the destination jid")
45 ) 45 )
46 46
47 def streamOutCb(self, port): 47 def streamOutCb(self, port):
48 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 48 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49 s.connect(("127.0.0.1", int(port))) 49 s.connect(("127.0.0.1", int(port)))
67 self.host.get_full_jid(self.args.jid), 67 self.host.get_full_jid(self.args.jid),
68 self.profile, 68 self.profile,
69 callback=self.streamOutCb, 69 callback=self.streamOutCb,
70 errback=partial( 70 errback=partial(
71 self.errback, 71 self.errback,
72 msg=_(u"can't start stream: {}"), 72 msg=_("can't start stream: {}"),
73 exit_code=C.EXIT_BRIDGE_ERRBACK, 73 exit_code=C.EXIT_BRIDGE_ERRBACK,
74 ), 74 ),
75 ) 75 )
76 76
77 77
78 class StreamServer(SocketServer.BaseRequestHandler): 78 class StreamServer(socketserver.BaseRequestHandler):
79 def handle(self): 79 def handle(self):
80 while True: 80 while True:
81 data = self.request.recv(4096) 81 data = self.request.recv(4096)
82 if not data: 82 if not data:
83 break 83 break
98 self.action_callbacks = {"STREAM": self.onStreamAction} 98 self.action_callbacks = {"STREAM": self.onStreamAction}
99 99
100 def add_parser_options(self): 100 def add_parser_options(self):
101 self.parser.add_argument( 101 self.parser.add_argument(
102 "jids", 102 "jids",
103 type=base.unicode_decoder,
104 nargs="*", 103 nargs="*",
105 help=_('Jids accepted (none means "accept everything")'), 104 help=_('Jids accepted (none means "accept everything")'),
106 ) 105 )
107 106
108 def getXmluiId(self, action_data): 107 def getXmluiId(self, action_data):
110 # should be available in the future 109 # should be available in the future
111 # TODO: XMLUI module 110 # TODO: XMLUI module
112 try: 111 try:
113 xml_ui = action_data["xmlui"] 112 xml_ui = action_data["xmlui"]
114 except KeyError: 113 except KeyError:
115 self.disp(_(u"Action has no XMLUI"), 1) 114 self.disp(_("Action has no XMLUI"), 1)
116 else: 115 else:
117 ui = ET.fromstring(xml_ui.encode("utf-8")) 116 ui = ET.fromstring(xml_ui.encode("utf-8"))
118 xmlui_id = ui.get("submit") 117 xmlui_id = ui.get("submit")
119 if not xmlui_id: 118 if not xmlui_id:
120 self.disp(_(u"Invalid XMLUI received"), error=True) 119 self.disp(_("Invalid XMLUI received"), error=True)
121 return xmlui_id 120 return xmlui_id
122 121
123 def onStreamAction(self, action_data, action_id, security_limit, profile): 122 def onStreamAction(self, action_data, action_id, security_limit, profile):
124 xmlui_id = self.getXmluiId(action_data) 123 xmlui_id = self.getXmluiId(action_data)
125 if xmlui_id is None: 124 if xmlui_id is None:
126 return self.host.quitFromSignal(1) 125 return self.host.quitFromSignal(1)
127 try: 126 try:
128 from_jid = jid.JID(action_data["meta_from_jid"]) 127 from_jid = jid.JID(action_data["meta_from_jid"])
129 except KeyError: 128 except KeyError:
130 self.disp(_(u"Ignoring action without from_jid data"), 1) 129 self.disp(_("Ignoring action without from_jid data"), 1)
131 return 130 return
132 131
133 if not self.bare_jids or from_jid.bare in self.bare_jids: 132 if not self.bare_jids or from_jid.bare in self.bare_jids:
134 host, port = "localhost", START_PORT 133 host, port = "localhost", START_PORT
135 while True: 134 while True:
136 try: 135 try:
137 server = SocketServer.TCPServer((host, port), StreamServer) 136 server = socketserver.TCPServer((host, port), StreamServer)
138 except socket.error as e: 137 except socket.error as e:
139 if e.errno == errno.EADDRINUSE: 138 if e.errno == errno.EADDRINUSE:
140 port += 1 139 port += 1
141 else: 140 else:
142 raise e 141 raise e
143 else: 142 else:
144 break 143 break
145 xmlui_data = {"answer": C.BOOL_TRUE, "port": unicode(port)} 144 xmlui_data = {"answer": C.BOOL_TRUE, "port": str(port)}
146 self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile) 145 self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
147 server.serve_forever() 146 server.serve_forever()
148 self.host.quitFromSignal() 147 self.host.quitFromSignal()
149 148
150 def start(self): 149 def start(self):