~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Aaron Bentley
  • Date: 2007-12-09 23:53:50 UTC
  • mto: This revision was merged to the branch mainline in revision 3133.
  • Revision ID: aaron.bentley@utoronto.ca-20071209235350-qp39yk0xzx7a4f6p
Don't use the base if not cherrypicking

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
        netloc = urlsplit(base)[1]
 
62
        if netloc != '':
 
63
            raise errors.InvalidURL(path=base)
46
64
 
47
65
    def _requires_launchpad_login(self, scheme, netloc, path, query,
48
66
                                  fragment):
55
73
                and (netloc.endswith('launchpad.net')
56
74
                     or netloc.endswith('launchpad.dev')))
57
75
 
58
 
    def look_up(self, name, url):
59
 
        """See DirectoryService.look_up"""
60
 
        return self._resolve(url)
61
 
 
62
 
    def _resolve(self, url,
 
76
    def _resolve(self, abspath,
63
77
                 _request_factory=ResolveLaunchpadPathRequest,
64
78
                 _lp_login=None):
65
79
        """Resolve the base URL for this transport."""
66
 
        result = urlsplit(url)
 
80
        path = urlsplit(abspath)[2].lstrip('/')
67
81
        # 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)
 
82
        resolve = _request_factory(path)
 
83
        service = LaunchpadService()
75
84
        try:
76
85
            result = resolve.submit(service)
77
86
        except xmlrpclib.Fault, fault:
78
87
            raise errors.InvalidURL(
79
 
                path=url, extra=fault.faultString)
 
88
                path=abspath, extra=fault.faultString)
80
89
 
81
90
        if 'launchpad' in debug.debug_flags:
82
91
            trace.mutter("resolve_lp_path(%r) == %r", path, result)
83
92
 
84
93
        if _lp_login is None:
85
94
            _lp_login = get_lp_login()
86
 
        _warned_login = False
87
95
        for url in result['urls']:
88
96
            scheme, netloc, path, query, fragment = urlsplit(url)
89
97
            if self._requires_launchpad_login(scheme, netloc, path, query,
90
98
                                              fragment):
91
99
                # Only accept launchpad.net bzr+ssh URLs if we know
92
100
                # the user's Launchpad login:
93
 
                if _lp_login is not None:
94
 
                    break
95
101
                if _lp_login is None:
96
 
                    if not _warned_login:
97
 
                        trace.warning(
98
 
'You have not informed bzr of your Launchpad ID, and you must do this to\n'
99
 
'write to Launchpad or access private data.  See "bzr help launchpad-login".')
100
 
                        _warned_login = True
 
102
                    continue
 
103
                url = urlunsplit((scheme, '%s@%s' % (_lp_login, netloc),
 
104
                                  path, query, fragment))
 
105
                break
101
106
            else:
102
107
                # Use the URL if we can create a transport for it.
103
108
                try:
107
112
                else:
108
113
                    break
109
114
        else:
110
 
            raise errors.InvalidURL(path=url, extra='no supported schemes')
 
115
            raise errors.InvalidURL(path=abspath,
 
116
                                    extra='no supported schemes')
111
117
        return url
112
118
 
 
119
    def _request_redirect(self, relpath):
 
120
        source = urlutils.join(self.base, relpath)
 
121
        # Split the source location into the branch location, and the
 
122
        # extra path components.
 
123
        pos = source.find('/.bzr/')
 
124
        if pos >= 0:
 
125
            branchpath = source[:pos]
 
126
            extra = source[pos:]
 
127
        else:
 
128
            branchpath = source
 
129
            extra = ''
 
130
        target = self._resolve(branchpath) + extra
 
131
        raise errors.RedirectRequested(
 
132
            source=source,
 
133
            target=target)
 
134
 
 
135
    def get(self, relpath):
 
136
        """See Transport.get()."""
 
137
        self._request_redirect(relpath)
 
138
 
 
139
    def mkdir(self, relpath, mode=None):
 
140
        """See Transport.mkdir()."""
 
141
        self._request_redirect(relpath)
 
142
 
113
143
 
114
144
def get_test_permutations():
115
145
    # Since this transport doesn't do anything once opened, it's not subjected