From c2ac7c904fb4bc445ddadf5bc7e276635ecce9e4 Mon Sep 17 00:00:00 2001
From: Anton Akhmerov <anton.akhmerov@gmail.com>
Date: Fri, 17 Jul 2015 22:45:39 +0200
Subject: [PATCH] switch to print function, reformat strings

---
 setup.py | 93 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 52 insertions(+), 41 deletions(-)

diff --git a/setup.py b/setup.py
index 93f29da..f0822b2 100755
--- a/setup.py
+++ b/setup.py
@@ -8,15 +8,7 @@
 # the AUTHORS file at the top-level directory of this distribution and at
 # http://kwant-project.org/authors.
 
-CONFIG_FILE = 'build.conf'
-README_FILE = 'README.rst'
-README_END_BEFORE = 'See also in this directory:'
-STATIC_VERSION_FILE = 'kwant/_static_version.py'
-REQUIRED_CYTHON_VERSION = (0, 17, 1)
-NO_CYTHON_OPTION = '--no-cython'
-TUT_DIR = 'tutorial'
-TUT_GLOB = 'doc/source/tutorial/*.py'
-TUT_HIDDEN_PREFIX = '#HIDDEN'
+from __future__ import print_function
 
 import sys
 import re
@@ -30,8 +22,19 @@ from distutils.errors import DistutilsError, DistutilsModuleError, \
     CCompilerError
 from distutils.command.build import build as distutils_build
 from distutils.command.sdist import sdist as distutils_sdist
+
 import numpy
 
+CONFIG_FILE = 'build.conf'
+README_FILE = 'README.rst'
+README_END_BEFORE = 'See also in this directory:'
+STATIC_VERSION_FILE = 'kwant/_static_version.py'
+REQUIRED_CYTHON_VERSION = (0, 17, 1)
+NO_CYTHON_OPTION = '--no-cython'
+TUT_DIR = 'tutorial'
+TUT_GLOB = 'doc/source/tutorial/*.py'
+TUT_HIDDEN_PREFIX = '#HIDDEN'
+
 try:
     import Cython
 except:
@@ -59,6 +62,18 @@ else:
 
 distr_root = os.path.dirname(os.path.abspath(__file__))
 
+def header(title=''):
+    return title.center(79, '*')
+
+error_msg = """{sep}
+The compilation of Kwant has failed.  Please examine the error message
+above and consult the installation instructions in README.rst.
+You might have to customize {{file}}.
+{sep}
+Build configuration was:
+{{summary}}
+{sep}"""
+error_msg = error_msg.format(sep=header())
 
 class kwant_build_ext(build_ext):
     def run(self):
@@ -73,18 +88,11 @@ class kwant_build_ext(build_ext):
         try:
             build_ext.run(self)
         except (DistutilsError, CCompilerError):
-            print >>sys.stderr, \
-"""{0}
-The compilation of Kwant has failed.  Please examine the error message
-above and consult the installation instructions in README.rst.
-You might have to customize {1}.
-{0}
-Build configuration was:
-{2}
-{0}""".format('*' * 70, CONFIG_FILE, build_summary)
+            print(error_msg.format(file=CONFIG_FILE, summary=build_summary),
+                  file=sys.stderr)
             raise
-        print '**************** Build summary ****************'
-        print build_summary
+        print(header(' Build summary '))
+        print(build_summary)
 
 
 class build_tut(Command):
@@ -136,7 +144,7 @@ class test(Command):
         self.run_command('build')
         major, minor = sys.version_info[:2]
         lib_dir = "build/lib.{0}-{1}.{2}".format(get_platform(), major, minor)
-        print '**************** Tests ****************'
+        print(header(' Tests '))
         if not run(argv=[__file__, '-v', lib_dir]):
             raise DistutilsError('at least one of the tests failed')
 
@@ -170,8 +178,8 @@ class kwant_sdist(distutils_sdist):
                 with open(distr_root + '/MANIFEST', 'r') as f:
                     line = f.read()
             except IOError:
