~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

 * Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
   that it is now attempted first when lookup up repositories, leading to
   an extra round trip on older bzr smart servers but supporting the
   feature on newer servers. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008 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
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
"""Directory lookup that uses Launchpad."""
 
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
"""
19
24
 
20
25
from urlparse import urlsplit, urlunsplit
21
26
import xmlrpclib
29
34
from bzrlib.transport import (
30
35
    get_transport,
31
36
    register_urlparse_netloc_protocol,
 
37
    Transport,
32
38
    )
33
39
 
34
40
from bzrlib.plugins.launchpad.lp_registration import (
42
48
register_urlparse_netloc_protocol('lp')
43
49
 
44
50
 
45
 
class LaunchpadDirectory(object):
 
51
class LaunchpadTransport(Transport):
 
52
    """lp:/// URL transport
 
53
 
 
54
    This transport redirects requests to the real branch location
 
55
    after resolving the URL via an XMLRPC request to Launchpad.
 
56
    """
 
57
 
 
58
    def __init__(self, base):
 
59
        super(LaunchpadTransport, self).__init__(base)
 
60
        # We only support URLs without a netloc
 
61
        self.lp_instance = urlsplit(base)[1]
 
62
        if self.lp_instance == '':
 
63
            self.lp_instance = None
 
64
        elif self.lp_instance not in LaunchpadService.LAUNCHPAD_INSTANCE:
 
65
            raise errors.InvalidURL(path=base)
46
66
 
47
67
    def _requires_launchpad_login(self, scheme, netloc, path, query,
48
68
                                  fragment):
55
75
                and (netloc.endswith('launchpad.net')
56
76
                     or netloc.endswith('launchpad.dev')))
57
77
 
58
 
    def look_up(self, name, url):
59
 
        """See DirectoryService.look_up"""
60
 
        return self._resolve(url)
61
 
 
62
 
    def _resolve(self, url,
 
78
    def _resolve(self, abspath,
63
79
                 _request_factory=ResolveLaunchpadPathRequest,
64
80
                 _lp_login=None):
65
81
        """Resolve the base URL for this transport."""
66
 
        result = urlsplit(url)
 
82
        path = urlsplit(abspath)[2].lstrip('/')
67
83
        # 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)
 
84
        resolve = _request_factory(path)
 
85
        service = LaunchpadService(lp_instance=self.lp_instance)
75
86
        try:
76
87
            result = resolve.submit(service)
77
88
        except xmlrpclib.Fault, fault:
78
89
            raise errors.InvalidURL(
79
 
                path=url, extra=fault.faultString)
 
90
                path=abspath, extra=fault.faultString)
80
91
 
81
92
        if 'launchpad' in debug.debug_flags:
82
93
            trace.mutter("resolve_lp_path(%r) == %r", path, result)
83
94
 
84
95
        if _lp_login is None:
85
96
            _lp_login = get_lp_login()
86
 
        _warned_login = False
87
97
        for url in result['urls']:
88
98
            scheme, netloc, path, query, fragment = urlsplit(url)
89
99
            if self._requires_launchpad_login(scheme, netloc, path, query,
90
100
                                              fragment):
91
101
                # Only accept launchpad.net bzr+ssh URLs if we know
92
102
                # the user's Launchpad login:
93
 
                if _lp_login is not None:
94
 
                    break
95
103
                if _lp_login is None:
96
 
                    if not _warned_login:
97
 
                        trace.warning('You have not informed bzr of your '
98
 
                                'launchpad login. If you are attempting a\n'
99
 
                                'write operation and it fails, run '
100
 
                                '"bzr launchpad-login YOUR_ID" and try again.')
101
 
                        _warned_login = True
 
104
                    continue
 
105
                url = urlunsplit((scheme, '%s@%s' % (_lp_login, netloc),
 
106
                                  path, query, fragment))
 
107
                break
102
108
            else:
103
109
                # Use the URL if we can create a transport for it.
104
110
                try:
108
114
                else:
109
115
                    break
110
116
        else:
111
 
            raise errors.InvalidURL(path=url, extra='no supported schemes')
 
117
            raise errors.InvalidURL(path=abspath,
 
118
                                    extra='no supported schemes')
112
119
        return url
113
120
 
 
121
    def _request_redirect(self, relpath):
 
122
        source = urlutils.join(self.base, relpath)
 
123
        # Split the source location into the branch location, and the
 
124
        # extra path components.
 
125
        pos = source.find('/.bzr/')
 
126
        if pos >= 0:
 
127
            branchpath = source[:pos]
 
128
            extra = source[pos:]
 
129
        else:
 
130
            branchpath = source
 
131
            extra = ''
 
132
        target = self._resolve(branchpath) + extra
 
133
        raise errors.RedirectRequested(
 
134
            source=source,
 
135
            target=target)
 
136
 
 
137
    def get(self, relpath):
 
138
        """See Transport.get()."""
 
139
        self._request_redirect(relpath)
 
140
 
 
141
    def mkdir(self, relpath, mode=None):
 
142
        """See Transport.mkdir()."""
 
143
        self._request_redirect(relpath)
 
144
 
114
145
 
115
146
def get_test_permutations():
116
147
    # Since this transport doesn't do anything once opened, it's not subjected