/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2007-2010 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
17
3251.4.3 by Aaron Bentley
More renames and cleanups
18
"""Directory lookup that uses Launchpad."""
2245.8.3 by Martin Pool
Start adding indirection transport
19
5121.2.1 by Jelmer Vernooij
Remove unused imports in lp_directory.
20
from urlparse import urlsplit
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
21
import xmlrpclib
22
2245.8.4 by Martin Pool
lp:/// indirection works
23
from bzrlib import (
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
24
    debug,
2245.8.4 by Martin Pool
lp:/// indirection works
25
    errors,
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
26
    trace,
2245.8.4 by Martin Pool
lp:/// indirection works
27
    )
28
from bzrlib.transport import (
29
    get_transport,
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
30
    register_urlparse_netloc_protocol,
2245.8.4 by Martin Pool
lp:/// indirection works
31
    )
32
2898.4.7 by James Henstridge
Fix up tests.
33
from bzrlib.plugins.launchpad.lp_registration import (
34
    LaunchpadService, ResolveLaunchpadPathRequest)
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
35
from bzrlib.plugins.launchpad.account import get_lp_login
36
37
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
38
# As bzrlib.transport.remote may not be loaded yet, make sure bzr+ssh
39
# is counted as a netloc protocol.
40
register_urlparse_netloc_protocol('bzr+ssh')
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
41
register_urlparse_netloc_protocol('lp')
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
42
5462.4.5 by Barry Warsaw
We only need to include distroseries shortcuts.
43
_ubuntu_series_shortcuts = {
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
44
    'n': 'natty',
45
    'm': 'maverick',
46
    'l': 'lucid',
47
    'k': 'karmic',
48
    'j': 'jaunty',
49
    'h': 'hardy',
50
    'd': 'dapper',
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
51
    }
52
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
53
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
54
class LaunchpadDirectory(object):
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
55
3031.2.4 by jml at canonical
Only split the URL once.
56
    def _requires_launchpad_login(self, scheme, netloc, path, query,
57
                                  fragment):
58
        """Does the URL require a Launchpad login in order to be reached?
59
60
        The URL is specified by its parsed components, as returned from
61
        urlsplit.
62
        """
3031.2.3 by jml at canonical
Make the test pass -- don't include sftp URLs if there's no lp login.
63
        return (scheme in ('bzr+ssh', 'sftp')
64
                and (netloc.endswith('launchpad.net')
65
                     or netloc.endswith('launchpad.dev')))
3031.2.1 by jml at canonical
Factor out the method that determines if a URL is a LP url.
66
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
67
    def look_up(self, name, url):
3251.4.5 by Aaron Bentley
Add docstring
68
        """See DirectoryService.look_up"""
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
69
        return self._resolve(url)
70
3251.4.3 by Aaron Bentley
More renames and cleanups
71
    def _resolve(self, url,
2898.4.11 by James Henstridge
Switch back to RedirectRequested based implementation.
72
                 _request_factory=ResolveLaunchpadPathRequest,
73
                 _lp_login=None):
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
74
        """Resolve the base URL for this transport."""
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
75
        # Do ubuntu: and debianlp: expansions.
5462.4.10 by Vincent Ladeuil
Fix python2.4 compatibility.
76
        scheme, netloc, path, query, fragment = urlsplit(url)
77
        if scheme in ('ubuntu', 'debianlp'):
78
            if scheme == 'ubuntu':
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
79
                distro = 'ubuntu'
5462.4.5 by Barry Warsaw
We only need to include distroseries shortcuts.
80
                distro_series = _ubuntu_series_shortcuts
5462.4.10 by Vincent Ladeuil
Fix python2.4 compatibility.
81
            elif scheme == 'debianlp':
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
82
                distro = 'debian'
5462.4.5 by Barry Warsaw
We only need to include distroseries shortcuts.
83
                # No shortcuts for Debian distroseries.
84
                distro_series = {}
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
85
            else:
86
                raise AssertionError('scheme should be ubuntu: or debianlp:')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
87
            # Split the path.  It's either going to be 'project' or
88
            # 'series/project', but recognize that it may be a series we don't
89
            # know about.
