~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/account.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-10-17 12:55:51 UTC
  • mfrom: (3777.1.18 launchpad-login)
  • Revision ID: pqm@pqm.ubuntu.com-20081017125551-l5zi213vopny82nt
Implement default ssh usernames via launchpad-login (abentley)

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
than once for each place that needs to take it into account.
21
21
"""
22
22
 
23
 
from bzrlib import errors
24
 
from bzrlib.config import GlobalConfig
 
23
from bzrlib import errors, trace
 
24
from bzrlib.config import AuthenticationConfig, GlobalConfig
25
25
from bzrlib.transport import get_transport
26
26
 
27
27
 
36
36
    _fmt = "The user %(user)s has not registered any SSH keys with Launchpad."
37
37
 
38
38
 
 
39
class MismatchedUsernames(errors.BzrError):
 
40
 
 
41
    _fmt = ('bazaar.conf and authentication.conf disagree about launchpad'
 
42
            ' account name.  Please re-run launchpad-login.')
 
43
 
 
44
 
39
45
def get_lp_login(_config=None):
40
 
    """Return the user's Launchpad username"""
41
 
    if _config is None:
42
 
        _config = GlobalConfig()
43
 
 
44
 
    return _config.get_user_option('launchpad_username')
 
46
    """Return the user's Launchpad username.
 
47
 
 
48
    :raises: MismatchedUsername if authentication.conf and bazaar.conf
 
49
        disagree about username.
 
50
    """
 
51
    if _config is None:
 
52
        _config = GlobalConfig()
 
53
 
 
54
    username = _config.get_user_option('launchpad_username')
 
55
    if username is not None:
 
56
        auth = AuthenticationConfig()
 
57
        auth_username = _get_auth_user(auth)
 
58
        # Auto-upgrading
 
59
        if auth_username is None:
 
60
            trace.note('Setting ssh/sftp username for bazaar.launchpad.net.')
 
61
            _set_auth_user(username, auth)
 
62
        elif auth_username != username:
 
63
            raise MismatchedUsernames()
 
64
    return username
 
65
 
 
66
 
 
67
def _set_global_option(username, _config=None):
 
68
    if _config is None:
 
69
        _config = GlobalConfig()
 
70
    _config.set_user_option('launchpad_username', username)
45
71
 
46
72
 
47
73
def set_lp_login(username, _config=None):
48
74
    """Set the user's Launchpad username"""
49
 
    if _config is None:
50
 
        _config = GlobalConfig()
51
 
 
52
 
    _config.set_user_option('launchpad_username', username)
 
75
    _set_global_option(username, _config)
 
76
    _set_auth_user(username)
 
77
 
 
78
 
 
79
def _get_auth_user(auth=None):
 
80
    if auth is None:
 
81
        auth = AuthenticationConfig()
 
82
    return auth.get_user('ssh', 'bazaar.launchpad.net')
 
83
 
 
84
def _set_auth_user(username, auth=None):
 
85
    if auth is None:
 
86
        auth = AuthenticationConfig()
 
87
    auth.set_credentials(
 
88
        'Launchpad', 'bazaar.launchpad.net', username, 'ssh')
53
89
 
54
90
 
55
91
def check_lp_login(username, _transport=None):