# HG changeset patch # User Marcin Kasperski # Date 2009-11-19 21:59:26 # Node ID 6f5398b69790e80eb71e47ea26269f51833eca19 # Parent 2b07af6723ad860e0bcf4d7d717cc264fb3a5ac9 Working version. Password is actually saved and restored, still errors are not handled. diff --git a/debug/hg.py b/debug/hg.py new file mode 100755 --- /dev/null +++ b/debug/hg.py @@ -0,0 +1,27 @@ +#!/usr/bin/python +# +# mercurial - scalable distributed SCM +# +# Copyright 2005-2007 Matt Mackall +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2, incorporated herein by reference. + +# enable importing on demand to reduce startup time +try: + from mercurial import demandimport; demandimport.enable() +except ImportError: + import sys + sys.stderr.write("abort: couldn't find mercurial libraries in [%s]\n" % + ' '.join(sys.path)) + sys.stderr.write("(check your install and PYTHONPATH)\n") + sys.exit(-1) + +import sys +import mercurial.util +import mercurial.dispatch + +for fp in (sys.stdin, sys.stdout, sys.stderr): + mercurial.util.set_binary(fp) + +mercurial.dispatch.run() diff --git a/mercurial_keyring.py b/mercurial_keyring.py --- a/mercurial_keyring.py +++ b/mercurial_keyring.py @@ -63,15 +63,15 @@ class PasswordStore(object): """ def __init__(self): self.cache = dict() - def save_password(self, url, username, password): - self.cache[url] = (username, password) - # TODO: keyring save - def get_password(self, url): - r = self.cache.get(url) - if r: - return r - # TODO: keyring restore - return None, None + def get_password(self, url, username): + return keyring.get_password(KEYRING_SERVICE, + self._format_key(url, username)) + def set_password(self, url, username, password): + keyring.set_password(KEYRING_SERVICE, + self._format_key(url, username), + password) + def _format_key(self, url, username): + return "%s@@%s" % (username, url) password_store = PasswordStore() @@ -123,23 +123,28 @@ def find_user_password(self, realm, auth local_passwordmgr = passwordmgr(local_ui) auth_token = local_passwordmgr.readauthtoken(base_url) if auth_token: - user, pwd = auth.get('username'), auth.get('password') - - - - - user, pwd = password_store.get_password(base_url) - if user and pwd: - return user, pwd + user, pwd = auth_token.get('username'), auth_token.get('password') - if not self.ui.interactive(): - raise util.Abort(_('mercurial_keyring: http authorization required')) - self.ui.write(_("http authorization required\n")) - self.ui.status(_("realm: %s, url: %s\n" % (realm, base_url))) - user = self.ui.prompt(_("user:"), default = user) - pwd = self.ui.getpass(_("password: ")) + # username still not known? Asking + if not user: + if not self.ui.interactive(): + raise util.Abort(_('mercurial_keyring: http authorization required')) + self.ui.write(_("http authorization required\n")) + self.ui.status(_("realm: %s\n") % realm) + user = self.ui.prompt(_("user:"), default=None) + + # username known and still no password? Time to check keyring + if user and not pwd: + pwd = password_store.get_password(base_url, user) - password_store.save_password(base_url, user, pwd) + # password still not known? Asking + if not pwd: + if not self.ui.interactive(): + raise util.Abort(_('mercurial_keyring: http authorization required')) + pwd = self.ui.getpass(_("password: ")) + password_store.set_password(base_url, user, pwd) + + self._pwd_cache[cache_key] = (user, pwd) return user, pwd #return None, None