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