/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

Merge bzr.dev, update to use new hooks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 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
24
24
    debug,
25
25
    errors,
26
26
    trace,
27
 
    )
28
 
from bzrlib.transport import (
29
 
    get_transport,
30
 
    register_urlparse_netloc_protocol,
31
 
    )
 
27
    transport,
 
28
    )
 
29
from bzrlib.i18n import gettext
32
30
 
33
31
from bzrlib.plugins.launchpad.lp_registration import (
34
32
    LaunchpadService, ResolveLaunchpadPathRequest)
37
35
 
38
36
# As bzrlib.transport.remote may not be loaded yet, make sure bzr+ssh
39
37
# is counted as a netloc protocol.
40
 
register_urlparse_netloc_protocol('bzr+ssh')
41
 
register_urlparse_netloc_protocol('lp')
 
38
transport.register_urlparse_netloc_protocol('bzr+ssh')
 
39
transport.register_urlparse_netloc_protocol('lp')
 
40
 
 
41
_ubuntu_series_shortcuts = {
 
42
    'n': 'natty',
 
43
    'm': 'maverick',
 
44
    'l': 'lucid',
 
45
    'k': 'karmic',
 
46
    'j': 'jaunty',
 
47
    'h': 'hardy',
 
48
    'd': 'dapper',
 
49
    }
42
50
 
43
51
 
44
52
class LaunchpadDirectory(object):
58
66
        """See DirectoryService.look_up"""
59
67
        return self._resolve(url)
60
68
 
61
 
    def _resolve(self, url,
62
 
                 _request_factory=ResolveLaunchpadPathRequest,
63
 
                 _lp_login=None):
64
 
        """Resolve the base URL for this transport."""
 
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
 
86
                or (parts[1] in ('ubuntu', 'debian') and len(parts) < 5)):
 
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]}
 
94
 
 
95
    def _resolve_via_xmlrpc(self, path, url, _request_factory):
65
96
        service = LaunchpadService.for_url(url)
66
 
        result = urlsplit(url)
67
 
        if _lp_login is None:
68
 
            _lp_login = get_lp_login()
69
 
        path = result[2].strip('/')
70
 
        if path.startswith('~/'):
71
 
            if _lp_login is None:
72
 
                raise errors.InvalidURL(path=url,
73
 
                    extra='Cannot resolve "~" to your username.'
74
 
                          ' See "bzr help launchpad-login"')
75
 
            path = '~' + _lp_login + path[1:]
76
97
        resolve = _request_factory(path)
77
98
        try:
78
99
            result = resolve.submit(service)
79
100
        except xmlrpclib.Fault, fault:
80
101
            raise errors.InvalidURL(
81
102
                path=url, extra=fault.faultString)
 
103
        return result
 
104
 
 
105
    def _update_url_scheme(self, url):
 
106
        # Do ubuntu: and debianlp: expansions.
 
107
        scheme, netloc, path, query, fragment = urlsplit(url)
 
108
        if scheme in ('ubuntu', 'debianlp'):
 
109
            if scheme == 'ubuntu':
 
110
                distro = 'ubuntu'
 
111
                distro_series = _ubuntu_series_shortcuts
 
112
            elif scheme == 'debianlp':
 
113
                distro = 'debian'
 
114
                # No shortcuts for Debian distroseries.
 
115
                distro_series = {}
 
116
            else:
 
117
                raise AssertionError('scheme should be ubuntu: or debianlp:')
 
118
            # Split the path.  It's either going to be 'project' or
 
119
            # 'series/project', but recognize that it may be a series we don't
 
120
            # know about.
 
121
            path_parts = path.split('/')
 
122
            if len(path_parts) == 1:
 
123
                # It's just a project name.
 
124
                lp_url_template = 'lp:%(distro)s/%(project)s'
 
125
                project = path_parts[0]
 
126
                series = None
 
127
            elif len(path_parts) == 2:
 
128
                # It's a series and project.
 
129
                lp_url_template = 'lp:%(distro)s/%(series)s/%(project)s'
 
130
                series, project = path_parts
 
131
            else:
 
132
                # There are either 0 or > 2 path parts, neither of which is
 
133
                # supported for these schemes.
 
134
                raise errors.InvalidURL('Bad path: %s' % url)
 
135
            # Expand any series shortcuts, but keep unknown series.
 
136
            series = distro_series.get(series, series)
 
137
            # Hack the url and let the following do the final resolution.
 
138
            url = lp_url_template % dict(
 
139
                distro=distro,
 
140
                series=series,
 
141
                project=project)
 
142
            scheme, netloc, path, query, fragment = urlsplit(url)
 
143
        return url, path
 
144
 
 
145
    def _expand_user(self, path, url, lp_login):
 
146
        if path.startswith('~/'):
 
147
            if lp_login is None:
 
148
                raise errors.InvalidURL(path=url,
 
149
                    extra='Cannot resolve "~" to your username.'
 
150
                          ' See "bzr help launchpad-login"')
 
151
            path = '~' + lp_login + path[1:]
 
152
        return path
 
153
 
 
154
    def _resolve(self, url,
 
155
                 _request_factory=ResolveLaunchpadPathRequest,
 
156
                 _lp_login=None):
 
157
        """Resolve the base URL for this transport."""
 
158
        url, path = self._update_url_scheme(url)
 
159
        if _lp_login is None:
 
160
            _lp_login = get_lp_login()
 
161
        path = path.strip('/')
 
162
        path = self._expand_user(path, url, _lp_login)
 
163
        if _lp_login is not None:
 
164
            result = self._resolve_locally(path, url, _request_factory)
 
165
            if 'launchpad' in debug.debug_flags:
 
166
                local_res = result
 
167
                result = self._resolve_via_xmlrpc(path, url, _request_factory)
 
168
                trace.note(gettext(
 
169
                    'resolution for {0}\n  local: {1}\n remote: {2}').format(
 
170
                           url, local_res['urls'], result['urls']))
 
171
        else:
 
172
            result = self._resolve_via_xmlrpc(path, url, _request_factory)
82
173
 
83
174
        if 'launchpad' in debug.debug_flags:
84
175
            trace.mutter("resolve_lp_path(%r) == %r", url, result)
101
192
            else:
102
193
                # Use the URL if we can create a transport for it.
103
194
                try:
104
 
                    get_transport(url)
 
195
                    transport.get_transport(url)
105
196
                except (errors.PathError, errors.TransportError):
106
197
                    pass
107
198
                else: