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