Make gen.py compatible with python3

Convert subprocess output from bytes to string using decode(). This
allows a string-based regex to be used.

Open last-commit-position.h using text mode instead of binary. This
causes f.read() to return a string, and allows f.write() to accept a
string.

Use dict.items() instead of dict.iteritems(). This makes the code
slightly slower under python2, but gen.py is rarely invoked anyway.

Change-Id: I0eb9aec758433b0cd26fdb4647bd56337ca89f4b
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/4400
Reviewed-by: Dirk Pranke <dpranke@google.com>
Commit-Queue: Dirk Pranke <dpranke@google.com>
diff --git a/build/gen.py b/build/gen.py
index a7142fa..3edefa7 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -119,7 +119,7 @@
   describe_output = subprocess.check_output(
       ['git', 'describe', 'HEAD', '--match', ROOT_TAG], shell=host.is_windows(),
       cwd=REPO_ROOT)
-  mo = re.match(ROOT_TAG + '-(\d+)-g([0-9a-f]+)', describe_output)
+  mo = re.match(ROOT_TAG + '-(\d+)-g([0-9a-f]+)', describe_output.decode())
   if not mo:
     raise ValueError(
         'Unexpected output from git describe when generating version header')
@@ -137,11 +137,11 @@
   # Only write/touch this file if the commit position has changed.
   old_contents = ''
   if os.path.isfile(header):
-    with open(header, 'rb') as f:
+    with open(header, 'r') as f:
       old_contents = f.read()
 
   if old_contents != contents:
-    with open(header, 'wb') as f:
+    with open(header, 'w') as f:
       f.write(contents)
 
 
@@ -211,7 +211,7 @@
             ' '.join(cflags_cc + settings.get('cflags_cc', [])),
     ])
 
-  for library, settings in static_libraries.iteritems():
+  for library, settings in static_libraries.items():
     for src_file in settings['sources']:
       build_source(src_file, settings)
 
@@ -221,7 +221,7 @@
     ninja_lines.append('  libflags = %s' % ' '.join(libflags))
 
 
-  for executable, settings in executables.iteritems():
+  for executable, settings in executables.items():
     for src_file in settings['sources']:
       build_source(src_file, settings)