-                print >>sys.stderr, "error: MANIFEST file is missing and " \
-                    "Git is not available to regenerate it."
+                print("error: MANIFEST file is missing and Git is not"
+                      " available to regenerate it.", file=sys.stderr)
                 exit(1)
             trustworthy = not line.strip().startswith('#')
         else:
@@ -190,17 +198,20 @@ class kwant_sdist(distutils_sdist):
 
         distutils_sdist.run(self)
 
+        msg = None
         if names is None:
-            print >>sys.stderr, \
-    """**************** Warning ****************
-Git was not available for re-generating the MANIFEST file (the list of file
-names to be included in the source distribution).  The old MANIFEST was used."""
-
+            msg = ['Git was not available for re-generating '
+                   'the MANIFEST file (the list of file',
+                   'names to be included in the source'
+                   ' distribution).  The old MANIFEST was used.']
         if not trustworthy:
-            print >>sys.stderr, \
-    """**************** Warning ****************
-The existing MANIFEST file seems to have been generated by distutils (it begins
-with a comment).  It may well be incomplete."""
+            msg = ['-The existing MANIFEST file seems to'
+                   ' have been generated by distutils (it begins',
+                   '-with a comment).  It may well be incomplete.']
+
+        if msg is not None:
+            msg.insert(0, header(' Warning '))
+            print(msg, sep='\n', file=sys.stderr)
 
 
 # This is an exact copy of the function from kwant/_common.py.  We can't import
@@ -407,10 +418,9 @@ def complain_cython_unavailable():
         msg = "Install Cython {0} or newer so it can be made or use a source " \
             "distribution of Kwant."
         ver = '.'.join(str(e) for e in REQUIRED_CYTHON_VERSION)
-        print >>sys.stderr, msg.format(ver)
+        print(msg.format(ver), file=sys.stderr)
     else:
-        print >>sys.stderr, "Run setup.py without", \
-            NO_CYTHON_OPTION
+        print("Run setup.py without", NO_CYTHON_OPTION, file=sys.stderr)
 
 
 def ext_modules(extensions):
@@ -424,7 +434,7 @@ def ext_modules(extensions):
                 elif kwrds['language'] == 'c++':
                     ext = '.cpp'
                 else:
-                    print >>sys.stderr, 'Unknown language'
+                    print('Unknown language', file=sys.stderr)
                     exit(1)
             else:
                 ext = '.c'
@@ -443,8 +453,8 @@ def ext_modules(extensions):
                 cythonized_oldest = min(os.stat(f).st_mtime
                                         for f in cythonized_files)
             except OSError:
-                print >>sys.stderr, \
-                    "error: Cython-generated file {0} is missing.".format(f)
+                print("error: Cython-generated file {} is missing.".format(f),
+                      file=sys.stderr)
                 complain_cython_unavailable()
                 exit(1)
             for f in pyx_files + kwrds.get('depends', []):
@@ -453,14 +463,14 @@ def ext_modules(extensions):
                     # of the cythonized file, not for the cythonization.
                     continue
                 if os.stat(f).st_mtime > cythonized_oldest:
-                    msg = "error: {0} is newer than its source file, but "
+                    msg = "error: {} is newer than its source file, but "
                     if cythonize and not cython_version:
                         msg += "Cython is not installed."
                     elif cythonize:
                         msg += "the installed Cython is too old."
                     else:
                         msg += "Cython is not to be run."
-                    print >>sys.stderr, msg.format(f)
+                    print(msg.format(f), file=sys.stderr)
                     complain_cython_unavailable()
                     exit(1)
 
@@ -472,7 +482,8 @@ def ext_modules(extensions):
 def main():
     setup(name='kwant',
           version=version(),
-          author='C. W. Groth (CEA), M. Wimmer, A. R. Akhmerov, X. Waintal (CEA), and others',
+          author='C. W. Groth (CEA), M. Wimmer, '
+                 'A. R. Akhmerov, X. Waintal (CEA), and others',
           author_email='authors@kwant-project.org',
           description="Package for numerical quantum transport calculations.",
           long_description=long_description(),
-- 
GitLab