~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-06-21 23:43:17 UTC
  • mto: (2520.5.2 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070621234317-5w3h8h36oe90sups
Implement new merge directive format

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, urlunsplit
21
 
import xmlrpclib
 
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
"""
22
24
 
23
25
from bzrlib import (
24
 
    debug,
25
26
    errors,
26
 
    trace,
27
 
    urlutils,
28
27
    )
29
28
from bzrlib.transport import (
30
29
    get_transport,
31
 
    register_urlparse_netloc_protocol,
 
30
    Transport,
32
31
    )
33
32
 
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
 
        service = LaunchpadService.for_url(url)
67
 
        result = urlsplit(url)
68
 
        resolve = _request_factory(result[2].strip('/'))
69
 
        try:
70
 
            result = resolve.submit(service)
71
 
        except xmlrpclib.Fault, fault:
72
 
            raise errors.InvalidURL(
73
 
                path=url, extra=fault.faultString)
74
 
 
75
 
        if 'launchpad' in debug.debug_flags:
76
 
            trace.mutter("resolve_lp_path(%r) == %r", url, result)
77
 
 
78
 
        if _lp_login is None:
79
 
            _lp_login = get_lp_login()
80
 
        _warned_login = False
81
 
        for url in result['urls']:
82
 
            scheme, netloc, path, query, fragment = urlsplit(url)
83
 
            if self._requires_launchpad_login(scheme, netloc, path, query,
84
 
                                              fragment):
85
 
                # Only accept launchpad.net bzr+ssh URLs if we know
86
 
                # the user's Launchpad login:
87
 
                if _lp_login is not None:
88
 
                    break
89
 
                if _lp_login is None:
90
 
                    if not _warned_login:
91
 
                        trace.warning(
92
 
'You have not informed bzr of your Launchpad ID, and you must do this to\n'
93
 
'write to Launchpad or access private data.  See "bzr help launchpad-login".')
94
 
                        _warned_login = True
95
 
            else:
96
 
                # Use the URL if we can create a transport for it.
97
 
                try:
98
 
                    get_transport(url)
99
 
                except (errors.PathError, errors.TransportError):
100
 
                    pass
101
 
                else:
102
 
                    break
103
 
        else:
104
 
            raise errors.InvalidURL(path=url, extra='no supported schemes')
105
 
        return url
 
33
 
 
34
def launchpad_transport_indirect(base_url):
 
35
    """Uses Launchpad.net as a directory of open source software"""
 
36
    if base_url.startswith('lp:///'):
 
37
        real_url = 'http://code.launchpad.net/' + base_url[6:]
 
38
    elif base_url.startswith('lp:') and base_url[3] != '/':
 
39
        real_url = 'http://code.launchpad.net/' + base_url[3:]
 
40
    else:
 
41
        raise errors.InvalidURL(path=base_url)
 
42
    return get_transport(real_url)
106
43
 
107
44
 
108
45
def get_test_permutations():