Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_pipe.py @ 2562:26edcf3a30eb
core, setup: huge cleaning:
- moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention
- move twisted directory to root
- removed all hacks from setup.py, and added missing dependencies, it is now clean
- use https URL for website in setup.py
- removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed
- renamed sat.sh to sat and fixed its installation
- added python_requires to specify Python version needed
- replaced glib2reactor which use deprecated code by gtk3reactor
sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Apr 2018 19:44:50 +0200 |
parents | frontends/src/jp/cmd_pipe.py@e2a7bb875957 |
children | 56f94936df1e |
comparison
equal
deleted
inserted
replaced
2561:bd30dc3ffe5a | 2562:26edcf3a30eb |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SAT command line tool | |
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat_frontends.jp import base | |
21 | |
22 from sat_frontends.jp.constants import Const as C | |
23 import sys | |
24 from sat.core.i18n import _ | |
25 from sat_frontends.tools import jid | |
26 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI | |
27 from functools import partial | |
28 import socket | |
29 import SocketServer | |
30 import errno | |
31 | |
32 __commands__ = ["Pipe"] | |
33 | |
34 START_PORT = 9999 | |
35 | |
36 class PipeOut(base.CommandBase): | |
37 | |
38 def __init__(self, host): | |
39 super(PipeOut, self).__init__(host, 'out', help=_('send a pipe a stream')) | |
40 self.need_loop = True | |
41 | |
42 def add_parser_options(self): | |
43 self.parser.add_argument("jid", type=base.unicode_decoder, help=_("the destination jid")) | |
44 | |
45 def streamOutCb(self, port): | |
46 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
47 s.connect(('127.0.0.1', int(port))) | |
48 while True: | |
49 buf = sys.stdin.read(4096) | |
50 if not buf: | |
51 break | |
52 try: | |
53 s.sendall(buf) | |
54 except socket.error as e: | |
55 if e.errno == errno.EPIPE: | |
56 sys.stderr.write(str(e) + '\n') | |
57 self.host.quit(1) | |
58 else: | |
59 raise e | |
60 self.host.quit() | |
61 | |
62 def start(self): | |
63 """ Create named pipe, and send stdin to it """ | |
64 self.host.bridge.streamOut( | |
65 self.host.get_full_jid(self.args.jid), | |
66 self.profile, | |
67 callback=self.streamOutCb, | |
68 errback=partial(self.errback, | |
69 msg=_(u"can't start stream: {}"), | |
70 exit_code=C.EXIT_BRIDGE_ERRBACK)) | |
71 | |
72 | |
73 class StreamServer(SocketServer.BaseRequestHandler): | |
74 | |
75 def handle(self): | |
76 while True: | |
77 data = self.request.recv(4096) | |
78 if not data: | |
79 break | |
80 sys.stdout.write(data) | |
81 try: | |
82 sys.stdout.flush() | |
83 except IOError as e: | |
84 sys.stderr.write(str(e) + '\n') | |
85 break | |
86 # calling shutdown will do a deadlock as we don't use separate thread | |
87 # this is a workaround (cf. https://stackoverflow.com/a/36017741) | |
88 self.server._BaseServer__shutdown_request = True | |
89 | |
90 | |
91 class PipeIn(base.CommandAnswering): | |
92 | |
93 def __init__(self, host): | |
94 super(PipeIn, self).__init__(host, 'in', help=_('receive a pipe stream')) | |
95 self.action_callbacks = {"STREAM": self.onStreamAction} | |
96 | |
97 def add_parser_options(self): | |
98 self.parser.add_argument("jids", type=base.unicode_decoder, nargs="*", help=_('Jids accepted (none means "accept everything")')) | |
99 | |
100 def getXmluiId(self, action_data): | |
101 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module | |
102 # should be available in the future | |
103 # TODO: XMLUI module | |
104 try: | |
105 xml_ui = action_data['xmlui'] | |
106 except KeyError: | |
107 self.disp(_(u"Action has no XMLUI"), 1) | |
108 else: | |
109 ui = ET.fromstring(xml_ui.encode('utf-8')) | |
110 xmlui_id = ui.get('submit') | |
111 if not xmlui_id: | |
112 self.disp(_(u"Invalid XMLUI received"), error=True) | |
113 return xmlui_id | |
114 | |
115 def onStreamAction(self, action_data, action_id, security_limit, profile): | |
116 xmlui_id = self.getXmluiId(action_data) | |
117 if xmlui_id is None: | |
118 return self.host.quitFromSignal(1) | |
119 try: | |
120 from_jid = jid.JID(action_data['meta_from_jid']) | |
121 except KeyError: | |
122 self.disp(_(u"Ignoring action without from_jid data"), 1) | |
123 return | |
124 | |
125 if not self.bare_jids or from_jid.bare in self.bare_jids: | |
126 host, port = "localhost", START_PORT | |
127 while True: | |
128 try: | |
129 server = SocketServer.TCPServer((host, port), StreamServer) | |
130 except socket.error as e: | |
131 if e.errno == errno.EADDRINUSE: | |
132 port += 1 | |
133 else: | |
134 raise e | |
135 else: | |
136 break | |
137 xmlui_data = {'answer': C.BOOL_TRUE, | |
138 'port': unicode(port)} | |
139 self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile) | |
140 server.serve_forever() | |
141 self.host.quitFromSignal() | |
142 | |
143 def start(self): | |
144 self.bare_jids = [jid.JID(jid_).bare for jid_ in self.args.jids] | |
145 | |
146 | |
147 class Pipe(base.CommandBase): | |
148 subcommands = (PipeOut, PipeIn) | |
149 | |
150 def __init__(self, host): | |
151 super(Pipe, self).__init__(host, 'pipe', use_profile=False, help=_('stream piping through XMPP')) |