/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5609.24.7 by John Arbash Meinel
Fix bug #397739. Avoid doing an XMLRPC lookup if a user
1
# Copyright (C) 2007-2011 Canonical Ltd
2245.8.3 by Martin Pool
Start adding indirection transport
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2245.8.3 by Martin Pool
Start adding indirection transport
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""Directory lookup that uses Launchpad."""
18
7479.2.1 by Jelmer Vernooij
Drop python2 support.
19
from urllib.parse import urlsplit
20
from xmlrpc.client import Fault
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
21
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
22
from ... import (
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
23
    debug,
2245.8.4 by Martin Pool
lp:/// indirection works
24
    errors,
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
25
    trace,
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
26
    transport,
2245.8.4 by Martin Pool
lp:/// indirection works
27
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
28
from ...i18n import gettext
2245.8.4 by Martin Pool
lp:/// indirection works
29
7290.35.1 by Jelmer Vernooij
Allow running tests without launchpadlib installed.
30
from .uris import (
7240.5.7 by Jelmer Vernooij
Move DEFAULT_INSTANCE.
31
    DEFAULT_INSTANCE,
32
    LAUNCHPAD_DOMAINS,
33
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
34
from .lp_registration import (
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
35
    InvalidURL,
36
    LaunchpadService,
37
    ResolveLaunchpadPathRequest,
38
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
39
from .account import get_lp_login
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
40
41
42
# As breezy.transport.remote may not be loaded yet, make sure bzr+ssh
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
43
# is counted as a netloc protocol.
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
44
transport.register_urlparse_netloc_protocol('bzr+ssh')
45
transport.register_urlparse_netloc_protocol('lp')
7240.11.1 by Jelmer Vernooij
Add support for lp+bzr.
46
transport.register_urlparse_netloc_protocol('lp+bzr')
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
47
48
7240.5.8 by Jelmer Vernooij
Factor out methods.
49
def _requires_launchpad_login(scheme, netloc, path, query,
50
                              fragment):
51
    """Does the URL require a Launchpad login in order to be reached?
52
53
    The URL is specified by its parsed components, as returned from
54
    urlsplit.
