diff options
Diffstat (limited to 'git_config.py')
| -rw-r--r-- | git_config.py | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/git_config.py b/git_config.py index 56cc6a24..a294a0b6 100644 --- a/git_config.py +++ b/git_config.py | |||
| @@ -14,8 +14,9 @@ | |||
| 14 | # limitations under the License. | 14 | # limitations under the License. |
| 15 | 15 | ||
| 16 | from __future__ import print_function | 16 | from __future__ import print_function |
| 17 | import cPickle | 17 | |
| 18 | import os | 18 | import os |
| 19 | import pickle | ||
| 19 | import re | 20 | import re |
| 20 | import subprocess | 21 | import subprocess |
| 21 | import sys | 22 | import sys |
| @@ -24,14 +25,13 @@ try: | |||
| 24 | except ImportError: | 25 | except ImportError: |
| 25 | import dummy_threading as _threading | 26 | import dummy_threading as _threading |
| 26 | import time | 27 | import time |
| 27 | try: | 28 | |
| 28 | import urllib2 | 29 | from pyversion import is_python3 |
| 29 | except ImportError: | 30 | if is_python3(): |
| 30 | # For python3 | ||
| 31 | import urllib.request | 31 | import urllib.request |
| 32 | import urllib.error | 32 | import urllib.error |
| 33 | else: | 33 | else: |
| 34 | # For python2 | 34 | import urllib2 |
| 35 | import imp | 35 | import imp |
| 36 | urllib = imp.new_module('urllib') | 36 | urllib = imp.new_module('urllib') |
| 37 | urllib.request = urllib2 | 37 | urllib.request = urllib2 |
| @@ -40,6 +40,10 @@ else: | |||
| 40 | from signal import SIGTERM | 40 | from signal import SIGTERM |
| 41 | from error import GitError, UploadError | 41 | from error import GitError, UploadError |
| 42 | from trace import Trace | 42 | from trace import Trace |
| 43 | if is_python3(): | ||
| 44 | from http.client import HTTPException | ||
| 45 | else: | ||
| 46 | from httplib import HTTPException | ||
| 43 | 47 | ||
| 44 | from git_command import GitCommand | 48 | from git_command import GitCommand |
| 45 | from git_command import ssh_sock | 49 | from git_command import ssh_sock |
| @@ -262,7 +266,7 @@ class GitConfig(object): | |||
| 262 | Trace(': unpickle %s', self.file) | 266 | Trace(': unpickle %s', self.file) |
| 263 | fd = open(self._pickle, 'rb') | 267 | fd = open(self._pickle, 'rb') |
| 264 | try: | 268 | try: |
| 265 | return cPickle.load(fd) | 269 | return pickle.load(fd) |
| 266 | finally: | 270 | finally: |
| 267 | fd.close() | 271 | fd.close() |
| 268 | except EOFError: | 272 | except EOFError: |
| @@ -271,7 +275,7 @@ class GitConfig(object): | |||
| 271 | except IOError: | 275 | except IOError: |
| 272 | os.remove(self._pickle) | 276 | os.remove(self._pickle) |
| 273 | return None | 277 | return None |
| 274 | except cPickle.PickleError: | 278 | except pickle.PickleError: |
| 275 | os.remove(self._pickle) | 279 | os.remove(self._pickle) |
| 276 | return None | 280 | return None |
| 277 | 281 | ||
| @@ -279,13 +283,13 @@ class GitConfig(object): | |||
| 279 | try: | 283 | try: |
| 280 | fd = open(self._pickle, 'wb') | 284 | fd = open(self._pickle, 'wb') |
| 281 | try: | 285 | try: |
| 282 | cPickle.dump(cache, fd, cPickle.HIGHEST_PROTOCOL) | 286 | pickle.dump(cache, fd, pickle.HIGHEST_PROTOCOL) |
| 283 | finally: | 287 | finally: |
| 284 | fd.close() | 288 | fd.close() |
| 285 | except IOError: | 289 | except IOError: |
| 286 | if os.path.exists(self._pickle): | 290 | if os.path.exists(self._pickle): |
| 287 | os.remove(self._pickle) | 291 | os.remove(self._pickle) |
| 288 | except cPickle.PickleError: | 292 | except pickle.PickleError: |
| 289 | if os.path.exists(self._pickle): | 293 | if os.path.exists(self._pickle): |
| 290 | os.remove(self._pickle) | 294 | os.remove(self._pickle) |
| 291 | 295 | ||
| @@ -537,8 +541,8 @@ class Remote(object): | |||
| 537 | self.url = self._Get('url') | 541 | self.url = self._Get('url') |
| 538 | self.review = self._Get('review') | 542 | self.review = self._Get('review') |
| 539 | self.projectname = self._Get('projectname') | 543 | self.projectname = self._Get('projectname') |
| 540 | self.fetch = map(RefSpec.FromString, | 544 | self.fetch = list(map(RefSpec.FromString, |
| 541 | self._Get('fetch', all_keys=True)) | 545 | self._Get('fetch', all_keys=True))) |
| 542 | self._review_url = None | 546 | self._review_url = None |
| 543 | 547 | ||
| 544 | def _InsteadOf(self): | 548 | def _InsteadOf(self): |
| @@ -592,14 +596,11 @@ class Remote(object): | |||
| 592 | try: | 596 | try: |
| 593 | info_url = u + 'ssh_info' | 597 | info_url = u + 'ssh_info' |
| 594 | info = urllib.request.urlopen(info_url).read() | 598 | info = urllib.request.urlopen(info_url).read() |
| 595 | if '<' in info: | 599 | if info == 'NOT_AVAILABLE' or '<' in info: |
| 596 | # Assume the server gave us some sort of HTML | 600 | # If `info` contains '<', we assume the server gave us some sort |
| 597 | # response back, like maybe a login page. | 601 | # of HTML response back, like maybe a login page. |
| 598 | # | 602 | # |
| 599 | raise UploadError('%s: Cannot parse response' % info_url) | 603 | # Assume HTTP if SSH is not enabled or ssh_info doesn't look right. |
| 600 | |||
| 601 | if info == 'NOT_AVAILABLE': | ||
| 602 | # Assume HTTP if SSH is not enabled. | ||
| 603 | self._review_url = http_url + 'p/' | 604 | self._review_url = http_url + 'p/' |
| 604 | else: | 605 | else: |
| 605 | host, port = info.split() | 606 | host, port = info.split() |
| @@ -608,6 +609,8 @@ class Remote(object): | |||
| 608 | raise UploadError('%s: %s' % (self.review, str(e))) | 609 | raise UploadError('%s: %s' % (self.review, str(e))) |
| 609 | except urllib.error.URLError as e: | 610 | except urllib.error.URLError as e: |
| 610 | raise UploadError('%s: %s' % (self.review, str(e))) | 611 | raise UploadError('%s: %s' % (self.review, str(e))) |
| 612 | except HTTPException as e: | ||
| 613 | raise UploadError('%s: %s' % (self.review, e.__class__.__name__)) | ||
| 611 | 614 | ||
| 612 | REVIEW_CACHE[u] = self._review_url | 615 | REVIEW_CACHE[u] = self._review_url |
| 613 | return self._review_url + self.projectname | 616 | return self._review_url + self.projectname |
| @@ -657,7 +660,7 @@ class Remote(object): | |||
| 657 | self._Set('url', self.url) | 660 | self._Set('url', self.url) |
| 658 | self._Set('review', self.review) | 661 | self._Set('review', self.review) |
| 659 | self._Set('projectname', self.projectname) | 662 | self._Set('projectname', self.projectname) |
| 660 | self._Set('fetch', map(str, self.fetch)) | 663 | self._Set('fetch', list(map(str, self.fetch))) |
| 661 | 664 | ||
| 662 | def _Set(self, key, value): | 665 | def _Set(self, key, value): |
| 663 | key = 'remote.%s.%s' % (self.name, key) | 666 | key = 'remote.%s.%s' % (self.name, key) |
