/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2018-05-19 13:16:11 UTC
  • mto: (6968.4.3 git-archive)
  • mto: This revision was merged to the branch mainline in revision 6972.
  • Revision ID: jelmer@jelmer.uk-20180519131611-l9h9ud41j7qg1m03
Move tar/zip to breezy.archive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
"""Functions to manage the user's Launchpad user ID.
 
18
 
 
19
This allows the user to configure their Launchpad user ID once, rather
 
20
than once for each place that needs to take it into account.
 
21
"""
 
22
 
 
23
from __future__ import absolute_import
 
24
 
 
25
from ... import (
 
26
    errors,
 
27
    trace,
 
28
    transport,
 
29
    )
 
30
from ...config import AuthenticationConfig, GlobalStack
 
31
from ...i18n import gettext
 
32
 
 
33
LAUNCHPAD_BASE = 'https://launchpad.net/'
 
34
 
 
35
 
 
36
class UnknownLaunchpadUsername(errors.BzrError):
 
37
    _fmt = "The user name %(user)s is not registered on Launchpad."
 
38
 
 
39
 
 
40
class NoRegisteredSSHKeys(errors.BzrError):
 
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 = ('breezy.conf and authentication.conf disagree about launchpad'
 
48
            ' account name.  Please re-run launchpad-login.')
 
49
 
 
50
 
 
51
def get_lp_login(_config=None):
 
52
    """Return the user's Launchpad username.
 
53
 
 
54
    :raises: MismatchedUsername if authentication.conf and breezy.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
    if username is None:
 
77
        _config.remove('launchpad_username')
 
78
    else:
 
79
        _config.set('launchpad_username', username)
 
80
 
 
81
 
 
82
def set_lp_login(username, _config=None):
 
83
    """Set the user's Launchpad username"""
 
84
    _set_global_option(username, _config)
 
85
    if username is not None:
 
86
        _set_auth_user(username)
 
87
 
 
88
 
 
89
def _get_auth_user(auth=None):
 
90
    if auth is None:
 
91
        auth = AuthenticationConfig()
 
92
    username = auth.get_user('ssh', '.launchpad.net')
 
93
    return username
 
94
 
 
95
def _set_auth_user(username, auth=None):
 
96
    if auth is None:
 
97
        auth = AuthenticationConfig()
 
98
    auth.set_credentials(
 
99
        'Launchpad', '.launchpad.net', username, 'ssh')
 
100
 
 
101
 
 
102
def check_lp_login(username, _transport=None):
 
103
    """Check whether the given Launchpad username is okay.
 
104
 
 
105
    This will check for both existence and whether the user has
 
106
    uploaded SSH keys.
 
107
    """
 
108
    if _transport is None:
 
109
        _transport = transport.get_transport_from_url(LAUNCHPAD_BASE)
 
110
 
 
111
    try:
 
112
        data = _transport.get_bytes('~%s/+sshkeys' % username)
 
113
    except errors.NoSuchFile:
 
114
        raise UnknownLaunchpadUsername(user=username)
 
115
 
 
116
    if not data:
 
117
        raise NoRegisteredSSHKeys(user=username)