diff options
| author | Sarah Owens <sarato@inkylabs.com> | 2012-10-31 09:21:55 -0700 |
|---|---|---|
| committer | Sarah Owens <sarato@inkylabs.com> | 2012-10-31 14:26:48 -0700 |
| commit | 1f7627fd3ccab0fbab88ad2d082b67f5719af92c (patch) | |
| tree | 3c03eb8efbdc001472f09fa31cf11bf1d2ec01c5 | |
| parent | 1d947b30342163b723c96db563967323535fef45 (diff) | |
| download | git-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
| -rw-r--r-- | git_config.py | 19 | ||||
| -rwxr-xr-x | main.py | 35 | ||||
| -rwxr-xr-x | repo | 33 |
3 files changed, 59 insertions, 28 deletions
diff --git a/git_config.py b/git_config.py index d6510aae..6589b193 100644 --- a/git_config.py +++ b/git_config.py | |||
| @@ -23,7 +23,18 @@ try: | |||
| 23 | except ImportError: | 23 | except ImportError: |
| 24 | import dummy_threading as _threading | 24 | import dummy_threading as _threading |
| 25 | import time | 25 | import time |
| 26 | import urllib2 | 26 | try: |
| 27 | import urllib2 | ||
| 28 | except ImportError: | ||
| 29 | # For python3 | ||
| 30 | import urllib.request | ||
| 31 | import urllib.error | ||
| 32 | else: | ||
| 33 | # For python2 | ||
| 34 | import imp | ||
| 35 | urllib = imp.new_module('urllib') | ||
| 36 | urllib.request = urllib2 | ||
| 37 | urllib.error = urllib2 | ||
| 27 | 38 | ||
| 28 | from signal import SIGTERM | 39 | from signal import SIGTERM |
| 29 | from error import GitError, UploadError | 40 | from error import GitError, UploadError |
| @@ -580,7 +591,7 @@ class Remote(object): | |||
| 580 | else: | 591 | else: |
| 581 | try: | 592 | try: |
| 582 | info_url = u + 'ssh_info' | 593 | info_url = u + 'ssh_info' |
| 583 | info = urllib2.urlopen(info_url).read() | 594 | info = urllib.request.urlopen(info_url).read() |
| 584 | if '<' in info: | 595 | if '<' in info: |
| 585 | # Assume the server gave us some sort of HTML | 596 | # Assume the server gave us some sort of HTML |
| 586 | # response back, like maybe a login page. | 597 | # response back, like maybe a login page. |
| @@ -593,9 +604,9 @@ class Remote(object): | |||
| 593 | else: | 604 | else: |
| 594 | host, port = info.split() | 605 | host, port = info.split() |
| 595 | self._review_url = self._SshReviewUrl(userEmail, host, port) | 606 | self._review_url = self._SshReviewUrl(userEmail, host, port) |
| 596 | except urllib2.HTTPError as e: | 607 | except urllib.error.HTTPError as e: |
| 597 | raise UploadError('%s: %s' % (self.review, str(e))) | 608 | raise UploadError('%s: %s' % (self.review, str(e))) |
| 598 | except urllib2.URLError as e: | 609 | except urllib.error.URLError as e: |
| 599 | raise UploadError('%s: %s' % (self.review, str(e))) | 610 | raise UploadError('%s: %s' % (self.review, str(e))) |
| 600 | 611 | ||
| 601 | REVIEW_CACHE[u] = self._review_url | 612 | REVIEW_CACHE[u] = self._review_url |
| @@ -29,7 +29,16 @@ import optparse | |||
| 29 | import os | 29 | import os |
| 30 | import sys | 30 | import sys |
| 31 | import time | 31 | import time |
| 32 | import urllib2 | 32 | try: |
| 33 | import urllib2 | ||
| 34 | except ImportError: | ||
| 35 | # For python3 | ||
| 36 | import urllib.request | ||
| 37 | else: | ||
| 38 | # For python2 | ||
| 39 | import imp | ||
| 40 | urllib = imp.new_module('urllib') | ||
| 41 | urllib.request = urllib2 | ||
| 33 | 42 | ||
| 34 | from trace import SetTrace | 43 | from trace import SetTrace |
| 35 | from git_command import git, GitCommand | 44 | from 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 | ||
| 270 | class _UserAgentHandler(urllib2.BaseHandler): | 279 | class _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 | ||
| 292 | class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): | 301 | class _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 | ||
| 315 | class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler): | 324 | class _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): | |||
| 338 | def init_http(): | 347 | def 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 | ||
| 363 | def _Main(argv): | 372 | def _Main(argv): |
| 364 | result = 0 | 373 | result = 0 |
| @@ -122,7 +122,18 @@ import os | |||
| 122 | import re | 122 | import re |
| 123 | import subprocess | 123 | import subprocess |
| 124 | import sys | 124 | import sys |
| 125 | import urllib2 | 125 | try: |
| 126 | import urllib2 | ||
| 127 | except ImportError: | ||
| 128 | # For python3 | ||
| 129 | import urllib.request | ||
| 130 | import urllib.error | ||
| 131 | else: | ||
| 132 | # For python2 | ||
| 133 | import imp | ||
| 134 | urllib = imp.new_module('urllib') | ||
| 135 | urllib.request = urllib2 | ||
| 136 | urllib.error = urllib2 | ||
| 126 | 137 | ||
| 127 | home_dot_repo = os.path.expanduser('~/.repoconfig') | 138 | home_dot_repo = os.path.expanduser('~/.repoconfig') |
| 128 | gpg_dir = os.path.join(home_dot_repo, 'gnupg') | 139 | gpg_dir = os.path.join(home_dot_repo, 'gnupg') |
| @@ -355,7 +366,7 @@ def _SetConfig(local, name, value): | |||
| 355 | def _InitHttp(): | 366 | def _InitHttp(): |
| 356 | handlers = [] | 367 | handlers = [] |
| 357 | 368 | ||
| 358 | mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() | 369 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
| 359 | try: | 370 | try: |
| 360 | import netrc | 371 | import netrc |
| 361 | n = netrc.netrc() | 372 | n = netrc.netrc() |
| @@ -365,16 +376,16 @@ def _InitHttp(): | |||
| 365 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) | 376 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
| 366 | except: | 377 | except: |
| 367 | pass | 378 | pass |
| 368 | handlers.append(urllib2.HTTPBasicAuthHandler(mgr)) | 379 | handlers.append(urllib.request.HTTPBasicAuthHandler(mgr)) |
| 369 | handlers.append(urllib2.HTTPDigestAuthHandler(mgr)) | 380 | handlers.append(urllib.request.HTTPDigestAuthHandler(mgr)) |
| 370 | 381 | ||
| 371 | if 'http_proxy' in os.environ: | 382 | if 'http_proxy' in os.environ: |
| 372 | url = os.environ['http_proxy'] | 383 | url = os.environ['http_proxy'] |
| 373 | handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) | 384 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
| 374 | if 'REPO_CURL_VERBOSE' in os.environ: | 385 | if 'REPO_CURL_VERBOSE' in os.environ: |
| 375 | handlers.append(urllib2.HTTPHandler(debuglevel=1)) | 386 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 376 | handlers.append(urllib2.HTTPSHandler(debuglevel=1)) | 387 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 377 | urllib2.install_opener(urllib2.build_opener(*handlers)) | 388 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
| 378 | 389 | ||
| 379 | def _Fetch(url, local, src, quiet): | 390 | def _Fetch(url, local, src, quiet): |
| 380 | if not quiet: | 391 | if not quiet: |
| @@ -423,14 +434,14 @@ def _DownloadBundle(url, local, quiet): | |||
| 423 | dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b') | 434 | dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b') |
| 424 | try: | 435 | try: |
| 425 | try: | 436 | try: |
| 426 | r = urllib2.urlopen(url) | 437 | r = urllib.request.urlopen(url) |
| 427 | except urllib2.HTTPError as e: | 438 | except urllib.error.HTTPError as e: |
| 428 | if e.code == 404: | 439 | if e.code == 404: |
| 429 | return False | 440 | return False |
| 430 | print >>sys.stderr, 'fatal: Cannot get %s' % url | 441 | print >>sys.stderr, 'fatal: Cannot get %s' % url |
| 431 | print >>sys.stderr, 'fatal: HTTP error %s' % e.code | 442 | print >>sys.stderr, 'fatal: HTTP error %s' % e.code |
| 432 | raise CloneFailure() | 443 | raise CloneFailure() |
| 433 | except urllib2.URLError as e: | 444 | except urllib.error.URLError as e: |
| 434 | print >>sys.stderr, 'fatal: Cannot get %s' % url | 445 | print >>sys.stderr, 'fatal: Cannot get %s' % url |
| 435 | print >>sys.stderr, 'fatal: error %s' % e.reason | 446 | print >>sys.stderr, 'fatal: error %s' % e.reason |
| 436 | raise CloneFailure() | 447 | raise CloneFailure() |
