/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/smart/request.py

Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
17
"""Basic server-side logic for dealing with requests."""
 
18
 
 
19
 
 
20
import tempfile
 
21
 
 
22
from bzrlib import bzrdir, errors, registry, revision
 
23
from bzrlib.bundle.serializer import write_bundle
 
24
 
 
25
 
 
26
class SmartServerRequest(object):
 
27
    """Base class for request handlers.
 
28
    """
 
29
 
 
30
    def __init__(self, backing_transport):
 
31
        self._backing_transport = backing_transport
 
32
 
 
33
    def do(self, *args):
 
34
        """Called with the arguments of the request.
 
35
        
 
36
        This should return a SmartServerResponse if this command expects to
 
37
        receive no body.
 
38
        """
 
39
        raise NotImplementedError(self.do)
 
40
 
 
41
    def do_body(self, body_bytes):
 
42
        """Called if the client sends a body with the request.
 
43
        
 
44
        Must return a SmartServerResponse.
 
45
        """
 
46
        # TODO: if a client erroneously sends a request that shouldn't have a
 
47
        # body, what to do?  Probably SmartServerRequestHandler should catch
 
48
        # this NotImplementedError and translate it into a 'bad request' error
 
49
        # to send to the client.
 
50
        raise NotImplementedError(self.do_body)
 
51
 
 
52
 
 
53
class SmartServerResponse(object):
 
54
    """Response generated by SmartServerRequestHandler."""
 
55
 
 
56
    def __init__(self, args, body=None):
 
57
        self.args = args
 
58
        self.body = body
 
59
 
 
60
 
 
61
class SmartServerRequestHandler(object):
 
62
    """Protocol logic for smart server.
 
63
    
 
64
    This doesn't handle serialization at all, it just processes requests and
 
65
    creates responses.
 
66
    """
 
67
 
 
68
    # IMPORTANT FOR IMPLEMENTORS: It is important that SmartServerRequestHandler
 
69
    # not contain encoding or decoding logic to allow the wire protocol to vary
 
70
    # from the object protocol: we will want to tweak the wire protocol separate
 
71
    # from the object model, and ideally we will be able to do that without
 
72
    # having a SmartServerRequestHandler subclass for each wire protocol, rather
 
73
    # just a Protocol subclass.
 
74
 
 
75
    # TODO: Better way of representing the body for commands that take it,
 
76
    # and allow it to be streamed into the server.
 
77
 
 
78
    def __init__(self, backing_transport, commands):
 
79
        """Constructor.
 
80
 
 
81
        :param backing_transport: a Transport to handle requests for.
 
82
        :param commands: a registry mapping command names to SmartServerRequest
 
83
            subclasses. e.g. bzrlib.transport.smart.vfs.vfs_commands.
 
84
        """
 
85
        self._backing_transport = backing_transport
 
86
        self._commands = commands
 
87
        self._body_bytes = ''
 
88
        self.response = None
 
89
        self.finished_reading = False
 
90
        self._command = None
 
91
 
 
92
    def accept_body(self, bytes):
 
93
        """Accept body data."""
 
94
 
 
95
        # TODO: This should be overriden for each command that desired body data
 
96
        # to handle the right format of that data, i.e. plain bytes, a bundle,
 
97
        # etc.  The deserialisation into that format should be done in the
 
98
        # Protocol object.
 
99
 
 
100
        # default fallback is to accumulate bytes.
 
101
        self._body_bytes += bytes
 
102
        
 
103
    def end_of_body(self):
 
104
        """No more body data will be received."""
 
105
        self._run_handler_code(self._command.do_body, (self._body_bytes,), {})
 
106
        # cannot read after this.
 
107
        self.finished_reading = True
 
108
 
 
109
    def dispatch_command(self, cmd, args):
 
110
        """Deprecated compatibility method.""" # XXX XXX
 
111
        try:
 
112
            command = self._commands.get(cmd)
 
113
        except LookupError:
 
114
            raise errors.SmartProtocolError("bad request %r" % (cmd,))
 
115
        self._command = command(self._backing_transport)
 
116
        self._run_handler_code(self._command.do, args, {})
 
117
 
 
118
    def _run_handler_code(self, callable, args, kwargs):
 
119
        """Run some handler specific code 'callable'.
 
120
 
 
121
        If a result is returned, it is considered to be the commands response,
 
122
        and finished_reading is set true, and its assigned to self.response.
 
123
 
 
124
        Any exceptions caught are translated and a response object created
 
125
        from them.
 
126
        """
 
127
        result = self._call_converting_errors(callable, args, kwargs)
 
128
 
 
129
        if result is not None:
 
130
            self.response = result
 
131
            self.finished_reading = True
 
132
 
 
133
    def _call_converting_errors(self, callable, args, kwargs):
 