5462.4.10 by Vincent Ladeuil
Fix python2.4 compatibility.
90
            path_parts = path.split('/')
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
91
            if len(path_parts) == 1:
92
                # It's just a project name.
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
93
                lp_url_template = 'lp:%(distro)s/%(project)s'
94
                project = path_parts[0]
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
95
                series = None
96
            elif len(path_parts) == 2:
97
                # It's a series and project.
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
98
                lp_url_template = 'lp:%(distro)s/%(series)s/%(project)s'
5462.4.3 by Barry Warsaw
* Add back Ubuntu distroseries shortcuts.
99
                series, project = path_parts
100
            else:
101
                # There are either 0 or > 2 path parts, neither of which is
102
                # supported for these schemes.
103
                raise errors.InvalidURL('Bad path: %s' % result.path)
104
            # Expand any series shortcuts, but keep unknown series.
105
            series = distro_series.get(series, series)
5462.4.1 by Barry Warsaw
Added support for ubuntu: and debianlp: schemes, accessing the relevant
106
            # Hack the url and let the following do the final resolution.
107
            url = lp_url_template % dict(
108
                distro=distro,
109
                series=series,
110
                project=project)
5462.4.10 by Vincent Ladeuil
Fix python2.4 compatibility.
111
            scheme, netloc, path, query, fragment = urlsplit(url)
4505.6.2 by Jonathan Lange
Extract a method that gets a service from a URL.
112
        service = LaunchpadService.for_url(url)
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
113
        if _lp_login is None:
114
            _lp_login = get_lp_login()
5462.4.10 by Vincent Ladeuil
Fix python2.4 compatibility.
115
        path = path.strip('/')
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
116
        if path.startswith('~/'):
117
            if _lp_login is None:
118
                raise errors.InvalidURL(path=url,
119
                    extra='Cannot resolve "~" to your username.'
120
                          ' See "bzr help launchpad-login"')
121
            path = '~' + _lp_login + path[1:]
122
        resolve = _request_factory(path)
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
123
        try:
124
            result = resolve.submit(service)
125
        except xmlrpclib.Fault, fault:
126
            raise errors.InvalidURL(
3251.4.3 by Aaron Bentley
More renames and cleanups
127
                path=url, extra=fault.faultString)
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
128
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
129
        if 'launchpad' in debug.debug_flags:
4505.6.2 by Jonathan Lange
Extract a method that gets a service from a URL.
130
            trace.mutter("resolve_lp_path(%r) == %r", url, result)
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
131
3270.4.1 by James Westby
Warn the user when resolving lp: URLs if they haven't set their login.
132
        _warned_login = False
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
133
        for url in result['urls']:
134
            scheme, netloc, path, query, fragment = urlsplit(url)
3031.2.4 by jml at canonical
Only split the URL once.
135
            if self._requires_launchpad_login(scheme, netloc, path, query,
136
                                              fragment):
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
137
                # Only accept launchpad.net bzr+ssh URLs if we know
138
                # the user's Launchpad login:
3777.1.21 by Aaron Bentley
Stop including usernames in resolved lp: urls
139
                if _lp_login is not None:
140
                    break
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
141
                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.
142
                    if not _warned_login:
3834.1.2 by Martin Pool
Better message when launchpad-login is needed
143
                        trace.warning(
3834.1.3 by Martin Pool
Shorter message about launchpad-login
144
'You have not informed bzr of your Launchpad ID, and you must do this to\n'
145
'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.
146
                        _warned_login = True
2898.4.15 by James Henstridge
Use get_transport() to decide whether Bazaar supports a given URL.
147
            else:
148
                # Use the URL if we can create a transport for it.
149
                try:
150
                    get_transport(url)
151
                except (errors.PathError, errors.TransportError):
152
                    pass
153
                else:
154
                    break
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
155
        else:
3251.4.3 by Aaron Bentley
More renames and cleanups
156
            raise errors.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
157
        return url
158
2245.8.3 by Martin Pool
Start adding indirection transport
159
160
def get_test_permutations():
161
    # Since this transport doesn't do anything once opened, it's not subjected
162
    # to the usual transport tests.
163
    return []