diff options
author | Conley Owens <cco3@android.com> | 2012-11-01 10:13:33 -0700 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2012-11-01 10:13:34 -0700 |
commit | e072a92a9bb9fdf61bbd1df4e8864f8fd52d5a82 (patch) | |
tree | 1b685c35a064db6605cdf7be0004788362302dd4 | |
parent | 7601ee260887caae6c5df7a7aa0033fdb844b744 (diff) | |
parent | 1f7627fd3ccab0fbab88ad2d082b67f5719af92c (diff) | |
download | git-repo-e072a92a9bb9fdf61bbd1df4e8864f8fd52d5a82.tar.gz |
Merge "Use python3 urllib when urllib2 not available"
-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 |
@@ -123,7 +123,18 @@ import re | |||
123 | import stat | 123 | import stat |
124 | import subprocess | 124 | import subprocess |
125 | import sys | 125 | import sys |
126 | import urllib2 | 126 | try: |
127 | import urllib2 | ||
128 | except ImportError: | ||
129 | # For python3 | ||
130 | import urllib.request | ||
131 | import urllib.error | ||
132 | else: | ||
133 | # For python2 | ||
134 | import imp | ||
135 | urllib = imp.new_module('urllib') | ||
136 | urllib.request = urllib2 | ||
137 | urllib.error = urllib2 | ||
127 | 138 | ||
128 | home_dot_repo = os.path.expanduser('~/.repoconfig') | 139 | home_dot_repo = os.path.expanduser('~/.repoconfig') |
129 | gpg_dir = os.path.join(home_dot_repo, 'gnupg') | 140 | gpg_dir = os.path.join(home_dot_repo, 'gnupg') |
@@ -356,7 +367,7 @@ def _SetConfig(local, name, value): | |||
356 | def _InitHttp(): | 367 | def _InitHttp(): |
357 | handlers = [] | 368 | handlers = [] |
358 | 369 | ||
359 | mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() | 370 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
360 | try: | 371 | try: |
361 | import netrc | 372 | import netrc |
362 | n = netrc.netrc() | 373 | n = netrc.netrc() |
@@ -366,16 +377,16 @@ def _InitHttp(): | |||
366 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) | 377 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
367 | except: | 378 | except: |
368 | pass | 379 | pass |
369 | handlers.append(urllib2.HTTPBasicAuthHandler(mgr)) | 380 | handlers.append(urllib.request.HTTPBasicAuthHandler(mgr)) |
370 | handlers.append(urllib2.HTTPDigestAuthHandler(mgr)) | 381 | handlers.append(urllib.request.HTTPDigestAuthHandler(mgr)) |
371 | 382 | ||
372 | if 'http_proxy' in os.environ: | 383 | if 'http_proxy' in os.environ: |
373 | url = os.environ['http_proxy'] | 384 | url = os.environ['http_proxy'] |
374 | handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) | 385 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
375 | if 'REPO_CURL_VERBOSE' in os.environ: | 386 | if 'REPO_CURL_VERBOSE' in os.environ: |
376 | handlers.append(urllib2.HTTPHandler(debuglevel=1)) | 387 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
377 | handlers.append(urllib2.HTTPSHandler(debuglevel=1)) | 388 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
378 | urllib2.install_opener(urllib2.build_opener(*handlers)) | 389 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
379 | 390 | ||
380 | def _Fetch(url, local, src, quiet): | 391 | def _Fetch(url, local, src, quiet): |
381 | if not quiet: | 392 | if not quiet: |
@@ -424,14 +435,14 @@ def _DownloadBundle(url, local, quiet): | |||
424 | dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b') | 435 | dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b') |
425 | try: | 436 | try: |
426 | try: | 437 | try: |
427 | r = urllib2.urlopen(url) | 438 | r = urllib.request.urlopen(url) |
428 | except urllib2.HTTPError as e: | 439 | except urllib.error.HTTPError as e: |
429 | if e.code == 404: | 440 | if e.code == 404: |
430 | return False | 441 | return False |
431 | print >>sys.stderr, 'fatal: Cannot get %s' % url | 442 | print >>sys.stderr, 'fatal: Cannot get %s' % url |
432 | print >>sys.stderr, 'fatal: HTTP error %s' % e.code | 443 | print >>sys.stderr, 'fatal: HTTP error %s' % e.code |
433 | raise CloneFailure() | 444 | raise CloneFailure() |
434 | except urllib2.URLError as e: | 445 | except urllib.error.URLError as e: |
435 | print >>sys.stderr, 'fatal: Cannot get %s' % url | 446 | print >>sys.stderr, 'fatal: Cannot get %s' % url |
436 | print >>sys.stderr, 'fatal: error %s' % e.reason | 447 | print >>sys.stderr, 'fatal: error %s' % e.reason |
437 | raise CloneFailure() | 448 | raise CloneFailure() |