27fcad
make btt scripts python3-ready
27fcad
27fcad
Many distributions are moving to python3 by default.  Here's
27fcad
an attempt to make the python scripts in blktrace python3-ready.
27fcad
27fcad
Most of this was done with automated tools.  I hand fixed some
27fcad
space-vs tab issues, and cast an array index to integer.  It
27fcad
passes rudimentary testing when run under python2.7 as well
27fcad
as python3.
27fcad
27fcad
This doesn't do anything with the shebangs, it leaves them both
27fcad
invoking whatever "env python" coughs up on the system.
27fcad
27fcad
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
27fcad
---
27fcad
27fcad
I am not a python guru at all. Happy to have review by anyone more
27fcad
pythonic than I am.  Hopefully this helps at least move things
27fcad
toward python3-readiness.  Thanks!
27fcad
27fcad
Index: blktrace-1.2.0/btt/bno_plot.py
27fcad
===================================================================
27fcad
--- blktrace-1.2.0.orig/btt/bno_plot.py
27fcad
+++ blktrace-1.2.0/btt/bno_plot.py
27fcad
@@ -1,4 +1,4 @@
27fcad
-#! /usr/bin/env python
27fcad
+#!/usr/bin/python3
27fcad
 #
27fcad
 # btt blkno plotting interface
27fcad
 #
27fcad
@@ -38,6 +38,8 @@ automatically push the keys under the gr
27fcad
 To exit the plotter, enter 'quit' or ^D at the 'gnuplot> ' prompt.
27fcad
 """
27fcad
 
27fcad
+from __future__ import absolute_import
27fcad
+from __future__ import print_function
27fcad
 import getopt, glob, os, sys, tempfile
27fcad
 
27fcad
 verbose	= 0
27fcad
@@ -60,14 +62,14 @@ def parse_args(in_args):
27fcad
 
27fcad
 	try:
27fcad
 		(opts, args) = getopt.getopt(in_args, s_opts, l_opts)
27fcad
-	except getopt.error, msg:
27fcad
-		print >>sys.stderr, msg
27fcad
-		print >>sys.stderr, __doc__
27fcad
+	except getopt.error as msg:
27fcad
+		print(msg, file=sys.stderr)
27fcad
+		print(__doc__, file=sys.stderr)
27fcad
 		sys.exit(1)
27fcad
 
27fcad
 	for (o, a) in opts:
27fcad
 		if o in ('-h', '--help'):
27fcad
-			print __doc__
27fcad
+			print(__doc__)
27fcad
 			sys.exit(0)
27fcad
 		elif o in ('-v', '--verbose'):
27fcad
 			verbose += 1
27fcad
@@ -84,10 +86,10 @@ if __name__ == '__main__':
27fcad
 	(bnos, keys_below) = parse_args(sys.argv[1:])
27fcad
 
27fcad
 	if verbose:
27fcad
-		print 'Using files:',
27fcad
-		for bno in bnos: print bno,
27fcad
-		if keys_below:	print '\nKeys are to be placed below graph'
27fcad
-		else:		print ''
27fcad
+		print('Using files:', end=' ')
27fcad
+		for bno in bnos: print(bno, end=' ')
27fcad
+		if keys_below:	print('\nKeys are to be placed below graph')
27fcad
+		else:		print('')
27fcad
 
27fcad
 	tmpdir = tempfile.mktemp()
27fcad
 	os.mkdir(tmpdir)
27fcad
@@ -99,7 +101,7 @@ if __name__ == '__main__':
27fcad
 		fo = open(t, 'w')
27fcad
 		for line in open(f, 'r'):
27fcad
 			fld = line.split(None)
27fcad
-			print >>fo, fld[0], fld[1], int(fld[2])-int(fld[1])
27fcad
+			print(fld[0], fld[1], int(fld[2])-int(fld[1]), file=fo)
27fcad
 		fo.close()
27fcad
 
27fcad
 		t = t[t.rfind('/')+1:]
27fcad
@@ -107,16 +109,16 @@ if __name__ == '__main__':
27fcad
 		else:                plot_cmd = "%s,'%s'" % (plot_cmd, t)
27fcad
 
27fcad
 	fo = open('%s/plot.cmds' % tmpdir, 'w')
27fcad
-	print >>fo, cmds
27fcad
-	if len(bnos) > 10 or keys_below: print >>fo, 'set key below'
27fcad
-	print >>fo, plot_cmd
27fcad
+	print(cmds, file=fo)
27fcad
+	if len(bnos) > 10 or keys_below: print('set key below', file=fo)
27fcad
+	print(plot_cmd, file=fo)
27fcad
 	fo.close()
27fcad
 
27fcad
 	pid = os.fork()
27fcad
 	if pid == 0:
27fcad
 		cmd = 'gnuplot %s/plot.cmds -' % tmpdir
27fcad
 
27fcad
-		if verbose: print 'Executing %s' % cmd
27fcad
+		if verbose: print('Executing %s' % cmd)
27fcad
 
27fcad
 		os.chdir(tmpdir)
27fcad
 		os.system(cmd)
27fcad
Index: blktrace-1.2.0/btt/btt_plot.py
27fcad
===================================================================
27fcad
--- blktrace-1.2.0.orig/btt/btt_plot.py
27fcad
+++ blktrace-1.2.0/btt/btt_plot.py
27fcad
@@ -1,4 +1,4 @@
27fcad
-#! /usr/bin/env python
27fcad
+#!/usr/bin/python3
27fcad
 #
27fcad
 # btt_plot.py: Generate matplotlib plots for BTT generate data files
27fcad
 #
27fcad
@@ -55,6 +55,10 @@ Arguments:
27fcad
   but the -o (--output) and -T (--title) options will be ignored.
27fcad
 """
