[PATCH 4 of 4 v2] contrib: revsetbenchmarks use absolute_import and print_function

Pulkit Goyal 7895pulkit at gmail.com
Tue Mar 15 19:06:03 EDT 2016


# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1458083103 -19800
#      Wed Mar 16 04:35:03 2016 +0530
# Node ID ecd3cf6f6e08c4a80fb3d331fc08715d5d96a92e
# Parent  b14fa3626927f058b79df9aa20fab5ecab664f97
contrib: revsetbenchmarks use absolute_import and print_function

diff --git a/contrib/revsetbenchmarks.py b/contrib/revsetbenchmarks.py
--- a/contrib/revsetbenchmarks.py
+++ b/contrib/revsetbenchmarks.py
@@ -8,13 +8,22 @@
 #
 # call with --help for details
 
-import sys
+from __future__ import absolute_import, print_function
+import math
 import os
 import re
-import math
-from subprocess import check_call, Popen, CalledProcessError, STDOUT, PIPE
+import sys
+from subprocess import (
+    CalledProcessError,
+    check_call,
+    PIPE,
+    Popen,
+    STDOUT,
+)
 # cannot use argparse, python 2.7 only
-from optparse import OptionParser
+from optparse import (
+    OptionParser,
+)
 
 DEFAULTVARIANTS = ['plain', 'min', 'max', 'first', 'last',
                    'reverse', 'reverse+first', 'reverse+last',
@@ -36,7 +45,7 @@
         check_output(['make', 'local'],
                      stderr=None)  # suppress output except for error/warning
     except CalledProcessError as exc:
-        print >> sys.stderr, 'update to revision %s failed, aborting' % rev
+        print('update to revision %s failed, aborting'%rev, file=sys.stderr)
         sys.exit(exc.returncode)
 
 
@@ -62,11 +71,11 @@
         output = hg(args, repo=target)
         return parseoutput(output)
     except CalledProcessError as exc:
-        print >> sys.stderr, 'abort: cannot run revset benchmark: %s' % exc.cmd
+        print('abort: cannot run revset benchmark: %s'%exc.cmd, file=sys.stderr)
         if getattr(exc, 'output', None) is None: # no output before 2.7
-            print >> sys.stderr, '(no output)'
+            print('(no output)', file=sys.stderr)
         else:
-            print >> sys.stderr, exc.output
+            print(exc.output, file=sys.stderr)
         return None
 
 outputre = re.compile(r'! wall (\d+.\d+) comb (\d+.\d+) user (\d+.\d+) '
@@ -80,8 +89,8 @@
     """
     match = outputre.search(output)
     if not match:
-        print >> sys.stderr, 'abort: invalid output:'
-        print >> sys.stderr, output
+        print('abort: invalid output:', file=sys.stderr)
+        print(output, file=sys.stderr)
         sys.exit(1)
     return {'comb': float(match.group(2)),
             'count': int(match.group(5)),
@@ -183,7 +192,7 @@
             out.append(formattiming(data[var]['user']))
             out.append(formattiming(data[var]['sys']))
             out.append('%6d'    % data[var]['count'])
-    print mask % (idx, ' '.join(out))
+    print(mask % (idx, ' '.join(out)))
 
 def printheader(variants, maxidx, verbose=False, relative=False):
     header = [' ' * (idxwidth(maxidx) + 1)]
@@ -200,14 +209,14 @@
             header.append('%-8s' % 'user')
             header.append('%-8s' % 'sys')
             header.append('%6s' % 'count')
-    print ' '.join(header)
+    print(' '.join(header))
 
 def getrevs(spec):
     """get the list of rev matched by a revset"""
     try:
         out = check_output(['hg', 'log', '--template={rev}\n', '--rev', spec])
     except CalledProcessError as exc:
-        print >> sys.stderr, "abort, can't get revision from %s" % spec
+        print("abort, can't get revision from %s"%spec, file=sys.stderr)
         sys.exit(exc.returncode)
     return [r for r in out.split() if r]
 
@@ -261,14 +270,14 @@
 revsets = [l.strip() for l in revsetsfile if not l.startswith('#')]
 revsets = [l for l in revsets if l]
 
-print "Revsets to benchmark"
-print "----------------------------"
+print("Revsets to benchmark")
+print("----------------------------")
 
 for idx, rset in enumerate(revsets):
-    print "%i) %s" % (idx, rset)
+    print("%i) %s" % (idx, rset))
 
-print "----------------------------"
-print
+print("----------------------------")
+print()
 
 revs = []
 for a in args:
@@ -278,9 +287,9 @@
 
 results = []
 for r in revs:
-    print "----------------------------"
+    print("----------------------------")
     printrevision(r)
-    print "----------------------------"
+    print("----------------------------")
     update(r)
     res = []
     results.append(res)
@@ -295,31 +304,31 @@
         printresult(variants, idx, varres, len(revsets),
                     verbose=options.verbose)
         sys.stdout.flush()
-    print "----------------------------"
+    print("----------------------------")
 
 
-print """
+print("""
 
 Result by revset
 ================
-"""
+""")
 
-print 'Revision:'
+print('Revision:')
 for idx, rev in enumerate(revs):
     sys.stdout.write('%i) ' % idx)
     sys.stdout.flush()
     printrevision(rev)
 
-print
-print
+print()
+print()
 
 for ridx, rset in enumerate(revsets):
 
-    print "revset #%i: %s" % (ridx, rset)
+    print("revset #%i: %s" % (ridx, rset))
     printheader(variants, len(results), verbose=options.verbose, relative=True)
     ref = None
     for idx, data in enumerate(results):
         printresult(variants, idx, data[ridx], len(results),
                     verbose=options.verbose, reference=ref)
         ref = data[ridx]
-    print
+    print()
diff --git a/tests/test-check-py3-compat.t b/tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t
+++ b/tests/test-check-py3-compat.t
@@ -5,8 +5,6 @@
   $ hg files 'set:(**.py)' | sed 's|\\|/|g' | xargs python contrib/check-py3-compat.py
   contrib/import-checker.py not using absolute_import
   contrib/import-checker.py requires print_function
-  contrib/revsetbenchmarks.py not using absolute_import
-  contrib/revsetbenchmarks.py requires print_function
   doc/check-seclevel.py not using absolute_import
   doc/gendoc.py not using absolute_import
   doc/hgmanpage.py not using absolute_import


More information about the Mercurial-devel mailing list