Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Unified Diff: gerrit_util.py

Issue 1419173011: depot_tools: add .gitcookies support to gerrit_util (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gerrit_util.py
diff --git a/gerrit_util.py b/gerrit_util.py
index 691daf9182989a93339e8ed4245b4e8f326713e1..bafd60a178acb0860ffb763237feb7df3208feff 100755
--- a/gerrit_util.py
+++ b/gerrit_util.py
@@ -9,6 +9,7 @@ https://gerrit-review.googlesource.com/Documentation/rest-api.html
"""
import base64
+import cookielib
import httplib
import json
import logging
@@ -91,6 +92,7 @@ class NetrcAuthenticator(Authenticator):
def __init__(self):
self.netrc = self._get_netrc()
+ self.gitcookies = self._get_gitcookies()
@staticmethod
def _get_netrc():
@@ -114,8 +116,39 @@ class NetrcAuthenticator(Authenticator):
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
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698