27fcad
 
27fcad
+from __future__ import absolute_import
27fcad
+from __future__ import print_function
27fcad
+import six
27fcad
+from six.moves import range
27fcad
 __author__ = 'Alan D. Brunelle <alan.brunelle@hp.com>'
27fcad
 
27fcad
 #------------------------------------------------------------------------------
27fcad
@@ -82,7 +86,7 @@ get_base 	= lambda file: file[file.find(
27fcad
 def fatal(msg):
27fcad
 	"""Generate fatal error message and exit"""
27fcad
 
27fcad
-	print >>sys.stderr, 'FATAL: %s' % msg
27fcad
+	print('FATAL: %s' % msg, file=sys.stderr)
27fcad
 	sys.exit(1)
27fcad
 
27fcad
 #------------------------------------------------------------------------------
27fcad
@@ -163,7 +167,7 @@ def get_data(files):
27fcad
 		if not os.path.exists(file):
27fcad
 			fatal('%s not found' % file)
27fcad
 		elif verbose:
27fcad
-			print 'Processing %s' % file
27fcad
+			print('Processing %s' % file)
27fcad
 
27fcad
 		xs = []
27fcad
 		ys = []
27fcad
@@ -214,8 +218,8 @@ def parse_args(args):
27fcad
 
27fcad
 	try:
27fcad
 		(opts, args) = getopt.getopt(args[1:], s_opts, l_opts)
27fcad
-	except getopt.error, msg:
27fcad
-		print >>sys.stderr, msg
27fcad
+	except getopt.error as msg:
27fcad
+		print(msg, file=sys.stderr)
27fcad
 		fatal(__doc__)
27fcad
 
27fcad
 	for (o, a) in opts:
27fcad
@@ -293,15 +297,15 @@ def generate_output(type, db):
27fcad
 	def color(idx, style):
27fcad
 		"""Returns a color/symbol type based upon the index passed."""
27fcad
 
27fcad
-                colors = [ 'b', 'g', 'r', 'c', 'm', 'y', 'k' ]
27fcad
+		colors = [ 'b', 'g', 'r', 'c', 'm', 'y', 'k' ]
27fcad
 		l_styles = [ '-', ':', '--', '-.' ]
27fcad
 		m_styles = [ 'o', '+', '.', ',', 's', 'v', 'x', '<', '>' ]
27fcad
 
27fcad
 		color = colors[idx % len(colors)]
27fcad
 		if style == 'line':
27fcad
-			style = l_styles[(idx / len(l_styles)) % len(l_styles)]
27fcad
+			style = l_styles[int((idx / len(l_styles)) % len(l_styles))]
27fcad
 		elif style == 'marker':
27fcad
-			style = m_styles[(idx / len(m_styles)) % len(m_styles)]
27fcad
+			style = m_styles[int((idx / len(m_styles)) % len(m_styles))]
27fcad
 
27fcad
 		return '%s%s' % (color, style)
27fcad
 
27fcad
@@ -314,7 +318,7 @@ def generate_output(type, db):
27fcad
 		ofile = '%s.png' % type
27fcad
 
27fcad
 	if verbose:
27fcad
-		print 'Generating plot into %s' % ofile
27fcad
+		print('Generating plot into %s' % ofile)
27fcad
 
27fcad
 	fig = plt.figure(figsize=plot_size)
27fcad
 	ax = fig.add_subplot(111)
27fcad
@@ -329,7 +333,7 @@ def generate_output(type, db):
27fcad
 		legends = None
27fcad
 
27fcad
 	keys = []
27fcad
-	for file in db.iterkeys():
27fcad
+	for file in six.iterkeys(db):
27fcad
 		if not file in ['min_x', 'max_x', 'min_y', 'max_y']:
27fcad
 			keys.append(file)
27fcad