Chromium Code Reviews| Index: gerrit_util.py |
| diff --git a/gerrit_util.py b/gerrit_util.py |
| index acaacb2895e64c36ae3b0d8406083ed858fc56f0..e3287a688ff02b12b2d101c33bdc862b543b3906 100755 |
| --- a/gerrit_util.py |
| +++ b/gerrit_util.py |
| @@ -83,28 +83,46 @@ class Authenticator(object): |
| """ |
| 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): |
| + # Assume *.googlesource.com pattern. |
| + parts = host.split('.') |
| + if not parts[0].endswith('-review'): |
| + parts[0] = parts[0] + '-review' |
|
Michael Achenbach
2016/04/13 10:29:13
I'm a fan of
parts[0] += '-review'
up to you
Paweł Hajdan Jr.
2016/04/13 11:41:52
+1
tandrii(chromium)
2016/04/13 11:55:58
Done.
|
| + url = 'https://%s/new-password' % ('.'.join(parts)) |
|
Michael Achenbach
2016/04/13 10:29:13
Maybe
assert not host.startswith('http')
I know i
|
| + 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 +134,17 @@ class NetrcAuthenticator(Authenticator): |
| 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 +178,10 @@ class NetrcAuthenticator(Authenticator): |
| 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 |
|
Michael Achenbach
2016/04/13 10:29:13
Optional: Maybe let the NetrcAuthenticator print a
tandrii(chromium)
2016/04/13 11:55:58
skipping because depot_tool will always be now a c
|
| + |
| class GceAuthenticator(Authenticator): |
| """Authenticator implementation that uses GCE metadata service for token. |