/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_indirect.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-11-12 21:23:07 UTC
  • mfrom: (2898.4.17 bzr.lp-urls-bug-121200)
  • Revision ID: pqm@pqm.ubuntu.com-20071112212307-eusj64ymto8l9abk
(robertc) Improved lp:/// URL support for the Launchpad plugin (James Henstridge).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 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
 
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
"""
 
24
 
 
25
from urlparse import urlsplit, urlunsplit
21
26
import xmlrpclib
22
27
 
23
28
from bzrlib import (
24
29
    debug,
25
30
    errors,
26
31
    trace,
 
32
    urlutils,
27
33
    )
28
34
from bzrlib.transport import (
29
35
    get_transport,
30
36
    register_urlparse_netloc_protocol,
 
37
    Transport,
31
38
    )
32
39
 
33
40
from bzrlib.plugins.launchpad.lp_registration import (
41
48
register_urlparse_netloc_protocol('lp')
42
49
 
43
50
 
44
 
class LaunchpadDirectory(object):
45
 
 
46
 
    def _requires_launchpad_login(self, scheme, netloc, path, query,
47
 
                                  fragment):
48
 
        """Does the URL require a Launchpad login in order to be reached?
49
 
 
50
 
        The URL is specified by its parsed components, as returned from
51
 
        urlsplit.
52
 
        """
53
 
        return (scheme in ('bzr+ssh', 'sftp')
54
 
                and (netloc.endswith('launchpad.net')
55
 
                     or netloc.endswith('launchpad.dev')))
56
 
 
57
 
    def look_up(self, name, url):
58
 
        """See DirectoryService.look_up"""
59
 
        return self._resolve(url)
60
 
 
61
 
    def _resolve(self, url,
 
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)
 
64
 
 
65
    def _resolve(self, abspath,
62
66
                 _request_factory=ResolveLaunchpadPathRequest,
63
67
                 _lp_login=None):
64
68
        """Resolve the base URL for this transport."""
65
 
        service = LaunchpadService.for_url(url)
66
 
        result = urlsplit(url)
67
 
        resolve = _request_factory(result[2].strip('/'))
 
69
        path = urlsplit(abspath)[2].lstrip('/')
 
70
        # Perform an XMLRPC request to resolve the path
 
71
        resolve = _request_factory(path)
 
72
        service = LaunchpadService()
68
73
        try:
69
74
            result = resolve.submit(service)
70
75
        except xmlrpclib.Fault, fault:
71
76
            raise errors.InvalidURL(
72
 
                path=url, extra=fault.faultString)
 
77
                path=abspath, extra=fault.faultString)
73
78
 
74
79
        if 'launchpad' in debug.debug_flags:
75
 
            trace.mutter("resolve_lp_path(%r) == %r", url, result)
 
80
            trace.mutter("resolve_lp_path(%r) == %r", path, result)
76
81
 
77
82
        if _lp_login is None:
78
83
            _lp_login = get_lp_login()
79
 
        _warned_login = False
80
84
        for url in result['urls']:
81
85
            scheme, netloc, path, query, fragment = urlsplit(url)
82
 
            if self._requires_launchpad_login(scheme, netloc, path, query,
83
 
                                              fragment):
 
86
            if scheme == 'bzr+ssh' and (netloc.endswith('launchpad.net') or
 
87
                                        netloc.endswith('launchpad.dev')):
84
88
                # Only accept launchpad.net bzr+ssh URLs if we know
85
89
                # the user's Launchpad login:
86
 
                if _lp_login is not None:
87
 
                    break
88
90
                if _lp_login is None:
89
 
                    if not _warned_login:
90
 
                        trace.warning(
91
 
'You have not informed bzr of your Launchpad ID, and you must do this to\n'
92
 
'write to Launchpad or access private data.  See "bzr help launchpad-login".')
93
 
                        _warned_login = True
 
91
                    continue
 
92
                url = urlunsplit((scheme, '%s@%s' % (_lp_login, netloc),
 
93
                                  path, query, fragment))
 
94
                break
94
95
            else:
95
96
                # Use the URL if we can create a transport for it.
96
97
                try:
100
101
                else:
101
102
                    break
102
103
        else:
103
 
            raise errors.InvalidURL(path=url, extra='no supported schemes')
 
104
            raise errors.InvalidURL(path=abspath,
 
105
                                    extra='no supported schemes')
104
106
        return url
105
107
 
 
108
    def _request_redirect(self, relpath):
 
109
        source = urlutils.join(self.base, relpath)
 
110
        # Split the source location into the branch location, and the
 
111
        # extra path components.
 
112
        pos = source.find('/.bzr/')
 
113
        if pos >= 0:
 
114
            branchpath = source[:pos]
 
115
            extra = source[pos:]
 
116
        else:
 
117
            branchpath = source
 
118
            extra = ''
 
119
        target = self._resolve(branchpath) + extra
 
120
        raise errors.RedirectRequested(
 
121
            source=source,
 
122
            target=target)
 
123
 
 
124
    def get(self, relpath):
 
125
        """See Transport.get()."""
 
126
        self._request_redirect(relpath)
 
127
 
 
128
    def mkdir(self, relpath, mode=None):
 
129
        """See Transport.mkdir()."""
 
130
        self._request_redirect(relpath)
 
131
 
106
132
 
107
133
def get_test_permutations():
108
134
    # Since this transport doesn't do anything once opened, it's not subjected