55
    """
56
    return (scheme in ('bzr+ssh', 'sftp')
57
            and (netloc.endswith('launchpad.net') or
7302.1.1 by Colin Watson
Handle renaming of launchpad.dev to launchpad.test.
58
                 netloc.endswith('launchpad.test')))
7240.5.8 by Jelmer Vernooij
Factor out methods.
59
60
61
def _expand_user(path, url, lp_login):
62
    if path.startswith('~/'):
63
        if lp_login is None:
64
            raise InvalidURL(path=url,
65
                             extra='Cannot resolve "~" to your username.'
66
                             ' See "bzr help launchpad-login"')
67
        path = '~' + lp_login + path[1:]
68
    return path
69
70
71
def _update_url_scheme(url):
72
    # Do ubuntu: and debianlp: expansions.
73
    scheme, netloc, path, query, fragment = urlsplit(url)
74
    if scheme in ('ubuntu', 'debianlp'):
75
        if scheme == 'ubuntu':
76
            distro = 'ubuntu'
77
        elif scheme == 'debianlp':
78
            distro = 'debian'
79
            # No shortcuts for Debian distroseries.
80
        else:
81
            raise AssertionError('scheme should be ubuntu: or debianlp:')
82
        # Split the path.  It's either going to be 'project' or
83
        # 'series/project', but recognize that it may be a series we don't
84
        # know about.
85
        path_parts = path.split('/')
86
        if len(path_parts) == 1:
87
            # It's just a project name.
88
            lp_url_template = 'lp:%(distro)s/%(project)s'
89
            project = path_parts[0]
90
            series = None
91
        elif len(path_parts) == 2:
92
            # It's a series and project.
93
            lp_url_template = 'lp:%(distro)s/%(series)s/%(project)s'
94
            series, project = path_parts
95
        else:
96
            # There are either 0 or > 2 path parts, neither of which is
97
            # supported for these schemes.
98
            raise InvalidURL('Bad path: %s' % url)
99
        # Hack the url and let the following do the final resolution.
100
        url = lp_url_template % dict(
101
            distro=distro,
102
            series=series,
103
            project=project)
104
        scheme, netloc, path, query, fragment = urlsplit(url)
7240.11.2 by Jelmer Vernooij
Merge trunk.
105
    elif scheme == 'lp+bzr':
106
        scheme = 'lp'
7240.5.8 by Jelmer Vernooij
Factor out methods.
107
    return url, path
108
109
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
110
class LaunchpadDirectory(object):
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
111
7268.11.2 by Jelmer Vernooij
Add purpose argument.
112
    def look_up(self, name, url, purpose=None):
3251.4.5 by Aaron Bentley
Add docstring
113
        """See DirectoryService.look_up"""
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
114
        return self._resolve(url)
115
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
116
    def _resolve_locally(self, path, url, _request_factory):
117
        # This is the best I could work out about XMLRPC. If an lp: url
118
        # includes ~user, then it is specially validated. Otherwise, it is just
119
        # sent to +branch/$path.
120
        _, netloc, _, _, _ = urlsplit(url)
121
        if netloc == '':
7240.5.7 by Jelmer Vernooij
Move DEFAULT_INSTANCE.
122
            netloc = DEFAULT_INSTANCE
7240.5.4 by Jelmer Vernooij
Fix last one.
123
        base_url = LAUNCHPAD_DOMAINS[netloc]
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
124
        base = 'bzr+ssh://bazaar.%s/' % (base_url,)
125
        maybe_invalid = False
126
        if path.startswith('~'):
127
            # A ~user style path, validate it a bit.
128
            # If a path looks fishy, fall back to asking XMLRPC to
129
            # resolve it for us. That way we still get their nicer error
130
            # messages.
131
            parts = path.split('/')
132
            if (len(parts) < 3
7143.15.2 by Jelmer Vernooij
Run autopep8.
133
                    or (parts[1] in ('ubuntu', 'debian') and len(parts) < 5)):
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
134
                # This special case requires 5-parts to be valid.
135
                maybe_invalid = True
136
        else:
137
            base += '+branch/'
138
        if maybe_invalid:
139
            return self._resolve_via_xmlrpc(path, url, _request_factory)
140
        return {'urls': [base + path]}
5609.24.7 by John Arbash Meinel
Fix bug #397739. Avoid doing an XMLRPC lookup if a user
141
142
    def _resolve_via_xmlrpc(self, path, url, _request_factory):
143
        service = LaunchpadService.for_url(url)
144
        resolve = _request_factory(path)
145
        try:
146
            result = resolve.submit(service)
7027.7.1 by Jelmer Vernooij
Fix launchpad plugin tests on python 3.
147
        except Fault as fault:
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
148
            raise InvalidURL(
5609.24.7 by John Arbash Meinel
Fix bug #397739. Avoid doing an XMLRPC lookup if a user
149
                path=url, extra=fault.faultString)
150
        return result
151
152
    def _resolve(self, url,
153
                 _request_factory=ResolveLaunchpadPathRequest,
154
                 _lp_login=None):
155
        """Resolve the base URL for this transport."""
7240.5.8 by Jelmer Vernooij
Factor out methods.
156
        url, path = _update_url_scheme(url)
5609.24.7 by John Arbash Meinel
Fix bug #397739. Avoid doing an XMLRPC lookup if a user
157
        if _lp_login is None:
158
            _lp_login = get_lp_login()
159
        path = path.strip('/')
7240.5.8 by Jelmer Vernooij
Factor out methods.
160
        path = _expand_user(path, url, _lp_login)
5609.24.7 by John Arbash Meinel
Fix bug #397739. Avoid doing an XMLRPC lookup if a user
161
        if _lp_login is not None:
5609.24.9 by John Arbash Meinel
A bunch more tests and bug fixes for the local resolution.
162
            result = self._resolve_locally(path, url, _request_factory)
163
            if 'launchpad' in debug.debug_flags:
5609.24.11 by John Arbash Meinel
If someone uses -Dlaunchpad, use the 'official' URL instead of the local one.
164
                local_res = result
165
                result = self._resolve_via_xmlrpc(path, url, _request_factory)
6150.3.4 by Jonathan Riddell
more launchpad plugin gettext()ing
166
                trace.note(gettext(
167
                    'resolution for {0}\n  local: {1}\n remote: {2}').format(
7143.15.2 by Jelmer Vernooij
Run autopep8.
168
                    url, local_res['urls'], result['urls']))
5609.24.7 by John Arbash Meinel
Fix bug #397739. Avoid doing an XMLRPC lookup if a user
169
        else:
170
            result = self._resolve_via_xmlrpc(path, url, _request_factory)
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
171
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
172
        if 'launchpad' in debug.debug_flags:
4505.6.2 by Jonathan Lange
Extract a method that gets a service from a URL.
173
            trace.mutter("resolve_lp_path(%r) == %r", url, result)
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
174
3270.4.1 by James Westby
Warn the user when resolving lp: URLs if they haven't set their login.
175
        _warned_login = False
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
176
        for url in result['urls']:
177
            scheme, netloc, path, query, fragment = urlsplit(url)
7240.5.8 by Jelmer Vernooij
Factor out methods.
178
            if _requires_launchpad_login(scheme, netloc, path, query,
179
                                         fragment):
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
180
                # Only accept launchpad.net bzr+ssh URLs if we know
181
                # the user's Launchpad login:
3777.1.21 by Aaron Bentley
Stop including usernames in resolved lp: urls
182
                if _lp_login is not None:
183
                    break
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
184
                if _lp_login is None:
3270.4.1 by James Westby
Warn the user when resolving lp: URLs if they haven't set their login.
185
                    if not _warned_login:
3834.1.2 by Martin Pool
Better message when launchpad-login is needed
186
                        trace.warning(
7143.15.2 by Jelmer Vernooij
Run autopep8.
187
                            'You have not informed bzr of your Launchpad ID, and you must do this to\n'
188
                            'write to Launchpad or access private data.  See "bzr help launchpad-login".')
3270.4.1 by James Westby
Warn the user when resolving lp: URLs if they haven't set their login.
189
                        _warned_login = True
2898.4.15 by James Henstridge
Use get_transport() to decide whether Bazaar supports a given URL.
190
            else:
191
                # Use the URL if we can create a transport for it.
192
                try:
5609.9.1 by Martin
Blindly change all users of get_transport to address the function via the transport module
193
                    transport.get_transport(url)
2898.4.15 by James Henstridge
Use get_transport() to decide whether Bazaar supports a given URL.
194
                except (errors.PathError, errors.TransportError):
195
                    pass
196
                else:
197
                    break
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
198
        else:
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
199
            raise InvalidURL(path=url, extra='no supported schemes')
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
200
        return url
201
2245.8.3 by Martin Pool
Start adding indirection transport
202
203
def get_test_permutations():
204
    # Since this transport doesn't do anything once opened, it's not subjected
205
    # to the usual transport tests.
206
    return []