depot_tools: add .gitcookies support to gerrit_util

BUG=551403

Review URL: https://codereview.chromium.org/1419173011

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@297450 0039d316-1c4b-4281-b951-d872f2087c98
parent 8990189a
...@@ -9,6 +9,7 @@ https://gerrit-review.googlesource.com/Documentation/rest-api.html ...@@ -9,6 +9,7 @@ https://gerrit-review.googlesource.com/Documentation/rest-api.html
""" """
import base64 import base64
import cookielib
import httplib import httplib
import json import json
import logging import logging
...@@ -91,6 +92,7 @@ class NetrcAuthenticator(Authenticator): ...@@ -91,6 +92,7 @@ class NetrcAuthenticator(Authenticator):
def __init__(self): def __init__(self):
self.netrc = self._get_netrc() self.netrc = self._get_netrc()
self.gitcookies = self._get_gitcookies()
@staticmethod @staticmethod
def _get_netrc(): def _get_netrc():
...@@ -114,8 +116,39 @@ class NetrcAuthenticator(Authenticator): ...@@ -114,8 +116,39 @@ class NetrcAuthenticator(Authenticator):
raise raise
return netrc.netrc(os.devnull) return netrc.netrc(os.devnull)
@staticmethod
def _get_gitcookies():
gitcookies = {}
path = os.path.join(os.environ['HOME'], '.gitcookies')
try:
f = open(path, 'rb')
except IOError:
return gitcookies
with f:
for line in f:
try:
fields = line.strip().split('\t')
if line.strip().startswith('#') or len(fields) != 7:
continue
domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6]
if xpath == '/' and key == 'o':
login, secret_token = value.split('=', 1)
gitcookies[domain] = (login, secret_token)
except (IndexError, ValueError, TypeError) as exc:
logging.warning(exc)
return gitcookies
def get_auth_header(self, host): def get_auth_header(self, host):
auth = self.netrc.authenticators(host) auth = None
for domain, creds in self.gitcookies.iteritems():
if cookielib.domain_match(host, domain):
auth = (creds[0], None, creds[1])
break
if not auth:
auth = self.netrc.authenticators(host)
if auth: if auth:
return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2]))) return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2])))
return None return None
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment