summaryrefslogtreecommitdiffstats
path: root/main.py
diff options
context:
space:
mode:
authorSarah Owens <sarato@inkylabs.com>2012-10-31 09:21:55 -0700
committerSarah Owens <sarato@inkylabs.com>2012-10-31 14:26:48 -0700
commit1f7627fd3ccab0fbab88ad2d082b67f5719af92c (patch)
tree3c03eb8efbdc001472f09fa31cf11bf1d2ec01c5 /main.py
parent1d947b30342163b723c96db563967323535fef45 (diff)
downloadgit-repo-1f7627fd3ccab0fbab88ad2d082b67f5719af92c.tar.gz
Use python3 urllib when urllib2 not available
This is part of a series of changes to introduce Python3 support. Change-Id: I605b145791053c1f2d7bf3c907c5a68649b21d12
Diffstat (limited to 'main.py')
-rwxr-xr-xmain.py35
1 files changed, 22 insertions, 13 deletions
diff --git a/main.py b/main.py
index 7a09c6ba..10e1cf8c 100755
--- a/main.py
+++ b/main.py
@@ -29,7 +29,16 @@ import optparse
29import os 29import os
30import sys 30import sys
31import time 31import time
32import urllib2 32try:
33 import urllib2
34except ImportError:
35 # For python3
36 import urllib.request
37else:
38 # For python2
39 import imp
40 urllib = imp.new_module('urllib')
41 urllib.request = urllib2
33 42
34from trace import SetTrace 43from trace import SetTrace
35from git_command import git, GitCommand 44from git_command import git, GitCommand
@@ -267,7 +276,7 @@ def _UserAgent():
267 py_version[0], py_version[1], py_version[2]) 276 py_version[0], py_version[1], py_version[2])
268 return _user_agent 277 return _user_agent
269 278
270class _UserAgentHandler(urllib2.BaseHandler): 279class _UserAgentHandler(urllib.request.BaseHandler):
271 def http_request(self, req): 280 def http_request(self, req):
272 req.add_header('User-Agent', _UserAgent()) 281 req.add_header('User-Agent', _UserAgent())
273 return req 282 return req
@@ -289,10 +298,10 @@ def _AddPasswordFromUserInput(handler, msg, req):
289 return 298 return
290 handler.passwd.add_password(None, url, user, password) 299 handler.passwd.add_password(None, url, user, password)
291 300
292class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): 301class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
293 def http_error_401(self, req, fp, code, msg, headers): 302 def http_error_401(self, req, fp, code, msg, headers):
294 _AddPasswordFromUserInput(self, msg, req) 303 _AddPasswordFromUserInput(self, msg, req)
295 return urllib2.HTTPBasicAuthHandler.http_error_401( 304 return urllib.request.HTTPBasicAuthHandler.http_error_401(
296 self, req, fp, code, msg, headers) 305 self, req, fp, code, msg, headers)
297 306
298 def http_error_auth_reqed(self, authreq, host, req, headers): 307 def http_error_auth_reqed(self, authreq, host, req, headers):
@@ -302,7 +311,7 @@ class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler):
302 val = val.replace('\n', '') 311 val = val.replace('\n', '')
303 old_add_header(name, val) 312 old_add_header(name, val)
304 req.add_header = _add_header 313 req.add_header = _add_header
305 return urllib2.AbstractBasicAuthHandler.http_error_auth_reqed( 314 return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
306 self, authreq, host, req, headers) 315 self, authreq, host, req, headers)
307 except: 316 except:
308 reset = getattr(self, 'reset_retry_count', None) 317 reset = getattr(self, 'reset_retry_count', None)
@@ -312,10 +321,10 @@ class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler):
312 self.retried = 0 321 self.retried = 0
313 raise 322 raise
314 323
315class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler): 324class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
316 def http_error_401(self, req, fp, code, msg, headers): 325 def http_error_401(self, req, fp, code, msg, headers):
317 _AddPasswordFromUserInput(self, msg, req) 326 _AddPasswordFromUserInput(self, msg, req)
318 return urllib2.HTTPDigestAuthHandler.http_error_401( 327 return urllib.request.HTTPDigestAuthHandler.http_error_401(
319 self, req, fp, code, msg, headers) 328 self, req, fp, code, msg, headers)
320 329
321 def http_error_auth_reqed(self, auth_header, host, req, headers): 330 def http_error_auth_reqed(self, auth_header, host, req, headers):
@@ -325,7 +334,7 @@ class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler):
325 val = val.replace('\n', '') 334 val = val.replace('\n', '')
326 old_add_header(name, val) 335 old_add_header(name, val)
327 req.add_header = _add_header 336 req.add_header = _add_header
328 return urllib2.AbstractDigestAuthHandler.http_error_auth_reqed( 337 return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
329 self, auth_header, host, req, headers) 338 self, auth_header, host, req, headers)
330 except: 339 except:
331 reset = getattr(self, 'reset_retry_count', None) 340 reset = getattr(self, 'reset_retry_count', None)
@@ -338,7 +347,7 @@ class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler):
338def init_http(): 347def init_http():
339 handlers = [_UserAgentHandler()] 348 handlers = [_UserAgentHandler()]
340 349
341 mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() 350 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
342 try: 351 try:
343 n = netrc.netrc() 352 n = netrc.netrc()
344 for host in n.hosts: 353 for host in n.hosts:
@@ -354,11 +363,11 @@ def init_http():
354 363
355 if 'http_proxy' in os.environ: 364 if 'http_proxy' in os.environ:
356 url = os.environ['http_proxy'] 365 url = os.environ['http_proxy']
357 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) 366 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
358 if 'REPO_CURL_VERBOSE' in os.environ: 367 if 'REPO_CURL_VERBOSE' in os.environ:
359 handlers.append(urllib2.HTTPHandler(debuglevel=1)) 368 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
360 handlers.append(urllib2.HTTPSHandler(debuglevel=1)) 369 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
361 urllib2.install_opener(urllib2.build_opener(*handlers)) 370 urllib.request.install_opener(urllib.request.build_opener(*handlers))
362 371
363def _Main(argv): 372def _Main(argv):
364 result = 0 373 result = 0