~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/launchpad/lp_indirect.py

  • Committer: Robert Collins
  • Date: 2007-07-04 01:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 2581.
  • Revision ID: robertc@robertcollins.net-20070704013950-7pp23plwyqjvgkxg
Review feedback.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
 
18
 
"""Directory lookup that uses Launchpad."""
19
 
 
20
 
from urlparse import urlsplit
21
 
import xmlrpclib
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
"""Transport indirection that uses Launchpad as a directory lookup.
 
19
 
 
20
When the transport is opened, it immediately redirects to a url
 
21
on Launchpad, which can then either serve the branch itself or redirect
 
22
again.
 
23
"""
22
24
 
23
25
from bzrlib import (
24
 
    debug,
25
26
    errors,
26
 
    trace,
27
 
    transport,
28
 
    )
29
 
 
30
 
from bzrlib.plugins.launchpad.lp_registration import (
31
 
    LaunchpadService, ResolveLaunchpadPathRequest)
32
 
from bzrlib.plugins.launchpad.account import get_lp_login
33
 
 
34
 
 
35
 
# As bzrlib.transport.remote may not be loaded yet, make sure bzr+ssh
36
 
# is counted as a netloc protocol.
37
 
transport.register_urlparse_netloc_protocol('bzr+ssh')
38
 
transport.register_urlparse_netloc_protocol('lp')
39
 
 
40
 
_ubuntu_series_shortcuts = {
41
 
    'n': 'natty',
42
 
    'm': 'maverick',
43
 
    'l': 'lucid',
44
 
    'k': 'karmic',
45
 
    'j': 'jaunty',
46
 
    'h': 'hardy',
47
 
    'd': 'dapper',
48
 
    }
49
 
 
50
 
 
51
 
class LaunchpadDirectory(object):
52
 
 
53
 
    def _requires_launchpad_login(self, scheme, netloc, path, query,
54
 
                                  fragment):
55
 
        """Does the URL require a Launchpad login in order to be reached?
56
 
 
57
 
        The URL is specified by its parsed components, as returned from
58
 
        urlsplit.
59
 
        """
60
 
        return (scheme in ('bzr+ssh', 'sftp')
61
 
                and (netloc.endswith('launchpad.net')
62
 
                     or netloc.endswith('launchpad.dev')))
63
 
 
64
 
    def look_up(self, name, url):
65
 
        """See DirectoryService.look_up"""
66
 
        return self._resolve(url)
67
 
 
68
 
    def _resolve_locally(self, path, url, _request_factory):
69
 
        # This is the best I could work out about XMLRPC. If an lp: url
70
 
        # includes ~user, then it is specially validated. Otherwise, it is just
71
 
        # sent to +branch/$path.
72
 
        _, netloc, _, _, _ = urlsplit(url)
73
 
        if netloc == '':
74
 
            netloc = LaunchpadService.DEFAULT_INSTANCE
75
 
        base_url = LaunchpadService.LAUNCHPAD_DOMAINS[netloc]
76
 
        base = 'bzr+ssh://bazaar.%s/' % (base_url,)
77
 
        maybe_invalid = False
78
 
        if path.startswith('~'):
79
 
            # A ~user style path, validate it a bit.
80
 
            # If a path looks fishy, fall back to asking XMLRPC to
81
 
            # resolve it for us. That way we still get their nicer error
82
 
            # messages.
83
 
            parts = path.split('/')
84
 
            if (len(parts) < 3
85
 
                or (parts[1] in ('ubuntu', 'debian') and len(parts) < 5)):
86
 
                # This special case requires 5-parts to be valid.
87
 
                maybe_invalid = True
88
 
        else:
89
 
            base += '+branch/'
90
 
        if maybe_invalid:
91
 
            return self._resolve_via_xmlrpc(path, url, _request_factory)
92
 
        return {'urls': [base + path]}
93
 
 
94
 
    def _resolve_via_xmlrpc(self, path, url, _request_factory):
95
 
        service = LaunchpadService.for_url(url)
96
 
        resolve = _request_factory(path)
97
 
        try:
98
 
            result = resolve.submit(service)
99
 
        except xmlrpclib.Fault, fault:
100
 
            raise errors.InvalidURL(
101
 
                path=url, extra=fault.faultString)
102
 
        return result
103
 
 
104
 
    def _update_url_scheme(self, url):
105
 
        # Do ubuntu: and debianlp: expansions.
106
 
        scheme, netloc, path, query, fragment = urlsplit(url)
107
 
        if scheme in ('ubuntu', 'debianlp'):
108
 
            if scheme == 'ubuntu':
109
 
                distro = 'ubuntu'
110
 
                distro_series = _ubuntu_series_shortcuts
111
 
            elif scheme == 'debianlp':
