/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2007-2010 Canonical Ltd
2245.8.3 by Martin Pool
Start adding indirection transport
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2245.8.3 by Martin Pool
Start adding indirection transport
16
17
3251.4.3 by Aaron Bentley
More renames and cleanups
18
"""Directory lookup that uses Launchpad."""
2245.8.3 by Martin Pool
Start adding indirection transport
19
5121.2.1 by Jelmer Vernooij
Remove unused imports in lp_directory.
20
from urlparse import urlsplit
2898.4.4 by James Henstridge
Changes to account for modifications to the XMLRPC API.
21
import xmlrpclib
22
2245.8.4 by Martin Pool
lp:/// indirection works
23
from bzrlib import (
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
24
    debug,
2245.8.4 by Martin Pool
lp:/// indirection works
25
    errors,
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
26
    trace,
2245.8.4 by Martin Pool
lp:/// indirection works
27
    )
28
from bzrlib.transport import (
29
    get_transport,
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
30
    register_urlparse_netloc_protocol,
2245.8.4 by Martin Pool
lp:/// indirection works
31
    )
32
2898.4.7 by James Henstridge
Fix up tests.
33
from bzrlib.plugins.launchpad.lp_registration import (
34
    LaunchpadService, ResolveLaunchpadPathRequest)
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
35
from bzrlib.plugins.launchpad.account import get_lp_login
36
37
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
38
# As bzrlib.transport.remote may not be loaded yet, make sure bzr+ssh
39
# is counted as a netloc protocol.
40
register_urlparse_netloc_protocol('bzr+ssh')
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
41
register_urlparse_netloc_protocol('lp')
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
42
43
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
44
class LaunchpadDirectory(object):
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
45
3031.2.4 by jml at canonical
Only split the URL once.
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
        """
3031.2.3 by jml at canonical
Make the test pass -- don't include sftp URLs if there's no lp login.
53
        return (scheme in ('bzr+ssh', 'sftp')
54
                and (netloc.endswith('launchpad.net')
55
                     or netloc.endswith('launchpad.dev')))
3031.2.1 by jml at canonical
Factor out the method that determines if a URL is a LP url.
56
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
57
    def look_up(self, name, url):
3251.4.5 by Aaron Bentley
Add docstring
58
        """See DirectoryService.look_up"""
3251.4.1 by Aaron Bentley
Convert LP transport into directory service
59
        return self._resolve(url)
60
3251.4.3 by Aaron Bentley
More renames and cleanups
61
    def _resolve(self, url,
2898.4.11 by James Henstridge
Switch back to RedirectRequested based implementation.
62
                 _request_factory=ResolveLaunchpadPathRequest,
63
                 _lp_login=None):
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
64
        """Resolve the base URL for this transport."""
4505.6.2 by Jonathan Lange
Extract a method that gets a service from a URL.
65
        service = LaunchpadService.for_url(url)
3251.4.3 by Aaron Bentley
More renames and cleanups
66
        result = urlsplit(url)
5361.1.1 by John Arbash Meinel
Handle a simple ~ in lp: urls.
67
        if _lp_login is None:
68
            _lp_login = get_lp_login()
69
        path = result[2].strip('/')
70
        if path.startswith('~/'):
71
            if _lp_login is None:
72
                raise errors.InvalidURL(path=url,
73
                    extra='Cannot resolve "~" to your username.'
74
                          ' See "bzr help launchpad-login"')
75
            path = '~' + _lp_login + path[1:]
76
        resolve = _request_factory(path)
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
77
        try:
78
            result = resolve.submit(service)
79
        except xmlrpclib.Fault, fault:
80
            raise errors.InvalidURL(
3251.4.3 by Aaron Bentley
More renames and cleanups
81
                path=url, extra=fault.faultString)
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
82
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
83
        if 'launchpad' in debug.debug_flags:
4505.6.2 by Jonathan Lange
Extract a method that gets a service from a URL.
84
            trace.mutter("resolve_lp_path(%r) == %r", url, result)
2898.4.14 by James Henstridge
* Use urlsplit() to process URLs.
85
3270.4.1 by James Westby
Warn the user when resolving lp: URLs if they haven't set their login.
86
        _warned_login = False
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
87
        for url in result['urls']:
88
            scheme, netloc, path, query, fragment = urlsplit(url)
3031.2.4 by jml at canonical
Only split the URL once.
89
            if self._requires_launchpad_login(scheme, netloc, path, query,
90
                                              fragment):
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
91
                # Only accept launchpad.net bzr+ssh URLs if we know
92
                # the user's Launchpad login:
3777.1.21 by Aaron Bentley
Stop including usernames in resolved lp: urls
93
                if _lp_login is not None:
94
                    break
2898.4.9 by James Henstridge
Add some more tests for the bzr+ssh://bazaar.launchpad.net URL
95
                if _lp_login is None:
3270.4.1 by James Westby
Warn the user when resolving lp: URLs if they haven't set their login.
96
                    if not _warned_login:
3834.1.2 by Martin Pool
Better message when launchpad-login is needed
97
                        trace.warning(
3834.1.3 by Martin Pool
Shorter message about launchpad-login
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".')
3270.4.1 by James Westby
Warn the user when resolving lp: URLs if they haven't set their login.
100
                        _warned_login = True
2898.4.15 by James Henstridge
Use get_transport() to decide whether Bazaar supports a given URL.
101
            else:
102
                # Use the URL if we can create a transport for it.
103
                try:
104
                    get_transport(url)
105
                except (errors.PathError, errors.TransportError):
106
                    pass
107
                else:
108
                    break
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
109
        else:
3251.4.3 by Aaron Bentley
More renames and cleanups
110
            raise errors.InvalidURL(path=url, extra='no supported schemes')
2898.4.8 by James Henstridge
Switch lp: over to a pass-through transport, so that the XMLRPC gets
111
        return url
112
2245.8.3 by Martin Pool
Start adding indirection transport
113
114
def get_test_permutations():
115
    # Since this transport doesn't do anything once opened, it's not subjected
116
    # to the usual transport tests.
117
    return []