Make fetch compatible with Python 3

The scripts still work with Python 2.
There are no intended behaviour changes.

Bug: 939847
Change-Id: Icada60c5b2cf351d62aead26b7364fcef2c2a3e5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1524486
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
diff --git a/git_common.py b/git_common.py
index e837519..f5edfb1 100644
--- a/git_common.py
+++ b/git_common.py
@@ -4,6 +4,9 @@
 
 # Monkeypatch IMapIterator so that Ctrl-C can kill everything properly.
 # Derived from https://gist.github.com/aljungberg/626518
+
+from __future__ import print_function
+
 import multiprocessing.pool
 from multiprocessing.pool import IMapIterator
 def wrapper(func):
@@ -32,7 +35,7 @@
 
 import subprocess2
 
-from StringIO import StringIO
+from io import BytesIO
 
 
 ROOT = os.path.abspath(os.path.dirname(__file__))
@@ -310,7 +313,7 @@
 ## Git functions
 
 def die(message, *args):
-  print >> sys.stderr, textwrap.dedent(message % args)
+  print(textwrap.dedent(message % args), file=sys.stderr)
   sys.exit(1)
 
 
@@ -805,14 +808,14 @@
   stat_entry = collections.namedtuple('stat_entry', 'lstat rstat src')
 
   def tokenizer(stream):
-    acc = StringIO()
+    acc = BytesIO()
     c = None
     while c != '':
       c = stream.read(1)
       if c in (None, '', '\0'):
-        if acc.len:
+        if len(acc.getvalue()):
           yield acc.getvalue()
-          acc = StringIO()
+          acc = BytesIO()
       else:
         acc.write(c)
 
@@ -843,7 +846,7 @@
   if not get_dirty_files():
     # Sometimes the squash can result in the same tree, meaning that there is
     # nothing to commit at this point.
-    print 'Nothing to commit; squashed branch is empty'
+    print('Nothing to commit; squashed branch is empty')
     return False
   run('commit', '--no-verify', '-a', '-F', '-', indata=log_msg)
   return True