Add Python 3 compatibility.

Change-Id: Id09cce1a0db2109ba5f37df03aa544b06bf41dda
diff --git a/git-cookie-authdaemon b/git-cookie-authdaemon
index 2ddad35..a4e7cac 100755
--- a/git-cookie-authdaemon
+++ b/git-cookie-authdaemon
@@ -26,14 +26,23 @@
 
 import atexit
 import contextlib
-import cookielib
 import json
 import os
 import platform
 import subprocess
 import sys
 import time
-import urllib2
+
+if sys.version_info[0] > 2:
+  # Python 3 imports
+  from urllib.request import urlopen, Request
+  from urllib.error import URLError
+  from urllib.error import HTTPError
+  import http.cookiejar as cookielib
+else:
+  # Python 2 imports
+  from urllib2 import urlopen, Request, HTTPError, URLError
+  import cookielib
 
 REFRESH = 25  # seconds remaining when starting refresh
 RETRY_INTERVAL = 5 # seconds between retrying a failed refresh
@@ -50,16 +59,16 @@
 IS_WINDOWS = platform.system() == 'Windows'
 
 def read_meta(part):
-  r = urllib2.Request(META_URL + part)
+  r = Request(META_URL + part)
   r.add_header('Metadata-Flavor', 'Google')
-  return contextlib.closing(urllib2.urlopen(r))
+  return contextlib.closing(urlopen(r))
 
 def select_scope():
   with read_meta('scopes') as d:
-    avail = set(d.read().split())
+    avail = set(map(lambda s: s.decode('utf-8'), d.read().split()))
   scopes = [s for s in SUPPORTED_SCOPES if s in avail]
   if scopes:
-    return iter(scopes).next()
+    return next(iter(scopes))
   sys.stderr.write('error: VM must have one of these scopes:\n\n')
   for s in SUPPORTED_SCOPES:
     sys.stderr.write('  %s\n' % (s))
@@ -103,7 +112,7 @@
     try:
       with read_meta('token?scopes=' + scope) as d:
         return json.load(d)
-    except urllib2.URLError:
+    except URLError:
       if not retry:
         raise
     time.sleep(RETRY_INTERVAL)
diff --git a/git-googlesource-login b/git-googlesource-login
index 0c577cb..ec90104 100755
--- a/git-googlesource-login
+++ b/git-googlesource-login
@@ -22,15 +22,30 @@
 
 from __future__ import print_function
 
-import cookielib
 import os
 from subprocess import Popen, PIPE, check_call
+import sys
+
+if sys.version_info[0] > 2:
+  # Python 3 imports
+  import http.cookiejar as cookielib
+else:
+  # Python 2 imports
+  import cookielib
+
+try:
+  # We want to use raw_input in Python 2.
+  input = raw_input
+except NameError:
+  # raw_input does not exist in in Python 3 and has been
+  # renamed to input.
+  pass
 
 def write_cookie(token):
   git_config = ['git', 'config', '--global', 'http.cookiefile']
   set_config = False
 
-  fn = Popen(git_config, stdout=PIPE).communicate()[0]
+  fn = Popen(git_config, stdout=PIPE).communicate()[0].decode('utf-8')
   if fn == '':
     home = os.environ['HOME']
     fn = os.path.join(home, '.git-credential-cache', 'cookies')
@@ -72,7 +87,7 @@
 
 def main():
   print('Open https://www.googlesource.com/new-password ...')
-  t = raw_input("Password (under 'Login for Git'): ")
+  t = input("Password (under 'Login for Git'): ")
   p = write_cookie(t)
   print("Saved to %s" % (p,))