Add support for Python 3
authorZaheer Chothia <zaheer.chothia@gmail.com>
Thu, 18 Oct 2012 13:02:48 +0000 (15:02 +0200)
committerZaheer Chothia <zaheer.chothia@gmail.com>
Thu, 18 Oct 2012 13:03:20 +0000 (15:03 +0200)
bootstrap.py
configure.py
misc/ninja_syntax.py
misc/ninja_test.py

index 3032a9b..1c05094 100755 (executable)
@@ -22,6 +22,14 @@ import shlex
 import shutil
 import subprocess
 
+if sys.version_info[0] == 3:
+    import builtins
+    print_ = getattr(builtins, "print")
+else:
+    def print_(*args):
+        sys.stdout.write(" ".join(str(x) for x in args))
+        sys.stdout.write("\n")
+
 os.chdir(os.path.dirname(os.path.abspath(__file__)))
 
 parser = OptionParser()
@@ -44,11 +52,12 @@ if sys.platform.startswith('freebsd'):
     cflags.append('-I/usr/local/include')
     ldflags.append('-L/usr/local/lib')
 
-print 'Building ninja manually...'
+print_('Building ninja manually...')
 
 try:
     os.mkdir('build')
-except OSError, e:
+except OSError:
+    e = sys.exc_info()[1]
     if e.errno != errno.EEXIST:
         raise
 
@@ -103,7 +112,7 @@ else:
     args.extend(['-o', binary])
 
 if options.verbose:
-    print ' '.join(args)
+    print_(' '.join(args))
 
 run(args)
 
@@ -112,7 +121,7 @@ if options.verbose:
     verbose = ['-v']
 
 if sys.platform.startswith('win32'):
-    print 'Building ninja using itself...'
+    print_('Building ninja using itself...')
     run([sys.executable, 'configure.py', '--with-ninja=%s' % binary] +
         conf_args)
     run(['./' + binary] + verbose)
@@ -124,17 +133,17 @@ if sys.platform.startswith('win32'):
     for obj in glob.glob('*.obj'):
         os.unlink(obj)
 
-    print """
+    print_("""
 Done!
 
 Note: to work around Windows file locking, where you can't rebuild an
 in-use binary, to run ninja after making any changes to build ninja itself
 you should run ninja.bootstrap instead.  Your build is also configured to
 use ninja.bootstrap.exe as the MSVC helper; see the --with-ninja flag of
-the --help output of configure.py."""
+the --help output of configure.py.""")
 else:
-    print 'Building ninja using itself...'
+    print_('Building ninja using itself...')
     run([sys.executable, 'configure.py'] + conf_args)
     run(['./' + binary] + verbose)
     os.unlink(binary)
-    print 'Done!'
+    print_('Done!')
index cb0d45e..f716067 100755 (executable)
@@ -26,6 +26,14 @@ sys.path.insert(0, 'misc')
 
 import ninja_syntax
 
+if sys.version_info[0] == 3:
+    import builtins
+    print_ = getattr(builtins, "print")
+else:
+    def print_(*args):
+        sys.stdout.write(" ".join(str(x) for x in args))
+        sys.stdout.write("\n")
+
 parser = OptionParser()
 platforms = ['linux', 'freebsd', 'solaris', 'mingw', 'windows']
 profilers = ['gmon', 'pprof']
@@ -50,7 +58,7 @@ parser.add_option('--with-ninja', metavar='NAME',
                   default="ninja")
 (options, args) = parser.parse_args()
 if args:
-    print 'ERROR: extra unparsed command-line arguments:', args
+    print_('ERROR: extra unparsed command-line arguments:', args)
     sys.exit(1)
 
 platform = options.platform
@@ -256,7 +264,7 @@ if has_re2c():
     n.build(src('depfile_parser.cc'), 're2c', src('depfile_parser.in.cc'))
     n.build(src('lexer.cc'), 're2c', src('lexer.in.cc'))
 else:
-    print ("warning: A compatible version of re2c (>= 0.11.3) was not found; "
+    print_("warning: A compatible version of re2c (>= 0.11.3) was not found; "
            "changes to src/*.in.cc will not affect your build.")
 n.newline()
 
@@ -436,4 +444,4 @@ if host == 'linux':
 
 n.build('all', 'phony', all_targets)
 
-print 'wrote %s.' % BUILD_FILENAME
+print_('wrote %s.' % BUILD_FILENAME)
index 66babbe..3572dd9 100644 (file)
@@ -71,7 +71,7 @@ class Writer(object):
 
         if variables:
             if isinstance(variables, dict):
-                iterator = variables.iteritems()
+                iterator = iter(variables.items())
             else:
                 iterator = iter(variables)
 
index b56033e..2aef7ff 100755 (executable)
 # limitations under the License.
 
 import unittest
-from StringIO import StringIO
+
+try:
+    from StringIO import StringIO
+except ImportError:
+    from io import StringIO
 
 import ninja_syntax