Merge "Use python3 urllib when urllib2 not available"

This commit is contained in:
Conley Owens 2012-11-01 10:13:33 -07:00 committed by Gerrit Code Review
commit e072a92a9b
3 changed files with 59 additions and 28 deletions

View File

@ -23,7 +23,18 @@ try:
except ImportError: except ImportError:
import dummy_threading as _threading import dummy_threading as _threading
import time import time
try:
import urllib2 import urllib2
except ImportError:
# For python3
import urllib.request
import urllib.error
else:
# For python2
import imp
urllib = imp.new_module('urllib')
urllib.request = urllib2
urllib.error = urllib2
from signal import SIGTERM from signal import SIGTERM
from error import GitError, UploadError from error import GitError, UploadError
@ -580,7 +591,7 @@ class Remote(object):
else: else:
try: try:
info_url = u + 'ssh_info' info_url = u + 'ssh_info'
info = urllib2.urlopen(info_url).read() info = urllib.request.urlopen(info_url).read()
if '<' in info: if '<' in info:
# Assume the server gave us some sort of HTML # Assume the server gave us some sort of HTML
# response back, like maybe a login page. # response back, like maybe a login page.
@ -593,9 +604,9 @@ class Remote(object):
else: else:
host, port = info.split() host, port = info.split()
self._review_url = self._SshReviewUrl(userEmail, host, port) self._review_url = self._SshReviewUrl(userEmail, host, port)
except urllib2.HTTPError as e: except urllib.error.HTTPError as e:
raise UploadError('%s: %s' % (self.review, str(e))) raise UploadError('%s: %s' % (self.review, str(e)))
except urllib2.URLError as e: except urllib.error.URLError as e:
raise UploadError('%s: %s' % (self.review, str(e))) raise UploadError('%s: %s' % (self.review, str(e)))
REVIEW_CACHE[u] = self._review_url REVIEW_CACHE[u] = self._review_url

33
main.py
View File

@ -29,7 +29,16 @@ import optparse
import os import os
import sys import sys
import time import time
try:
import urllib2 import urllib2
except ImportError:
# For python3
import urllib.request
else:
# For python2
import imp
urllib = imp.new_module('urllib')
urllib.request = urllib2
from trace import SetTrace from trace import SetTrace
from git_command import git, GitCommand from git_command import git, GitCommand
@ -267,7 +276,7 @@ def _UserAgent():
py_version[0], py_version[1], py_version[2]) py_version[0], py_version[1], py_version[2])
return _user_agent return _user_agent
class _UserAgentHandler(urllib2.BaseHandler): class _UserAgentHandler(urllib.request.BaseHandler):
def http_request(self, req): def http_request(self, req):
req.add_header('User-Agent', _UserAgent()) req.add_header('User-Agent', _UserAgent())
return req return req
@ -289,10 +298,10 @@ def _AddPasswordFromUserInput(handler, msg, req):
return return
handler.passwd.add_password(None, url, user, password) handler.passwd.add_password(None, url, user, password)
class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
def http_error_401(self, req, fp, code, msg, headers): def http_error_401(self, req, fp, code, msg, headers):
_AddPasswordFromUserInput(self, msg, req) _AddPasswordFromUserInput(self, msg, req)
return urllib2.HTTPBasicAuthHandler.http_error_401( return urllib.request.HTTPBasicAuthHandler.http_error_401(
self, req, fp, code, msg, headers) self, req, fp, code, msg, headers)
def http_error_auth_reqed(self, authreq, host, req, headers): def http_error_auth_reqed(self, authreq, host, req, headers):
@ -302,7 +311,7 @@ class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler):
val = val.replace('\n', '') val = val.replace('\n', '')
old_add_header(name, val) old_add_header(name, val)
req.add_header = _add_header req.add_header = _add_header
return urllib2.AbstractBasicAuthHandler.http_error_auth_reqed( return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
self, authreq, host, req, headers) self, authreq, host, req, headers)
except: except:
reset = getattr(self, 'reset_retry_count', None) reset = getattr(self, 'reset_retry_count', None)
@ -312,10 +321,10 @@ class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler):
self.retried = 0 self.retried = 0
raise raise
class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler): class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
def http_error_401(self, req, fp, code, msg, headers): def http_error_401(self, req, fp, code, msg, headers):
_AddPasswordFromUserInput(self, msg, req) _AddPasswordFromUserInput(self, msg, req)
return urllib2.HTTPDigestAuthHandler.http_error_401( return urllib.request.HTTPDigestAuthHandler.http_error_401(
self, req, fp, code, msg, headers) self, req, fp, code, msg, headers)
def http_error_auth_reqed(self, auth_header, host, req, headers): def http_error_auth_reqed(self, auth_header, host, req, headers):
@ -325,7 +334,7 @@ class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler):
val = val.replace('\n', '') val = val.replace('\n', '')
old_add_header(name, val) old_add_header(name, val)
req.add_header = _add_header req.add_header = _add_header
return urllib2.AbstractDigestAuthHandler.http_error_auth_reqed( return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
self, auth_header, host, req, headers) self, auth_header, host, req, headers)
except: except:
reset = getattr(self, 'reset_retry_count', None) reset = getattr(self, 'reset_retry_count', None)
@ -338,7 +347,7 @@ class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler):
def init_http(): def init_http():
handlers = [_UserAgentHandler()] handlers = [_UserAgentHandler()]
mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
try: try:
n = netrc.netrc() n = netrc.netrc()
for host in n.hosts: for host in n.hosts:
@ -354,11 +363,11 @@ def init_http():
if 'http_proxy' in os.environ: if 'http_proxy' in os.environ:
url = os.environ['http_proxy'] url = os.environ['http_proxy']
handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
if 'REPO_CURL_VERBOSE' in os.environ: if 'REPO_CURL_VERBOSE' in os.environ:
handlers.append(urllib2.HTTPHandler(debuglevel=1)) handlers.append(urllib.request.HTTPHandler(debuglevel=1))
handlers.append(urllib2.HTTPSHandler(debuglevel=1)) handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
urllib2.install_opener(urllib2.build_opener(*handlers)) urllib.request.install_opener(urllib.request.build_opener(*handlers))
def _Main(argv): def _Main(argv):
result = 0 result = 0

