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
diff --git a/gerrit_util.py b/gerrit_util.py
index 691daf9..bafd60a 100755
--- a/gerrit_util.py
+++ b/gerrit_util.py
@@ -9,6 +9,7 @@
"""
import base64
+import cookielib
import httplib
import json
import logging
@@ -91,6 +92,7 @@
def __init__(self):
self.netrc = self._get_netrc()
+ self.gitcookies = self._get_gitcookies()
@staticmethod
def _get_netrc():
@@ -114,8 +116,39 @@
raise
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):
- 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:
return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2])))
return None