/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

First attempt to merge .dev and resolve the conflicts (but tests are 
failing)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, 2008 Canonical Ltd
 
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
"""Directory lookup that uses Launchpad."""
 
19
 
 
20
from urlparse import urlsplit, urlunsplit
 
21
import xmlrpclib
 
22
 
 
23
from bzrlib import (
 
24
    debug,
 
25
    errors,
 
26
    trace,
 
27
    urlutils,
 
28
    )
 
29
from bzrlib.transport import (
 
30
    get_transport,
 
31
    register_urlparse_netloc_protocol,
 
32
    )
 
33
 
 
34
from bzrlib.plugins.launchpad.lp_registration import (
 
35
    LaunchpadService, ResolveLaunchpadPathRequest)
 
36
from bzrlib.plugins.launchpad.account import get_lp_login
 
37
 
 
38
 
 
39
# As bzrlib.transport.remote may not be loaded yet, make sure bzr+ssh
 
40
# is counted as a netloc protocol.
 
41
register_urlparse_netloc_protocol('bzr+ssh')
 
42
register_urlparse_netloc_protocol('lp')
 
43
 
 
44
 
 
45
class LaunchpadDirectory(object):
 
46
 
 
47
    def _requires_launchpad_login(self, scheme, netloc, path, query,
 
48
                                  fragment):
 
49
        """Does the URL require a Launchpad login in order to be reached?
 
50
 
 
51
        The URL is specified by its parsed components, as returned from
 
52
        urlsplit.
 
53
        """
 
54
        return (scheme in ('bzr+ssh', 'sftp')
 
55
                and (netloc.endswith('launchpad.net')
 
56
                     or netloc.endswith('launchpad.dev')))
 
57
 
 
58
    def look_up(self, name, url):
 
59
        """See DirectoryService.look_up"""
 
60
        return self._resolve(url)
 
61
 
 
62
    def _resolve(self, url,
 
63
                 _request_factory=ResolveLaunchpadPathRequest,
 
64
                 _lp_login=None):
 
65
        """Resolve the base URL for this transport."""
 
66
        result = urlsplit(url)
 
67
        # Perform an XMLRPC request to resolve the path
 
68
        lp_instance = result[1]
 
69
        if lp_instance == '':
 
70
            lp_instance = None
 
71
        elif lp_instance not in LaunchpadService.LAUNCHPAD_INSTANCE:
 
72
            raise errors.InvalidURL(path=url)
 
73
        resolve = _request_factory(result[2].strip('/'))
 
74
        service = LaunchpadService(lp_instance=lp_instance)
 
75
        try:
 
76
            result = resolve.submit(service)
 
77
        except xmlrpclib.Fault, fault:
 
78
            raise errors.InvalidURL(
 
79
                path=url, extra=fault.faultString)
 
80
 
 
81
        if 'launchpad' in debug.debug_flags:
 
82
            trace.mutter("resolve_lp_path(%r) == %r", path, result)
 
83
 
 
84
        if _lp_login is None:
 
85
            _lp_login = get_lp_login()
 
86
        _warned_login = False
 
87
        for url in result['urls']:
 
88
            scheme, netloc, path, query, fragment = urlsplit(url)
 
89
            if self._requires_launchpad_login(scheme, netloc, path, query,
 
90
                                              fragment):
 
91
                # Only accept launchpad.net bzr+ssh URLs if we know
 
92
                # the user's Launchpad login:
 
93
                if _lp_login is None:
 
94
                    if not _warned_login:
 
95
                        trace.warning('You have not informed bzr of your '
 
96
                                'launchpad login. If you are attempting a\n'
 
97
                                'write operation and it fails, run '
 
98
                                '"bzr launchpad-login YOUR_ID" and try again.')
 
99
                        _warned_login = True
 
100
                    continue
 
101
                url = urlunsplit((scheme, '%s@%s' % (_lp_login, netloc),
 
102
                                  path, query, fragment))
 
103
                break
 
104
            else:
 
105
                # Use the URL if we can create a transport for it.
 
106
                try:
 
107
                    get_transport(url)
 
108
                except (errors.PathError, errors.TransportError):
 
109
                    pass
 
110
                else:
 
111
                    break
 
112
        else:
 
113
            raise errors.InvalidURL(path=url, extra='no supported schemes')
 
114
        return url
 
115
 
 
116
 
 
117
def get_test_permutations():
 
118
    # Since this transport doesn't do anything once opened, it's not subjected
 
119
    # to the usual transport tests.
 
120
    return []