applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / linux / scripts / tunchannel.py
index cd30b8b..8a3047c 100644 (file)
@@ -3,9 +3,8 @@
 #    Copyright (C) 2013 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,6 +18,7 @@
 #         Claudio Freire <claudio-daniel.freire@inria.fr>
 #
 
+from __future__ import print_function
 
 import select
 import sys
@@ -202,7 +202,7 @@ def decrypt(packet, crypter, ord=ord):
         padding = ord(packet[-1])
         if not (0 < padding <= crypter.block_size):
             # wrong padding
-            raise RuntimeError, "Truncated packet %s"
+            raise RuntimeError("Truncated packet %s")
         packet = packet[:-padding]
     
     return packet
@@ -262,9 +262,9 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
 
     if stderr is not None:
         if crypto_mode:
-            print >>stderr, "Packets are transmitted in CIPHER"
+            print("Packets are transmitted in CIPHER", file=stderr)
         else:
-            print >>stderr, "Packets are transmitted in PLAINTEXT"
+            print("Packets are transmitted in PLAINTEXT", file=stderr)
     
     if hasattr(remote, 'fileno'):
         remote_fd = remote.fileno()
@@ -404,7 +404,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
         
         try:
             rdrdy, wrdy, errs = select(rset,wset,eset,1)
-        except selecterror, e:
+        except selecterror as e:
             if e.args[0] == errno.EINTR:
                 # just retry
                 continue
@@ -460,7 +460,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                         
                         if not rnonblock or not fpacketReady(fwbuf):
                             break
-                except OSError,e:
+                except OSError as e:
                     # This except handles the entire While block on PURPOSE
                     # as an optimization (setting a try/except block is expensive)
                     # The only operation that can raise this exception is rwrite
@@ -500,7 +500,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                     if slowlocal:
                         # Give some time for the kernel to process the packets
                         time.sleep(0)
-            except OSError,e:
+            except OSError as e:
                 # This except handles the entire While block on PURPOSE
                 # as an optimization (setting a try/except block is expensive)
                 # The only operation that can raise this exception is os_write
@@ -522,7 +522,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                     
                     if not tnonblock or len(fwbuf) >= maxfwbuf:
                         break
-            except OSError,e:
+            except OSError as e:
                 # This except handles the entire While block on PURPOSE
                 # as an optimization (setting a try/except block is expensive)
                 # The only operation that can raise this exception is os_read
@@ -543,7 +543,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                         elif not packet:
                             if not udp and packet == "":
                                 # Connection broken, try to reconnect (or just die)
-                                raise RuntimeError, "Connection broken"
+                                raise RuntimeError("Connection broken")
                             else:
                                 continue
 
@@ -551,13 +551,13 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                         
                         if not rnonblock or len(bkbuf) >= maxbkbuf:
                             break
-                except OSError,e:
+                except OSError as e:
                     # This except handles the entire While block on PURPOSE
                     # as an optimization (setting a try/except block is expensive)
                     # The only operation that can raise this exception is rread
                     if e.errno not in retrycodes:
                         raise
-            except Exception, e:
+            except Exception as e:
                 if reconnect is not None:
                     # in UDP mode, sometimes connected sockets can return a connection refused
                     # on read. Give the caller a chance to reconnect
@@ -587,19 +587,19 @@ def udp_connect(TERMINATE, local_addr, local_port, peer_addr, peer_port):
         # TERMINATE is a array. An item can be added to TERMINATE, from
         # outside this function to force termination of the loop
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         try:
             rsock.bind((local_addr, local_port))
             break
         except socket.error:
             # wait a while, retry
-            print >>sys.stderr, "%s: Could not bind. Retrying in a sec..." % (time.strftime('%c'),)
+            print("%s: Could not bind. Retrying in a sec..." % (time.strftime('%c'),), file=sys.stderr)
             time.sleep(min(30.0,retrydelay))
             retrydelay *= 1.1
     else:
         rsock.bind((local_addr, local_port))
-    print >>sys.stderr, "Listening UDP at: %s:%d" % (local_addr, local_port)
-    print >>sys.stderr, "Connecting UDP to: %s:%d" % (peer_addr, peer_port)
+    print("Listening UDP at: %s:%d" % (local_addr, local_port), file=sys.stderr)
+    print("Connecting UDP to: %s:%d" % (peer_addr, peer_port), file=sys.stderr)
     rsock.connect((peer_addr, peer_port))
     return rsock
 
