~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007-2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 errors
24
 
from bzrlib.config import GlobalConfig
25
 
from bzrlib.transport import get_transport
 
23
from __future__ import absolute_import
26
24
 
 
25
from bzrlib import (
 
26
    errors,
 
27
    trace,
 
28
    transport,
 
29
    )
 
30
from bzrlib.config import AuthenticationConfig, GlobalStack
 
31
from bzrlib.i18n import gettext
27
32
 
28
33
LAUNCHPAD_BASE = 'https://launchpad.net/'
29
34
 
33
38
 
34
39
 
35
40
class NoRegisteredSSHKeys(errors.BzrError):
36
 
    _fmt = "The user %(user)s has not registered any SSH keys with Launchpad."
 
41
    _fmt = "The user %(user)s has not registered any SSH keys with Launchpad.\n" \
 
42
        "See <https://launchpad.net/people/+me>"
 
43
 
 
44
 
 
45
class MismatchedUsernames(errors.BzrError):
 
46
 
 
47
    _fmt = ('bazaar.conf and authentication.conf disagree about launchpad'
 
48
            ' account name.  Please re-run launchpad-login.')
37
49
 
38
50
 
39
51
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')
 
52
    """Return the user's Launchpad username.
 
53
 
 
54
    :raises: MismatchedUsername if authentication.conf and bazaar.conf
 
55
        disagree about username.
 
56
    """
 
57
    if _config is None:
 
58
        _config = GlobalStack()
 
59
 
 
60
    username = _config.get('launchpad_username')
 
61
    if username is not None:
 
62
        auth = AuthenticationConfig()
 
63
        auth_username = _get_auth_user(auth)
 
64
        # Auto-upgrading
 
65
        if auth_username is None:
 
66
            trace.note(gettext('Setting ssh/sftp usernames for launchpad.net.'))
 
67
            _set_auth_user(username, auth)
 
68
        elif auth_username != username:
 
69
            raise MismatchedUsernames()
 
70
    return username
 
71
 
 
72
 
 
73
def _set_global_option(username, _config=None):
 
74
    if _config is None:
 
75
        _config = GlobalStack()
 
76
    _config.set('launchpad_username', username)
45
77
 
46
78
 
47
79
def set_lp_login(username, _config=None):
48
80
    """Set the user's Launchpad username"""
49
 
    if _config is None:
50
 
        _config = GlobalConfig()
51
 
 
52
 
    _config.set_user_option('launchpad_username', username)
 
81
    _set_global_option(username, _config)
 
82
    _set_auth_user(username)
 
83
 
 
84
 
 
85
def _get_auth_user(auth=None):
 
86
    if auth is None:
 
87
        auth = AuthenticationConfig()
 
88
    username = auth.get_user('ssh', '.launchpad.net')
 
89
    return username
 
90
 
 
91
def _set_auth_user(username, auth=None):
 
92
    if auth is None:
 
93
        auth = AuthenticationConfig()
 
94
    auth.set_credentials(
 
95
        'Launchpad', '.launchpad.net', username, 'ssh')
53
96
 
54
97
 
55
98
def check_lp_login(username, _transport=None):
59
102
    uploaded SSH keys.
60
103
    """
61
104
    if _transport is None:
62
 
        _transport = get_transport(LAUNCHPAD_BASE)
 
105
        _transport = transport.get_transport_from_url(LAUNCHPAD_BASE)
63
106
 
64
107
    try:
65
108
        data = _transport.get_bytes('~%s/+sshkeys' % username)