Fix python3 compatibility

Use print() function with proper import.

Change-Id: Ib10dfbec18202245031a634fc3f19677fc952e60
This commit is contained in:
Max 2017-12-15 12:15:39 +01:00
parent 120075a6a5
commit 6ccd0785d8
4 changed files with 24 additions and 22 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
__version__ = '0.0.4'
__version__ = '0.0.5'
__all__ = ['obscvty', 'osmoutil', 'osmo_ipa']

View File

@ -16,6 +16,7 @@
#
# VTY helper code for OpenBSC
#
from __future__ import print_function
import re
import socket
import sys, subprocess
@ -31,12 +32,12 @@ Methods: __init__, command, enabled_command, verify, w_verify"""
debug_tcp_sockets = (os.getenv('OSMOPY_DEBUG_TCP_SOCKETS', '0') != '0')
def cmd(what):
print '\n> %s' % what
print('\n> %s' % what)
sys.stdout.flush()
subprocess.call(what, shell=True)
sys.stdout.flush()
sys.stderr.flush()
print ''
print('')
sys.stdout.flush()
def print_used_tcp_sockets():
@ -86,16 +87,16 @@ class VTYInteract(object):
raise
# possibly the binary hasn't launched yet
if debug_tcp_sockets:
print "Connecting socket failed, retrying..."
print("Connecting socket failed, retrying...")
time.sleep(.1)
continue
break
if debug_tcp_sockets:
VTYInteract.all_sockets.append(self.socket)
print "Socket: in %d tries, connected to %s:%d %r (%d sockets open)" % (
print("Socket: in %d tries, connected to %s:%d %r (%d sockets open)" % (
took, self.host, self.port, self.socket,
len(VTYInteract.all_sockets))
len(VTYInteract.all_sockets)))
self.socket.recv(4096)
def _close_socket(self):
@ -108,9 +109,9 @@ class VTYInteract(object):
VTYInteract.all_sockets.remove(self.socket)
except ValueError:
pass
print "Socket: closing %s:%d %r (%d sockets open)" % (
print("Socket: closing %s:%d %r (%d sockets open)" % (
self.host, self.port, self.socket,
len(VTYInteract.all_sockets))
len(VTYInteract.all_sockets)))
self.socket.close()
self.socket = None
@ -248,7 +249,7 @@ class VTYInteract(object):
if loud:
if res != results:
print "Rec: %s\nExp: %s" % (res, results)
print("Rec: %s\nExp: %s" % (res, results))
return res == results

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import subprocess
import os
import sys
@ -30,10 +31,10 @@ def popen_devnull(cmd, verbose=True):
global devnull
if devnull is None:
if verbose:
print "Opening /dev/null"
print("Opening /dev/null")
devnull = open(os.devnull, 'w')
if verbose:
print "Launching: PWD=%s %s" % (os.getcwd(), ' '.join([repr(c) for c in cmd]))
print("Launching: PWD=%s %s" % (os.getcwd(), ' '.join([repr(c) for c in cmd])))
return subprocess.Popen(cmd, stdout=devnull, stderr=devnull)
@ -65,9 +66,9 @@ def end_proc(proc):
if proc.poll() is None:
# termination seems to be slower than that, let's just kill
proc.kill()
print "Killed child process"
print("Killed child process")
elif waited_time > .002:
print "Terminating took %.3fs" % waited_time
print("Terminating took %.3fs" % waited_time)
proc.wait()
@ -80,9 +81,9 @@ def importappconf_or_quit(dirname, confname, p_set):
return importlib.import_module(confname)
except ImportError as e:
if p_set:
print >> sys.stderr, "osmoappdesc not found in %s" % dirname
print("osmoappdesc not found in %s" % dirname, file=sys.stderr)
else:
print >> sys.stderr, "set osmoappdesc location with -p <dir>"
print("set osmoappdesc location with -p <dir>", file=sys.stderr)
sys.exit(1)

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
"""
from __future__ import print_function
from optparse import OptionParser
from osmopy.osmo_ipa import Ctrl
import socket
@ -30,7 +30,7 @@ verbose = False
def connect(host, port):
if verbose:
print "Connecting to host %s:%i" % (host, port)
print("Connecting to host %s:%i" % (host, port))
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.setblocking(1)
@ -57,13 +57,13 @@ def _leftovers(sck, fl):
"""
try:
data = sck.recv(1024, fl)
except socket.error as (s_errno, strerror):
except socket.error as _:
return False
if len(data) != 0:
tail = data
while True:
(head, tail) = Ctrl().split_combined(tail)
print "Got message:", Ctrl().rem_header(head)
print("Got message:", Ctrl().rem_header(head))
if len(tail) == 0:
break
return True
@ -103,18 +103,18 @@ if __name__ == '__main__':
if len(args) < 2:
parser.error("Set requires var and value arguments")
_leftovers(sock, socket.MSG_DONTWAIT)
print "Got message:", set_var(sock, args[0], ' '.join(args[1:]))
print("Got message:", set_var(sock, args[0], ' '.join(args[1:])))
if options.cmd_get:
if len(args) != 1:
parser.error("Get requires the var argument")
_leftovers(sock, socket.MSG_DONTWAIT)
(a, _, _) = do_set_get(sock, args[0])
print "Got message:", a
print("Got message:", a)
if options.monitor:
while True:
if not _leftovers(sock, 0):
print "Connection is gone."
print("Connection is gone.")
break
sock.close()