@@ -620,7 +620,7 @@ def udp_handshake(TERMINATE, rsock):
     keepalive_thread.start()
     for i in xrange(900):
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         try:
             heartbeat = rsock.recv(10)
             break
@@ -648,24 +648,24 @@ def tcp_connect(TERMINATE, stop, rsock, peer_addr, peer_port):
         if stop:
             break
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         try:
             rsock.connect((peer_addr, peer_port))
             sock = rsock
             break
         except socket.error:
             # wait a while, retry
-            print >>sys.stderr, "%s: Could not connect. Retrying in a sec..." % (time.strftime('%c'),)
+            print("%s: Could not connect. Retrying in a sec..." % (time.strftime('%c'),), file=sys.stderr)
             time.sleep(min(30.0,retrydelay))
             retrydelay *= 1.1
     else:
         rsock.connect((peer_addr, peer_port))
         sock = rsock
     if sock:
-        print >>sys.stderr, "tcp_connect: TCP sock connected to remote %s:%s" % (peer_addr, peer_port)
+        print("tcp_connect: TCP sock connected to remote %s:%s" % (peer_addr, peer_port), file=sys.stderr)
         sock.settimeout(0) 
         
-        print >>sys.stderr, "tcp_connect: disabling NAGLE"
+        print("tcp_connect: disabling NAGLE", file=sys.stderr)
         sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
     return sock
 
@@ -678,32 +678,32 @@ def tcp_listen(TERMINATE, stop, lsock, local_addr, local_port):
         if stop:
             break
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         try:
             lsock.bind((local_addr, local_port))
             break
         except socket.error:
             # wait a while, retry
-            print >>sys.stderr, "%s: Could not bind. Retrying in a sec..." % (time.strftime('%c'),)
+            print("%s: Could not bind. Retrying in a sec..." % (time.strftime('%c'),), file=sys.stderr)
             time.sleep(min(30.0,retrydelay))
             retrydelay *= 1.1
     else:
         lsock.bind((local_addr, local_port))
 
-    print >>sys.stderr, "tcp_listen: TCP sock listening in local sock %s:%s" % (local_addr, local_port)
+    print("tcp_listen: TCP sock listening in local sock %s:%s" % (local_addr, local_port), file=sys.stderr)
     # Now we wait until the other side connects. 
     # The other side might not be ready yet, so we also wait in a loop for timeouts.
     timeout = 1
     lsock.listen(1)
     for i in xrange(30):
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         rlist, wlist, xlist = select.select([lsock], [], [], timeout)
         if stop:
             break
         if lsock in rlist:
             sock,raddr = lsock.accept()
-            print >>sys.stderr, "tcp_listen: TCP connection accepted in local sock %s:%s" % (local_addr, local_port)
+            print("tcp_listen: TCP connection accepted in local sock %s:%s" % (local_addr, local_port), file=sys.stderr)
             break
         timeout += 5
     return sock
@@ -718,10 +718,10 @@ def tcp_handshake(rsock, listen, hand):
         rsock.send(hand)
         peer_hand = rsock.recv(4)
         if not peer_hand:
-            print >>sys.stderr, "tcp_handshake: connection reset by peer"
+            print("tcp_handshake: connection reset by peer", file=sys.stderr)
             return False
         else:
-            print >>sys.stderr, "tcp_handshake: hand %r, peer_hand %r" % (hand, peer_hand)
+            print("tcp_handshake: hand %r, peer_hand %r" % (hand, peer_hand), file=sys.stderr)
         if hand < peer_hand:
             if listen:
                 win = True
@@ -755,7 +755,7 @@ def tcp_establish(TERMINATE, local_addr, local_port, peer_addr, peer_port):
         if end:
             break
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         hand = struct.pack("!L", random.randint(0, 2**30))
         stop = []
         lresult = []
@@ -786,7 +786,5 @@ def tcp_establish(TERMINATE, local_addr, local_port, peer_addr, peer_port):
                 end = True
 
     if not sock:
-        raise OSError, "Error: tcp_establish could not establish connection."
+        raise OSError("Error: tcp_establish could not establish connection.")
     return sock
-
-