31
repo
View File

@ -123,7 +123,18 @@ import re
import stat import stat
import subprocess import subprocess
import sys import sys
try:
import urllib2 import urllib2
except ImportError:
# For python3
import urllib.request
import urllib.error
else:
# For python2
import imp
urllib = imp.new_module('urllib')
urllib.request = urllib2
urllib.error = urllib2
home_dot_repo = os.path.expanduser('~/.repoconfig') home_dot_repo = os.path.expanduser('~/.repoconfig')
gpg_dir = os.path.join(home_dot_repo, 'gnupg') gpg_dir = os.path.join(home_dot_repo, 'gnupg')
@ -356,7 +367,7 @@ def _SetConfig(local, name, value):
def _InitHttp(): def _InitHttp():
handlers = [] handlers = []
mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
try: try:
import netrc import netrc
n = netrc.netrc() n = netrc.netrc()
@ -366,16 +377,16 @@ def _InitHttp():
mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
except: except:
pass pass
handlers.append(urllib2.HTTPBasicAuthHandler(mgr)) handlers.append(urllib.request.HTTPBasicAuthHandler(mgr))
handlers.append(urllib2.HTTPDigestAuthHandler(mgr)) handlers.append(urllib.request.HTTPDigestAuthHandler(mgr))
if 'http_proxy' in os.environ: if 'http_proxy' in os.environ:
url = os.environ['http_proxy'] url = os.environ['http_proxy']
handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
if 'REPO_CURL_VERBOSE' in os.environ: if 'REPO_CURL_VERBOSE' in os.environ:
handlers.append(urllib2.HTTPHandler(debuglevel=1)) handlers.append(urllib.request.HTTPHandler(debuglevel=1))
handlers.append(urllib2.HTTPSHandler(debuglevel=1)) handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
urllib2.install_opener(urllib2.build_opener(*handlers)) urllib.request.install_opener(urllib.request.build_opener(*handlers))
def _Fetch(url, local, src, quiet): def _Fetch(url, local, src, quiet):
if not quiet: if not quiet:
@ -424,14 +435,14 @@ def _DownloadBundle(url, local, quiet):
dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b') dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b')
try: try:
try: try:
r = urllib2.urlopen(url) r = urllib.request.urlopen(url)
except urllib2.HTTPError as e: except urllib.error.HTTPError as e:
if e.code == 404: if e.code == 404:
return False return False
print >>sys.stderr, 'fatal: Cannot get %s' % url print >>sys.stderr, 'fatal: Cannot get %s' % url
print >>sys.stderr, 'fatal: HTTP error %s' % e.code print >>sys.stderr, 'fatal: HTTP error %s' % e.code
raise CloneFailure() raise CloneFailure()
except urllib2.URLError as e: except urllib.error.URLError as e:
print >>sys.stderr, 'fatal: Cannot get %s' % url print >>sys.stderr, 'fatal: Cannot get %s' % url
print >>sys.stderr, 'fatal: error %s' % e.reason print >>sys.stderr, 'fatal: error %s' % e.reason
raise CloneFailure() raise CloneFailure()