/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
1
# Copyright (C) 2006 Canonical Ltd
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
3192.2.2 by Andrew Bennetts
Just use urllib.unquote, as suggested by John's review.
17
import urllib
2414.1.1 by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch).
18
from urlparse import urlparse
19
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
20
from bzrlib.smart import message, protocol
3104.4.2 by Andrew Bennetts
All tests passing.
21
from bzrlib import urlutils
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
22
23
2414.1.4 by Andrew Bennetts
Rename SmartClient to _SmartClient.
24
class _SmartClient(object):
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
25
3104.4.4 by Andrew Bennetts
Rename parameter to _SmartClient.__init__ to be less confusing.
26
    def __init__(self, shared_connection):
27
        """Constructor.
28
29
        :param shared_connection: a bzrlib.transport._SharedConnection
30
        """
31
        self._shared_connection = shared_connection
2485.8.54 by Vincent Ladeuil
Refactor medium uses by making a distinction betweem shared and real medium.
32
33
    def get_smart_medium(self):
3104.4.4 by Andrew Bennetts
Rename parameter to _SmartClient.__init__ to be less confusing.
34
        return self._shared_connection.connection
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
35
3241.2.1 by Andrew Bennetts
Dynamically choose the best client protocol version in bzrlib.smart.client.
36
    def _build_client_protocol(self):
37
        medium = self.get_smart_medium()
38
        version = medium.protocol_version()
39
        request = medium.get_request()
3245.4.1 by Andrew Bennetts
Merge in the protocol-v3 implementation so far, integrating with the protocol negotiation in bzrlib.smart.client.
40
        if version == 3:
41
            request_encoder = protocol.ProtocolThreeRequester(request)
42
            response_handler = message.ConventionalResponseHandler()
3245.4.7 by Andrew Bennetts
Rename _ProtocolThreeBase to ProtocolThreeDecoder, remove SmartServerRequestProtocolThree.
43
            response_proto = protocol.ProtocolThreeDecoder(response_handler)
3245.4.1 by Andrew Bennetts
Merge in the protocol-v3 implementation so far, integrating with the protocol negotiation in bzrlib.smart.client.
44
            response_handler.setProtoAndMedium(response_proto, request)
45
        elif version == 2:
46
            request_encoder = protocol.SmartClientRequestProtocolTwo(request)
47
            response_handler = request_encoder
3241.2.1 by Andrew Bennetts
Dynamically choose the best client protocol version in bzrlib.smart.client.
48
        else:
3245.4.1 by Andrew Bennetts
Merge in the protocol-v3 implementation so far, integrating with the protocol negotiation in bzrlib.smart.client.
49
            request_encoder = protocol.SmartClientRequestProtocolOne(request)
50
            response_handler = request_encoder
51
        return request_encoder, response_handler
3241.2.1 by Andrew Bennetts
Dynamically choose the best client protocol version in bzrlib.smart.client.
52
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
53
    def call(self, method, *args):
54
        """Call a method on the remote server."""
2414.1.2 by Andrew Bennetts
Deal with review comments.
55
        result, protocol = self.call_expecting_body(method, *args)
2414.1.1 by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch).
56
        protocol.cancel_read_body()
57
        return result
58
2414.1.2 by Andrew Bennetts
Deal with review comments.
59
    def call_expecting_body(self, method, *args):
60
        """Call a method and return the result and the protocol object.
61
        
62
        The body can be read like so::
63
64
            result, smart_protocol = smart_client.call_expecting_body(...)
65
            body = smart_protocol.read_body_bytes()
66
        """
3245.4.1 by Andrew Bennetts
Merge in the protocol-v3 implementation so far, integrating with the protocol negotiation in bzrlib.smart.client.
67
        request_encoder, response_handler = self._build_client_protocol()
3195.3.17 by Andrew Bennetts
Some tests now passing using protocol 3.
68
        request_encoder.call(method, *args)
3245.4.1 by Andrew Bennetts
Merge in the protocol-v3 implementation so far, integrating with the protocol negotiation in bzrlib.smart.client.
69
        return (response_handler.read_response_tuple(expect_body=True),
70
                response_handler)
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
71
72
    def call_with_body_bytes(self, method, args, body):
73
        """Call a method on the remote server with body bytes."""
2018.5.131 by Andrew Bennetts
Be strict about unicode passed to transport.put_{bytes,file} and SmartClient.call_with_body_bytes, fixing part of TestLockableFiles_RemoteLockDir.test_read_write.
74
        if type(method) is not str:
75
            raise TypeError('method must be a byte string, not %r' % (method,))
76
        for arg in args:
77
            if type(arg) is not str:
78
                raise TypeError('args must be byte strings, not %r' % (args,))
79
        if type(body) is not str:
80
            raise TypeError('body must be byte string, not %r' % (body,))
3245.4.1 by Andrew Bennetts
Merge in the protocol-v3 implementation so far, integrating with the protocol negotiation in bzrlib.smart.client.
81
        request_encoder, response_handler = self._build_client_protocol()
82
        request_encoder.call_with_body_bytes((method, ) + args, body)
83
        return response_handler.read_response_tuple()
2414.1.1 by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch).
84
3184.1.10 by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads.
85
    def call_with_body_bytes_expecting_body(self, method, args, body):
86
        """Call a method on the remote server with body bytes."""
87
        if type(method) is not str:
88
            raise TypeError('method must be a byte string, not %r' % (method,))
89
        for arg in args:
90
            if type(arg) is not str:
91
                raise TypeError('args must be byte strings, not %r' % (args,))
92
        if type(body) is not str:
93
            raise TypeError('body must be byte string, not %r' % (body,))
3245.4.1 by Andrew Bennetts
Merge in the protocol-v3 implementation so far, integrating with the protocol negotiation in bzrlib.smart.client.
94
        request_encoder, response_handler = self._build_client_protocol()
95
        request_encoder.call_with_body_bytes((method, ) + args, body)
96
        return (response_handler.read_response_tuple(expect_body=True),
97
                response_handler)
3184.1.10 by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads.
98
2414.1.1 by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch).
99
    def remote_path_from_transport(self, transport):
2414.1.2 by Andrew Bennetts
Deal with review comments.
100
        """Convert transport into a path suitable for using in a request.
101
        
102
        Note that the resulting remote path doesn't encode the host name or
103
        anything but path, so it is only safe to use it in requests sent over
104
        the medium from the matching transport.
105
        """
3104.4.4 by Andrew Bennetts
Rename parameter to _SmartClient.__init__ to be less confusing.
106
        if self._shared_connection.base.startswith('bzr+http://'):
107
            medium_base = self._shared_connection.base
3104.4.2 by Andrew Bennetts
All tests passing.
108
        else:
3104.4.4 by Andrew Bennetts
Rename parameter to _SmartClient.__init__ to be less confusing.
109
            medium_base = urlutils.join(self._shared_connection.base, '/')
3104.4.2 by Andrew Bennetts
All tests passing.
110
            
3192.2.1 by Andrew Bennetts
Don't transmit URL-escaped relpaths in the smart protocol, which is back to how things worked in bzr 1.1 and earlier.
111
        rel_url = urlutils.relative_url(medium_base, transport.base)
3192.2.2 by Andrew Bennetts
Just use urllib.unquote, as suggested by John's review.
112
        return urllib.unquote(rel_url)