Gerrit git cl upload: add check for missing credentials.
Checks creds before uploading and running presubmit, generalizing
the case of Rietveld. If they are missing, suggests a URL to
generate them.
R=andybons@chromium.org,phajdan.jr@chromium.org
BUG=583153
Review URL: https://codereview.chromium.org/1882583003
git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@299883 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/gerrit_util.py b/gerrit_util.py
index acaacb2..53dc69c 100755
--- a/gerrit_util.py
+++ b/gerrit_util.py
@@ -83,28 +83,47 @@
"""
if GceAuthenticator.is_gce():
return GceAuthenticator()
- return NetrcAuthenticator()
+ return CookiesAuthenticator()
-class NetrcAuthenticator(Authenticator):
- """Authenticator implementation that uses ".netrc" for token.
+class CookiesAuthenticator(Authenticator):
+ """Authenticator implementation that uses ".netrc" or ".gitcookies" for token.
+
+ Expected case for developer workstations.
"""
def __init__(self):
self.netrc = self._get_netrc()
self.gitcookies = self._get_gitcookies()
- @staticmethod
- def _get_netrc():
+ @classmethod
+ def get_new_password_message(cls, host):
+ assert not host.startswith('http')
+ # Assume *.googlesource.com pattern.
+ parts = host.split('.')
+ if not parts[0].endswith('-review'):
+ parts[0] += '-review'
+ url = 'https://%s/new-password' % ('.'.join(parts))
+ return 'You can (re)generate your credentails by visiting %s' % url
+
+ @classmethod
+ def get_netrc_path(cls):
path = '_netrc' if sys.platform.startswith('win') else '.netrc'
- path = os.path.expanduser(os.path.join('~', path))
+ return os.path.expanduser(os.path.join('~', path))
+
+ @classmethod
+ def _get_netrc(cls):
+ path = cls.get_netrc_path()
+ if not os.path.exists(path):
+ return netrc.netrc(os.devnull)
+
try:
return netrc.netrc(path)
except IOError:
print >> sys.stderr, 'WARNING: Could not read netrc file %s' % path
return netrc.netrc(os.devnull)
- except netrc.NetrcParseError as e:
- st = os.stat(e.path)
+ except netrc.NetrcParseError:
+ st = os.stat(path)
if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO):
print >> sys.stderr, (
'WARNING: netrc file %s cannot be used because its file '
@@ -116,10 +135,17 @@
raise
return netrc.netrc(os.devnull)
- @staticmethod
- def _get_gitcookies():
+ @classmethod
+ def get_gitcookies_path(cls):
+ return os.path.join(os.environ['HOME'], '.gitcookies')
+
+ @classmethod
+ def _get_gitcookies(cls):
gitcookies = {}
- path = os.path.join(os.environ['HOME'], '.gitcookies')
+ path = cls.get_gitcookies_path()
+ if not os.path.exists(path):
+ return gitcookies
+
try:
f = open(path, 'rb')
except IOError:
@@ -153,6 +179,10 @@
return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2])))
return None
+# Backwards compatibility just in case somebody imports this outside of
+# depot_tools.
+NetrcAuthenticator = CookiesAuthenticator
+
class GceAuthenticator(Authenticator):
"""Authenticator implementation that uses GCE metadata service for token.