]>
Commit | Line | Data |
---|---|---|
4e1f1f9c TG |
1 | From 9f2edcde382b2de1f2b49c8fbfb0497385395a8c Mon Sep 17 00:00:00 2001 |
2 | From: James Page <james.page@ubuntu.com> | |
3 | Date: Thu, 8 Feb 2018 09:46:46 +0000 | |
4 | Subject: [PATCH] Python 3 support | |
5 | ||
6 | Misc fixes to Python based tooling for compatibility with | |
7 | Python 2 and 3. | |
8 | ||
9 | Signed-off-by: James Page <james.page@ubuntu.com> | |
10 | --- | |
4e1f1f9c TG |
11 | python/ovstest/rpcserver.py | 16 ++++--------- |
12 | python/ovstest/tcp.py | 4 ++-- | |
13 | python/ovstest/util.py | 4 +--- | |
14 | python/ovstest/vswitch.py | 5 +++- | |
15 | utilities/bugtool/ovs-bugtool.in | 22 +++++++++--------- | |
16 | utilities/ovs-check-dead-ifs.in | 8 ++++--- | |
17 | utilities/ovs-dpctl-top.in | 18 ++++++++------- | |
18 | utilities/ovs-l3ping.in | 12 ++++++---- | |
19 | utilities/ovs-parse-backtrace.in | 14 +++++++----- | |
20 | utilities/ovs-tcpdump.in | 2 ++ | |
21 | utilities/ovs-tcpundump.in | 2 ++ | |
22 | utilities/ovs-test.in | 39 +++++++++++++++++--------------- | |
23 | utilities/ovs-vlan-test.in | 25 +++++++++++--------- | |
24 | 14 files changed, 102 insertions(+), 89 deletions(-) | |
25 | ||
72dc24e4 TG |
26 | --- a/python/ovstest/rpcserver.py |
27 | +++ b/python/ovstest/rpcserver.py | |
28 | @@ -16,24 +16,18 @@ | |
4e1f1f9c TG |
29 | rpcserver is an XML RPC server that allows RPC client to initiate tests |
30 | """ | |
31 | ||
72dc24e4 TG |
32 | -import sys |
33 | +from __future__ import absolute_import | |
4e1f1f9c TG |
34 | |
35 | -import exceptions | |
72dc24e4 TG |
36 | +import sys |
37 | ||
38 | import xmlrpc.client | |
4e1f1f9c TG |
39 | |
40 | -import tcp | |
41 | - | |
42 | from twisted.internet import reactor | |
43 | from twisted.internet.error import CannotListenError | |
44 | from twisted.web import server | |
45 | from twisted.web import xmlrpc | |
46 | ||
47 | -import udp | |
48 | - | |
49 | -import util | |
50 | - | |
51 | -import vswitch | |
52 | +from . import tcp, udp, util, vswitch | |
53 | ||
54 | ||
55 | class TestArena(xmlrpc.XMLRPC): | |
72dc24e4 | 56 | @@ -210,7 +204,7 @@ class TestArena(xmlrpc.XMLRPC): |
4e1f1f9c TG |
57 | (_, port) = self.__get_handle_resources(handle) |
58 | port.loseConnection() | |
59 | self.__delete_handle(handle) | |
60 | - except exceptions.KeyError: | |
61 | + except KeyError: | |
62 | return -1 | |
63 | return 0 | |
64 | ||
72dc24e4 | 65 | @@ -222,7 +216,7 @@ class TestArena(xmlrpc.XMLRPC): |
4e1f1f9c TG |
66 | (_, connector) = self.__get_handle_resources(handle) |
67 | connector.disconnect() | |
68 | self.__delete_handle(handle) | |
69 | - except exceptions.KeyError: | |
70 | + except KeyError: | |
71 | return -1 | |
72 | return 0 | |
73 | ||
72dc24e4 TG |
74 | --- a/python/ovstest/tcp.py |
75 | +++ b/python/ovstest/tcp.py | |
4e1f1f9c TG |
76 | @@ -21,7 +21,7 @@ import time |
77 | from twisted.internet import interfaces | |
78 | from twisted.internet.protocol import ClientFactory, Factory, Protocol | |
79 | ||
80 | -from zope.interface import implements | |
81 | +from zope.interface.declarations import implementer | |
82 | ||
83 | ||
84 | class TcpListenerConnection(Protocol): | |
85 | @@ -55,8 +55,8 @@ class TcpListenerFactory(Factory): | |
86 | return str(self.stats) | |
87 | ||
88 | ||
89 | +@implementer(interfaces.IPushProducer) | |
90 | class Producer(object): | |
91 | - implements(interfaces.IPushProducer) | |
92 | """ | |
93 | This producer class generates infinite byte stream for a specified time | |
94 | duration | |
72dc24e4 TG |
95 | --- a/python/ovstest/util.py |
96 | +++ b/python/ovstest/util.py | |
4e1f1f9c TG |
97 | @@ -26,8 +26,6 @@ import socket |
98 | import struct | |
99 | import subprocess | |
100 | ||
101 | -import exceptions | |
102 | - | |
72dc24e4 | 103 | import xmlrpc.client |
4e1f1f9c | 104 | |
72dc24e4 TG |
105 | |
106 | @@ -88,7 +86,7 @@ def start_process(args): | |
4e1f1f9c TG |
107 | stderr=subprocess.PIPE) |
108 | out, err = p.communicate() | |
109 | return (p.returncode, out, err) | |
110 | - except exceptions.OSError: | |
111 | + except OSError: | |
112 | return (-1, None, None) | |
113 | ||
114 | ||
72dc24e4 TG |
115 | --- a/python/ovstest/vswitch.py |
116 | +++ b/python/ovstest/vswitch.py | |
4e1f1f9c TG |
117 | @@ -15,7 +15,10 @@ |
118 | """ | |
119 | vswitch module allows its callers to interact with OVS DB. | |
120 | """ | |
121 | -import util | |
122 | + | |
123 | +from __future__ import absolute_import | |
124 | + | |
125 | +from . import util | |
126 | ||
127 | ||
128 | def ovs_vsctl_add_bridge(bridge): | |
72dc24e4 TG |
129 | --- a/utilities/ovs-check-dead-ifs.in |
130 | +++ b/utilities/ovs-check-dead-ifs.in | |
4e1f1f9c | 131 | @@ -1,5 +1,7 @@ |
72dc24e4 | 132 | #! @PYTHON3@ |
4e1f1f9c TG |
133 | |
134 | +from __future__ import print_function | |
135 | + | |
136 | import os | |
137 | import re | |
138 | import stat | |
72dc24e4 TG |
139 | --- a/utilities/ovs-dpctl-top.in |
140 | +++ b/utilities/ovs-dpctl-top.in | |
4e1f1f9c TG |
141 | @@ -131,6 +131,8 @@ $ ovs-dpctl-top --script --flow-file dum |
142 | # pylint: disable-msg=R0913 | |
143 | # pylint: disable-msg=R0914 | |
144 | ||
145 | +from __future__ import print_function | |
146 | + | |
147 | import sys | |
148 | import os | |
149 | try: | |
72dc24e4 | 150 | @@ -419,7 +421,7 @@ def flow_line_iter(line): |
4e1f1f9c TG |
151 | rc.append(element) |
152 | element = "" | |
153 | else: | |
154 | - element += ch | |
155 | + element += str(ch) | |
156 | ||
157 | if (paren_count): | |
158 | raise ValueError(line) | |
72dc24e4 | 159 | @@ -468,7 +470,7 @@ def flow_line_split(line): |
4e1f1f9c TG |
160 | |
161 | """ | |
162 | ||
163 | - results = re.split(', ', line) | |
164 | + results = re.split(b', ', line) | |
165 | ||
166 | (field, stats, action) = (results[0], results[1:-1], results[-1]) | |
167 | ||
420a9583 LB |
168 | @@ -963,7 +965,7 @@ class FlowDB: |
169 | if not isinstance(line, str): | |
170 | line = str(line) | |
4e1f1f9c TG |
171 | |
172 | - line = line.rstrip("\n") | |
173 | + line = line.rstrip(b"\n") | |
174 | (fields, stats, _) = flow_line_split(line) | |
175 | ||
176 | try: | |
72dc24e4 TG |
177 | --- a/utilities/ovs-l3ping.in |
178 | +++ b/utilities/ovs-l3ping.in | |
4e1f1f9c TG |
179 | @@ -18,8 +18,10 @@ opening holes in the firewall for the XM |
180 | achieved by tunneling the control connection inside the tunnel itself. | |
181 | """ | |
182 | ||
183 | +from __future__ import print_function | |
184 | + | |
185 | import socket | |
186 | -import xmlrpclib | |
187 | +from six.moves import xmlrpc_client as xmlrpclib | |
188 | ||
189 | import ovstest.args as args | |
190 | import ovstest.tests as tests | |
191 | @@ -64,13 +66,13 @@ if __name__ == '__main__': | |
192 | ps = get_packet_sizes(me, he, args.client[0]) | |
193 | tests.do_direct_tests(me, he, bandwidth, interval, ps) | |
194 | except KeyboardInterrupt: | |
195 | - print "Terminating" | |
196 | + print("Terminating") | |
197 | except xmlrpclib.Fault: | |
198 | - print "Couldn't contact peer" | |
199 | + print("Couldn't contact peer") | |
200 | except socket.error: | |
201 | - print "Couldn't contact peer" | |
202 | + print("Couldn't contact peer") | |
203 | except xmlrpclib.ProtocolError: | |
204 | - print "XMLRPC control channel was abruptly terminated" | |
205 | + print("XMLRPC control channel was abruptly terminated") | |
206 | finally: | |
207 | if local_server is not None: | |
208 | local_server.terminate() | |
72dc24e4 TG |
209 | --- a/utilities/ovs-parse-backtrace.in |
210 | +++ b/utilities/ovs-parse-backtrace.in | |
4e1f1f9c TG |
211 | @@ -14,6 +14,8 @@ |
212 | # See the License for the specific language governing permissions and | |
213 | # limitations under the License. | |
214 | ||
215 | +from __future__ import print_function | |
216 | + | |
217 | import optparse | |
218 | import os | |
219 | import re | |
220 | @@ -70,7 +72,7 @@ result. Expected usage is for ovs-appct | |
221 | if os.path.exists(debug): | |
222 | binary = debug | |
223 | ||
224 | - print "Binary: %s\n" % binary | |
225 | + print("Binary: %s\n" % binary) | |
226 | ||
227 | stdin = sys.stdin.read() | |
228 | ||
229 | @@ -88,15 +90,15 @@ result. Expected usage is for ovs-appct | |
230 | for lines, count in traces: | |
231 | longest = max(len(l) for l in lines) | |
232 | ||
233 | - print "Backtrace Count: %d" % count | |
234 | + print("Backtrace Count: %d" % count) | |
235 | for line in lines: | |
236 | match = re.search(r'\[(0x.*)]', line) | |
237 | if match: | |
238 | - print "%s %s" % (line.ljust(longest), | |
239 | - addr2line(binary, match.group(1))) | |
240 | + print("%s %s" % (line.ljust(longest), | |
241 | + addr2line(binary, match.group(1)))) | |
242 | else: | |
243 | - print line | |
244 | ||
245 | + print(line) | |
246 | + print() | |
247 | ||
248 | ||
249 | if __name__ == "__main__": | |
72dc24e4 TG |
250 | --- a/utilities/ovs-tcpdump.in |
251 | +++ b/utilities/ovs-tcpdump.in | |
4e1f1f9c TG |
252 | @@ -14,6 +14,8 @@ |
253 | # See the License for the specific language governing permissions and | |
254 | # limitations under the License. | |
255 | ||
256 | +from __future__ import print_function | |
257 | + | |
258 | import fcntl | |
259 | ||
260 | import os | |
72dc24e4 TG |
261 | --- a/utilities/ovs-tcpundump.in |
262 | +++ b/utilities/ovs-tcpundump.in | |
4e1f1f9c TG |
263 | @@ -14,6 +14,8 @@ |
264 | # See the License for the specific language governing permissions and | |
265 | # limitations under the License. | |
266 | ||
267 | +from __future__ import print_function | |
268 | + | |
269 | import getopt | |
270 | import re | |
271 | import sys | |
72dc24e4 TG |
272 | --- a/utilities/ovs-test.in |
273 | +++ b/utilities/ovs-test.in | |
4e1f1f9c TG |
274 | @@ -16,6 +16,8 @@ |
275 | ovs test utility that allows to do tests between remote hosts | |
276 | """ | |
277 | ||
278 | +from __future__ import print_function | |
279 | + | |
280 | import fcntl | |
281 | import math | |
282 | import os | |
283 | @@ -25,7 +27,6 @@ import socket | |
284 | import subprocess | |
285 | import sys | |
286 | import time | |
287 | -import xmlrpclib | |
288 | ||
289 | import argparse | |
290 | import twisted | |
291 | @@ -35,6 +36,8 @@ import ovstest.rpcserver as rpcserver | |
292 | import ovstest.tests as tests | |
293 | import ovstest.util as util | |
294 | ||
295 | +from six.moves import xmlrpc_client as xmlrpclib | |
296 | + | |
297 | DEFAULT_TEST_BRIDGE = "ovstestbr0" | |
298 | DEFAULT_TEST_PORT = "ovstestport0" | |
299 | DEFAULT_TEST_TUN = "ovstestport1" | |
300 | @@ -42,7 +45,7 @@ DEFAULT_TEST_TUN = "ovstestport1" | |
301 | ||
302 | def collect_information(node): | |
303 | """Print information about hosts that will do testing""" | |
304 | - print "Node %s:%u " % (node[0], node[1]) | |
305 | + print("Node %s:%u " % (node[0], node[1])) | |
306 | server = util.rpc_client(node[0], node[1]) | |
307 | interface_name = server.get_interface(node[0]) | |
308 | phys_iface = None | |
309 | @@ -50,8 +53,8 @@ def collect_information(node): | |
310 | mtu = 1500 | |
311 | ||
312 | if not interface_name: | |
313 | - print ("Could not find interface that has %s IP address." | |
314 | - "Make sure that you specified correct Outer IP." % (node[0])) | |
315 | + print("Could not find interface that has %s IP address." | |
316 | + "Make sure that you specified correct Outer IP." % (node[0])) | |
317 | else: | |
318 | if server.is_ovs_bridge(interface_name): | |
319 | phys_iface = server.get_iface_from_bridge(interface_name) | |
320 | @@ -62,18 +65,18 @@ def collect_information(node): | |
321 | driver = server.get_driver(phys_iface) | |
322 | mtu = server.get_interface_mtu(phys_iface) | |
323 | ||
324 | - print "Will be using %s (%s) with MTU %u" % (phys_iface, node[0], | |
325 | - mtu) | |
326 | + print("Will be using %s (%s) with MTU %u" % (phys_iface, node[0], | |
327 | + mtu)) | |
328 | if not driver: | |
329 | - print "Unable to get driver information from ethtool." | |
330 | + print("Unable to get driver information from ethtool.") | |
331 | else: | |
332 | - print "On this host %s has %s." % (phys_iface, driver) | |
333 | + print("On this host %s has %s." % (phys_iface, driver)) | |
334 | ||
335 | if not uname: | |
336 | - print "Unable to retrieve kernel information. Is this Linux?" | |
337 | + print("Unable to retrieve kernel information. Is this Linux?") | |
338 | else: | |
339 | - print "Running kernel %s." % uname | |
340 | - print "\n" | |
341 | + print("Running kernel %s." % uname) | |
342 | + print() | |
343 | ||
344 | return mtu | |
345 | ||
346 | @@ -113,29 +116,29 @@ if __name__ == '__main__': | |
347 | tunnel_modes = ovs_args.tunnelModes | |
348 | ||
349 | if direct is not None: | |
350 | - print "Performing direct tests" | |
351 | + print("Performing direct tests") | |
352 | tests.do_direct_tests(node2, node1, bandwidth, interval, ps) | |
353 | ||
354 | if vlan_tag is not None: | |
355 | - print "Performing VLAN tests" | |
356 | + print("Performing VLAN tests") | |
357 | tests.do_vlan_tests(node2, node1, bandwidth, interval, ps, | |
358 | vlan_tag) | |
359 | ||
360 | for tmode in tunnel_modes: | |
361 | - print "Performing", tmode, "tests" | |
362 | + print("Performing", tmode, "tests") | |
363 | tests.do_l3_tests(node2, node1, bandwidth, interval, ps, | |
364 | tmode) | |
365 | ||
366 | except KeyboardInterrupt: | |
367 | pass | |
368 | except xmlrpclib.Fault: | |
369 | - print "Couldn't establish XMLRPC control channel" | |
370 | + print("Couldn't establish XMLRPC control channel") | |
371 | except socket.error: | |
372 | - print "Couldn't establish XMLRPC control channel" | |
373 | + print("Couldn't establish XMLRPC control channel") | |
374 | except xmlrpclib.ProtocolError: | |
375 | - print "XMLRPC control channel was abruptly terminated" | |
376 | + print("XMLRPC control channel was abruptly terminated") | |
377 | except twisted.internet.error.CannotListenError: | |
378 | - print "Couldn't start XMLRPC server on port %u" % ovs_args.port | |
379 | + print("Couldn't start XMLRPC server on port %u" % ovs_args.port) | |
380 | finally: | |
381 | if local_server is not None: | |
382 | local_server.terminate() | |
72dc24e4 TG |
383 | --- a/utilities/ovs-vlan-test.in |
384 | +++ b/utilities/ovs-vlan-test.in | |
4e1f1f9c TG |
385 | @@ -14,9 +14,9 @@ |
386 | # See the License for the specific language governing permissions and | |
387 | # limitations under the License. | |
388 | ||
389 | -import BaseHTTPServer | |
390 | +from __future__ import print_function | |
391 | + | |
392 | import getopt | |
393 | -import httplib | |
394 | import os | |
395 | import threading | |
396 | import time | |
397 | @@ -24,6 +24,9 @@ import signal #Causes keyboard interrupt | |
398 | import socket | |
399 | import sys | |
400 | ||
401 | +from six.moves import BaseHTTPServer | |
402 | +from six.moves import http_client as httplib | |
403 | + | |
404 | print_safe_lock = threading.Lock() | |
405 | def print_safe(s): | |
406 | print_safe_lock.acquire() | |
407 | @@ -84,7 +87,7 @@ class UDPReceiver: | |
408 | ||
409 | try: | |
410 | sock.bind((self.vlan_ip, self.vlan_port)) | |
411 | - except socket.error, e: | |
412 | + except socket.error as e: | |
413 | print_safe('Failed to bind to %s:%d with error: %s' | |
414 | % (self.vlan_ip, self.vlan_port, e)) | |
415 | os._exit(1) #sys.exit only exits the current thread. | |
416 | @@ -95,7 +98,7 @@ class UDPReceiver: | |
417 | data, _ = sock.recvfrom(4096) | |
418 | except socket.timeout: | |
419 | continue | |
420 | - except socket.error, e: | |
421 | + except socket.error as e: | |
422 | print_safe('Failed to receive from %s:%d with error: %s' | |
423 | % (self.vlan_ip, self.vlan_port, e)) | |
424 | os._exit(1) | |
425 | @@ -180,7 +183,7 @@ class VlanServer: | |
426 | for _ in range(send_time * 2): | |
427 | try: | |
428 | send_packet(test_id, size, ip, port) | |
429 | - except socket.error, e: | |
430 | + except socket.error as e: | |
431 | self.set_result(test_id, 'Failure: ' + str(e)) | |
432 | return | |
433 | time.sleep(.5) | |
434 | @@ -196,7 +199,7 @@ class VlanServer: | |
435 | try: | |
436 | BaseHTTPServer.HTTPServer((self.server_ip, self.server_port), | |
437 | VlanServerHandler).serve_forever() | |
438 | - except socket.error, e: | |
439 | + except socket.error as e: | |
440 | print_safe('Failed to start control server: %s' % e) | |
441 | self.udp_recv.stop() | |
442 | ||
443 | @@ -256,7 +259,7 @@ class VlanClient: | |
444 | try: | |
445 | conn = self.request('/start/recv') | |
446 | data = conn.getresponse().read() | |
447 | - except (socket.error, httplib.HTTPException), e: | |
448 | + except (socket.error, httplib.HTTPException) as e: | |
449 | error_msg(e) | |
450 | return False | |
451 | ||
452 | @@ -277,7 +280,7 @@ class VlanClient: | |
453 | send_packet(test_id, size, ip, port) | |
454 | resp = self.request('/result/%d' % test_id).getresponse() | |
455 | data = resp.read() | |
456 | - except (socket.error, httplib.HTTPException), e: | |
457 | + except (socket.error, httplib.HTTPException) as e: | |
458 | error_msg(e) | |
459 | return False | |
460 | ||
461 | @@ -302,7 +305,7 @@ class VlanClient: | |
462 | try: | |
463 | conn = self.request(resource) | |
464 | test_id = conn.getresponse().read() | |
465 | - except (socket.error, httplib.HTTPException), e: | |
466 | + except (socket.error, httplib.HTTPException) as e: | |
467 | error_msg(e) | |
468 | return False | |
469 | ||
470 | @@ -335,7 +338,7 @@ class VlanClient: | |
471 | try: | |
472 | resp = self.request('/ping').getresponse() | |
473 | data = resp.read() | |
474 | - except (socket.error, httplib.HTTPException), e: | |
475 | + except (socket.error, httplib.HTTPException) as e: | |
476 | error_msg(e) | |
477 | return False | |
478 | ||
479 | @@ -383,7 +386,7 @@ def main(): | |
480 | try: | |
481 | options, args = getopt.gnu_getopt(sys.argv[1:], 'hVs', | |
482 | ['help', 'version', 'server']) | |
483 | - except getopt.GetoptError, geo: | |
484 | + except getopt.GetoptError as geo: | |
485 | print_safe('%s: %s\n' % (sys.argv[0], geo.msg)) | |
486 | return 1 | |
487 |