112
 
                distro = 'debian'
113
 
                # No shortcuts for Debian distroseries.
114
 
                distro_series = {}
115
 
            else:
116
 
                raise AssertionError('scheme should be ubuntu: or debianlp:')
117
 
            # Split the path.  It's either going to be 'project' or
118
 
            # 'series/project', but recognize that it may be a series we don't
119
 
            # know about.
120
 
            path_parts = path.split('/')
121
 
            if len(path_parts) == 1:
122
 
                # It's just a project name.
123
 
                lp_url_template = 'lp:%(distro)s/%(project)s'
124
 
                project = path_parts[0]
125
 
                series = None
126
 
            elif len(path_parts) == 2:
127
 
                # It's a series and project.
128
 
                lp_url_template = 'lp:%(distro)s/%(series)s/%(project)s'
129
 
                series, project = path_parts
130
 
            else:
131
 
                # There are either 0 or > 2 path parts, neither of which is
132
 
                # supported for these schemes.
133
 
                raise errors.InvalidURL('Bad path: %s' % result.path)
134
 
            # Expand any series shortcuts, but keep unknown series.
135
 
            series = distro_series.get(series, series)
136
 
            # Hack the url and let the following do the final resolution.
137
 
            url = lp_url_template % dict(
138
 
                distro=distro,
139
 
                series=series,
140
 
                project=project)
141
 
            scheme, netloc, path, query, fragment = urlsplit(url)
142
 
        return url, path
143
 
 
144
 
    def _expand_user(self, path, url, lp_login):
145
 
        if path.startswith('~/'):
146
 
            if lp_login is None:
147
 
                raise errors.InvalidURL(path=url,
148
 
                    extra='Cannot resolve "~" to your username.'
149
 
                          ' See "bzr help launchpad-login"')
150
 
            path = '~' + lp_login + path[1:]
151
 
        return path
152
 
 
153
 
    def _resolve(self, url,
154
 
                 _request_factory=ResolveLaunchpadPathRequest,
155
 
                 _lp_login=None):
156
 
        """Resolve the base URL for this transport."""
157
 
        url, path = self._update_url_scheme(url)
158
 
        if _lp_login is None:
159
 
            _lp_login = get_lp_login()
160
 
        path = path.strip('/')
161
 
        path = self._expand_user(path, url, _lp_login)
162
 
        if _lp_login is not None:
163
 
            result = self._resolve_locally(path, url, _request_factory)
164
 
            if 'launchpad' in debug.debug_flags:
165
 
                local_res = result
166
 
                result = self._resolve_via_xmlrpc(path, url, _request_factory)
167
 
                trace.note('resolution for %s\n  local: %s\n remote: %s'
168
 
                           % (url, local_res['urls'], result['urls']))
169
 
        else:
170
 
            result = self._resolve_via_xmlrpc(path, url, _request_factory)
171
 
 
172
 
        if 'launchpad' in debug.debug_flags:
173
 
            trace.mutter("resolve_lp_path(%r) == %r", url, result)
174
 
 
175
 
        _warned_login = False
176
 
        for url in result['urls']:
177
 
            scheme, netloc, path, query, fragment = urlsplit(url)
178
 
            if self._requires_launchpad_login(scheme, netloc, path, query,
179
 
                                              fragment):
180
 
                # Only accept launchpad.net bzr+ssh URLs if we know
181
 
                # the user's Launchpad login:
182
 
                if _lp_login is not None:
183
 
                    break
184
 
                if _lp_login is None:
185
 
                    if not _warned_login:
186
 
                        trace.warning(
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".')
189
 
                        _warned_login = True
190
 
            else:
191
 
                # Use the URL if we can create a transport for it.
192
 
                try:
193
 
                    transport.get_transport(url)
194
 
                except (errors.PathError, errors.TransportError):
195
 
                    pass
196
 
                else:
197
 
                    break
198
 
        else:
199
 
            raise errors.InvalidURL(path=url, extra='no supported schemes')
200
 
        return url
 
27
    )
 
28
from bzrlib.transport import (
 
29
    get_transport,
 
30
    Transport,
 
31
    )
 
32
 
 
33
 
 
34
def launchpad_transport_indirect(base_url):
 
35
    """Uses Launchpad.net as a directory of open source software"""
 
36
    if base_url.startswith('lp:///'):
 
37
        real_url = 'http://code.launchpad.net/' + base_url[6:]
 
38
    elif base_url.startswith('lp:') and base_url[3] != '/':
 
39
        real_url = 'http://code.launchpad.net/' + base_url[3:]
 
40
    else:
 
41
        raise errors.InvalidURL(path=base_url)
 
42
    return get_transport(real_url)
201
43
 
202
44
 
203
45
def get_test_permutations():