134
        """Call callable converting errors to Response objects."""
 
135
        # XXX: most of this error conversion is VFS-related, and thus ought to
 
136
        # be in SmartServerVFSRequestHandler somewhere.
 
137
        try:
 
138
            return callable(*args, **kwargs)
 
139
        except errors.NoSuchFile, e:
 
140
            return SmartServerResponse(('NoSuchFile', e.path))
 
141
        except errors.FileExists, e:
 
142
            return SmartServerResponse(('FileExists', e.path))
 
143
        except errors.DirectoryNotEmpty, e:
 
144
            return SmartServerResponse(('DirectoryNotEmpty', e.path))
 
145
        except errors.ShortReadvError, e:
 
146
            return SmartServerResponse(('ShortReadvError',
 
147
                e.path, str(e.offset), str(e.length), str(e.actual)))
 
148
        except UnicodeError, e:
 
149
            # If it is a DecodeError, than most likely we are starting
 
150
            # with a plain string
 
151
            str_or_unicode = e.object
 
152
            if isinstance(str_or_unicode, unicode):
 
153
                # XXX: UTF-8 might have \x01 (our seperator byte) in it.  We
 
154
                # should escape it somehow.
 
155
                val = 'u:' + str_or_unicode.encode('utf-8')
 
156
            else:
 
157
                val = 's:' + str_or_unicode.encode('base64')
 
158
            # This handles UnicodeEncodeError or UnicodeDecodeError
 
159
            return SmartServerResponse((e.__class__.__name__,
 
160
                    e.encoding, val, str(e.start), str(e.end), e.reason))
 
161
        except errors.TransportNotPossible, e:
 
162
            if e.msg == "readonly transport":
 
163
                return SmartServerResponse(('ReadOnlyError', ))
 
164
            else:
 
165
                raise
 
166
 
 
167
 
 
168
class HelloRequest(SmartServerRequest):
 
169
    """Answer a version request with my version."""
 
170
 
 
171
    def do(self):
 
172
        return SmartServerResponse(('ok', '1'))
 
173
 
 
174
 
 
175
class GetBundleRequest(SmartServerRequest):
 
176
 
 
177
    def do(self, path, revision_id):
 
178
        # open transport relative to our base
 
179
        t = self._backing_transport.clone(path)
 
180
        control, extra_path = bzrdir.BzrDir.open_containing_from_transport(t)
 
181
        repo = control.open_repository()
 
182
        tmpf = tempfile.TemporaryFile()
 
183
        base_revision = revision.NULL_REVISION
 
184
        write_bundle(repo, revision_id, base_revision, tmpf)
 
185
        tmpf.seek(0)
 
186
        return SmartServerResponse((), tmpf.read())
 
187
 
 
188
 
 
189
request_handlers = registry.Registry()
 
190
request_handlers.register_lazy(
 
191
    'append', 'bzrlib.smart.vfs', 'AppendRequest')
 
192
request_handlers.register_lazy(
 
193
    'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
 
194
request_handlers.register_lazy(
 
195
    'get', 'bzrlib.smart.vfs', 'GetRequest')
 
196
request_handlers.register_lazy(
 
197
    'get_bundle', 'bzrlib.smart.request', 'GetBundleRequest')
 
198
request_handlers.register_lazy(
 
199
    'has', 'bzrlib.smart.vfs', 'HasRequest')
 
200
request_handlers.register_lazy(
 
201
    'hello', 'bzrlib.smart.request', 'HelloRequest')
 
202
request_handlers.register_lazy(
 
203
    'iter_files_recursive', 'bzrlib.smart.vfs', 'IterFilesRecursive')
 
204
request_handlers.register_lazy(
 
205
    'list_dir', 'bzrlib.smart.vfs', 'ListDirRequest')
 
206
request_handlers.register_lazy(
 
207
    'mkdir', 'bzrlib.smart.vfs', 'MkdirCommand')
 
208
request_handlers.register_lazy(
 
209
    'move', 'bzrlib.smart.vfs', 'MoveCommand')
 
210
request_handlers.register_lazy(
 
211
    'put', 'bzrlib.smart.vfs', 'PutCommand')
 
212
request_handlers.register_lazy(
 
213
    'put_non_atomic', 'bzrlib.smart.vfs', 'PutNonAtomicCommand')
 
214
request_handlers.register_lazy(
 
215
    'readv', 'bzrlib.smart.vfs', 'ReadvCommand')
 
216
request_handlers.register_lazy(
 
217
    'rename', 'bzrlib.smart.vfs', 'RenameCommand')
 
218
request_handlers.register_lazy(
 
219
    'rmdir', 'bzrlib.smart.vfs', 'RmdirCommand')
 
220
request_handlers.register_lazy(
 
221
    'stat', 'bzrlib.smart.vfs', 'StatCommand')
 
222