~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2008-09-11 19:36:38 UTC
  • mfrom: (3703 +trunk)
  • mto: (3705.1.1 trunk2)
  • mto: This revision was merged to the branch mainline in revision 3708.
  • Revision ID: v.ladeuil+lp@free.fr-20080911193638-wtjyc1kcmacc6t1f
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Functions to manage the user's Launchpad user ID.
18
18
 
20
20
than once for each place that needs to take it into account.
21
21
"""
22
22
 
23
 
from bzrlib import (
24
 
    errors,
25
 
    trace,
26
 
    transport,
27
 
    )
28
 
from bzrlib.config import AuthenticationConfig, GlobalConfig
 
23
from bzrlib import errors
 
24
from bzrlib.config import GlobalConfig
 
25
from bzrlib.transport import get_transport
29
26
 
30
27
 
31
28
LAUNCHPAD_BASE = 'https://launchpad.net/'
36
33
 
37
34
 
38
35
class NoRegisteredSSHKeys(errors.BzrError):
39
 
    _fmt = "The user %(user)s has not registered any SSH keys with Launchpad.\n" \
40
 
        "See <https://launchpad.net/people/+me>"
41
 
 
42
 
 
43
 
class MismatchedUsernames(errors.BzrError):
44
 
 
45
 
    _fmt = ('bazaar.conf and authentication.conf disagree about launchpad'
46
 
            ' account name.  Please re-run launchpad-login.')
 
36
    _fmt = "The user %(user)s has not registered any SSH keys with Launchpad."
47
37
 
48
38
 
49
39
def get_lp_login(_config=None):
50
 
    """Return the user's Launchpad username.
51
 
 
52
 
    :raises: MismatchedUsername if authentication.conf and bazaar.conf
53
 
        disagree about username.
54
 
    """
55
 
    if _config is None:
56
 
        _config = GlobalConfig()
57
 
 
58
 
    username = _config.get_user_option('launchpad_username')
59
 
    if username is not None:
60
 
        auth = AuthenticationConfig()
61
 
        auth_username = _get_auth_user(auth)
62
 
        # Auto-upgrading
63
 
        if auth_username is None:
64
 
            trace.note('Setting ssh/sftp usernames for launchpad.net.')
65
 
            _set_auth_user(username, auth)
66
 
        elif auth_username != username:
67
 
            raise MismatchedUsernames()
68
 
    return username
69
 
 
70
 
 
71
 
def _set_global_option(username, _config=None):
72
 
    if _config is None:
73
 
        _config = GlobalConfig()
74
 
    _config.set_user_option('launchpad_username', username)
 
40
    """Return the user's Launchpad username"""
 
41
    if _config is None:
 
42
        _config = GlobalConfig()
 
43
 
 
44
    return _config.get_user_option('launchpad_username')
75
45
 
76
46
 
77
47
def set_lp_login(username, _config=None):
78
48
    """Set the user's Launchpad username"""
79
 
    _set_global_option(username, _config)
80
 
    _set_auth_user(username)
81
 
 
82
 
 
83
 
def _get_auth_user(auth=None):
84
 
    if auth is None:
85
 
        auth = AuthenticationConfig()
86
 
    username = auth.get_user('ssh', '.launchpad.net')
87
 
    return username
88
 
 
89
 
def _set_auth_user(username, auth=None):
90
 
    if auth is None:
91
 
        auth = AuthenticationConfig()
92
 
    auth.set_credentials(
93
 
        'Launchpad', '.launchpad.net', username, 'ssh')
 
49
    if _config is None:
 
50
        _config = GlobalConfig()
 
51
 
 
52
    _config.set_user_option('launchpad_username', username)
94
53
 
95
54
 
96
55
def check_lp_login(username, _transport=None):
100
59
    uploaded SSH keys.
101
60
    """
102
61
    if _transport is None:
103
 
        _transport = transport.get_transport_from_url(LAUNCHPAD_BASE)
 
62
        _transport = get_transport(LAUNCHPAD_BASE)
104
63
 
105
64
    try:
106
65
        data = _transport.get_bytes('~%s/+sshkeys' % username)