/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

  • Committer: Colin D Bennett
  • Date: 2009-02-12 17:21:02 UTC
  • mto: This revision was merged to the branch mainline in revision 4008.
  • Revision ID: colin@gibibit.com-20090212172102-0t7xufywds9l1g33
Strip trailing whitespace.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 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
**XXX**:
 
20
 
 
21
The class names are a little confusing: the protocol will instantiate a
 
22
SmartServerRequestHandler, whose dispatch_command method creates an instance of
 
23
a SmartServerRequest subclass.
 
24
 
 
25
The request_handlers registry tracks SmartServerRequest classes (rather than
 
26
SmartServerRequestHandler).
 
27
"""
 
28
 
 
29
import tempfile
 
30
 
 
31
from bzrlib import (
 
32
    bzrdir,
 
33
    errors,
 
34
    registry,
 
35
    revision,
 
36
    urlutils,
 
37
    )
 
38
from bzrlib.lazy_import import lazy_import
 
39
lazy_import(globals(), """
 
40
from bzrlib.bundle import serializer
 
41
""")
 
42
 
 
43
 
 
44
class SmartServerRequest(object):
 
45
    """Base class for request handlers.
 
46
    
 
47
    To define a new request, subclass this class and override the `do` method
 
48
    (and if appropriate, `do_body` as well).  Request implementors should take
 
49
    care to call `translate_client_path` and `transport_from_client_path` as
 
50
    appropriate when dealing with paths received from the client.
 
51
    """
 
52
    # XXX: rename this class to BaseSmartServerRequestHandler ?  A request
 
53
    # *handler* is a different concept to the request.
 
54
 
 
55
    def __init__(self, backing_transport, root_client_path='/'):
 
56
        """Constructor.
 
57
 
 
58
        :param backing_transport: the base transport to be used when performing
 
59
            this request.
 
60
        :param root_client_path: the client path that maps to the root of
 
61
            backing_transport.  This is used to interpret relpaths received
 
62
            from the client.  Clients will not be able to refer to paths above
 
63
            this root.  If root_client_path is None, then no translation will
 
64
            be performed on client paths.  Default is '/'.
 
65
        """
 
66
        self._backing_transport = backing_transport
 
67
        if root_client_path is not None:
 
68
            if not root_client_path.startswith('/'):
 
69
                root_client_path = '/' + root_client_path
 
70
            if not root_client_path.endswith('/'):
 
71
                root_client_path += '/'
 
72
        self._root_client_path = root_client_path
 
73
        self._body_chunks = []
 
74
 
 
75
    def _check_enabled(self):
 
76
        """Raises DisabledMethod if this method is disabled."""
 
77
        pass
 
78
 
 
79
    def do(self, *args):
 
80
        """Mandatory extension point for SmartServerRequest subclasses.
 
81
        
 
82
        Subclasses must implement this.
 
83
        
 
84
        This should return a SmartServerResponse if this command expects to
 
85
        receive no body.
 
86
        """
 
87
        raise NotImplementedError(self.do)
 
88
 
 
89
    def execute(self, *args):
 
90
        """Public entry point to execute this request.
 
91
 
 
92
        It will return a SmartServerResponse if the command does not expect a
 
93
        body.
 
94
 
 
95
        :param *args: the arguments of the request.
 
96
        """
 
97
        self._check_enabled()
 
98
        return self.do(*args)
 
99
 
 
100
    def do_body(self, body_bytes):
 
101
        """Called if the client sends a body with the request.
 
102
 
 
103
        The do() method is still called, and must have returned None.
 
104
        
 
105
        Must return a SmartServerResponse.
 
106
        """
 
107
        raise NotImplementedError(self.do_body)
 
108
 
 
109
    def do_chunk(self, chunk_bytes):
 
110
        """Called with each body chunk if the request has a streamed body.
 
111
 
 
112
        The do() method is still called, and must have returned None.
 
113
        """
 
114
        self._body_chunks.append(chunk_bytes)
 
115
 
 
116
    def do_end(self):
 
117
        """Called when the end of the request has been received."""
 
118
        body_bytes = ''.join(self._body_chunks)
 
119
        self._body_chunks = None
 
120
        return self.do_body(body_bytes)
 
121
    
 
122
    def translate_client_path(self, client_path):
 
123
        """Translate a path received from a network client into a local
 
124
        relpath.
 
125
 
 
126
        All paths received from the client *must* be translated.
 
127
 
 
128
        :param client_path: the path from the client.
 
129
        :returns: a relpath that may be used with self._backing_transport
 
130
            (unlike the untranslated client_path, which must not be used with
 
131
            the backing transport).
 
132
        """
 
133
        if self._root_client_path is None:
 
134
            # no translation necessary!
 
135
            return client_path
 
136
        if not client_path.startswith('/'):
 
137
            client_path = '/' + client_path
 
138
        if client_path.startswith(self._root_client_path):
 
139
            path = client_path[len(self._root_client_path):]
 
140
            relpath = urlutils.joinpath('/', path)
 
141
            if not relpath.startswith('/'):
 
142
                raise ValueError(relpath)
 
143
            return '.' + relpath
 
144
        else:
 
145
            raise errors.PathNotChild(client_path, self._root_client_path)
 
146
 
 
147
    def transport_from_client_path(self, client_path):
 
148
        """Get a backing transport corresponding to the location referred to by
 
149
        a network client.
 
150
 
 
151
        :seealso: translate_client_path
 
152
        :returns: a transport cloned from self._backing_transport
 
153
        """
 
154
        relpath = self.translate_client_path(client_path)
 
155
        return self._backing_transport.clone(relpath)
 
156
 
 
157
 
 
158
class SmartServerResponse(object):
 
159
    """A response to a client request.
 
160
    
 
161
    This base class should not be used. Instead use
 
162
    SuccessfulSmartServerResponse and FailedSmartServerResponse as appropriate.
 
163
    """
 
164
 
 
165
    def __init__(self, args, body=None, body_stream=None):
 
166
        """Constructor.
 
167
 
 
168
        :param args: tuple of response arguments.
 
169
        :param body: string of a response body.
 
170
        :param body_stream: iterable of bytestrings to be streamed to the
 
171
            client.
 
172
        """
 
173
        self.args = args
 
174
        if body is not None and body_stream is not None:
 
175
            raise errors.BzrError(
 
176
                "'body' and 'body_stream' are mutually exclusive.")
 
177
        self.body = body
 
178
        self.body_stream = body_stream
 
179
 
 
180
    def __eq__(self, other):
 
181
        if other is None:
 
182
            return False
 
183
        return (other.args == self.args and
 
184
                other.body == self.body and
 
185
                other.body_stream is self.body_stream)
 
186
 
 
187
    def __repr__(self):
 
188
        return "<%s args=%r body=%r>" % (self.__class__.__name__,
 
189
            self.args, self.body)
 
190
 
 
191
 
 
192
class FailedSmartServerResponse(SmartServerResponse):
 
193
    """A SmartServerResponse for a request which failed."""
 
194
 
 
195
    def is_successful(self):
 
196
        """FailedSmartServerResponse are not successful."""
 
197
        return False
 
198
 
 
199
 
 
200
class SuccessfulSmartServerResponse(SmartServerResponse):
 
201
    """A SmartServerResponse for a successfully completed request."""
 
202
 
 
203
    def is_successful(self):
 
204
        """SuccessfulSmartServerResponse are successful."""
 
205
        return True
 
206
 
 
207
 
 
208
class SmartServerRequestHandler(object):
 
209
    """Protocol logic for smart server.
 
210
    
 
211
    This doesn't handle serialization at all, it just processes requests and
 
212
    creates responses.
 
213
    """
 
214
 
 
215
    # IMPORTANT FOR IMPLEMENTORS: It is important that SmartServerRequestHandler
 
216
    # not contain encoding or decoding logic to allow the wire protocol to vary
 
217
    # from the object protocol: we will want to tweak the wire protocol separate
 
218
    # from the object model, and ideally we will be able to do that without
 
219
    # having a SmartServerRequestHandler subclass for each wire protocol, rather
 
220
    # just a Protocol subclass.
 
221
 
 
222
    # TODO: Better way of representing the body for commands that take it,
 
223
    # and allow it to be streamed into the server.
 
224
 
 
225
    def __init__(self, backing_transport, commands, root_client_path):
 
226
        """Constructor.
 
227
 
 
228
        :param backing_transport: a Transport to handle requests for.
 
229
        :param commands: a registry mapping command names to SmartServerRequest
 
230
            subclasses. e.g. bzrlib.transport.smart.vfs.vfs_commands.
 
231
        """
 
232
        self._backing_transport = backing_transport
 
233
        self._root_client_path = root_client_path
 
234
        self._commands = commands
 
235
        self.response = None
 
236
        self.finished_reading = False
 
237
        self._command = None
 
238
 
 
239
    def accept_body(self, bytes):
 
240
        """Accept body data."""
 
241
        self._run_handler_code(self._command.do_chunk, (bytes,), {})
 
242
        
 
243
    def end_of_body(self):
 
244
        """No more body data will be received."""
 
245
        self._run_handler_code(self._command.do_end, (), {})
 
246
        # cannot read after this.
 
247
        self.finished_reading = True
 
248
 
 
249
    def dispatch_command(self, cmd, args):
 
250
        """Deprecated compatibility method.""" # XXX XXX
 
251
        try:
 
252
            command = self._commands.get(cmd)
 
253
        except LookupError:
 
254
            raise errors.UnknownSmartMethod(cmd)
 
255
        self._command = command(self._backing_transport, self._root_client_path)
 
256
        self._run_handler_code(self._command.execute, args, {})
 
257
 
 
258
    def _run_handler_code(self, callable, args, kwargs):
 
259
        """Run some handler specific code 'callable'.
 
260
 
 
261
        If a result is returned, it is considered to be the commands response,
 
262
        and finished_reading is set true, and its assigned to self.response.
 
263
 
 
264
        Any exceptions caught are translated and a response object created
 
265
        from them.
 
266
        """
 
267
        result = self._call_converting_errors(callable, args, kwargs)
 
268
 
 
269
        if result is not None:
 
270
            self.response = result
 
271
            self.finished_reading = True
 
272
 
 
273
    def _call_converting_errors(self, callable, args, kwargs):
 
274
        """Call callable converting errors to Response objects."""
 
275
        # XXX: most of this error conversion is VFS-related, and thus ought to
 
276
        # be in SmartServerVFSRequestHandler somewhere.
 
277
        try:
 
278
            return callable(*args, **kwargs)
 
279
        except errors.NoSuchFile, e:
 
280
            return FailedSmartServerResponse(('NoSuchFile', e.path))
 
281
        except errors.FileExists, e:
 
282
            return FailedSmartServerResponse(('FileExists', e.path))
 
283
        except errors.DirectoryNotEmpty, e:
 
284
            return FailedSmartServerResponse(('DirectoryNotEmpty', e.path))
 
285
        except errors.ShortReadvError, e:
 
286
            return FailedSmartServerResponse(('ShortReadvError',
 
287
                e.path, str(e.offset), str(e.length), str(e.actual)))
 
288
        except errors.UnstackableRepositoryFormat, e:
 
289
            return FailedSmartServerResponse(('UnstackableRepositoryFormat',
 
290
                str(e.format), e.url))
 
291
        except errors.UnstackableBranchFormat, e:
 
292
            return FailedSmartServerResponse(('UnstackableBranchFormat',
 
293
                str(e.format), e.url))
 
294
        except errors.NotStacked, e:
 
295
            return FailedSmartServerResponse(('NotStacked',))
 
296
        except UnicodeError, e:
 
297
            # If it is a DecodeError, than most likely we are starting
 
298
            # with a plain string
 
299
            str_or_unicode = e.object
 
300
            if isinstance(str_or_unicode, unicode):
 
301
                # XXX: UTF-8 might have \x01 (our protocol v1 and v2 seperator
 
302
                # byte) in it, so this encoding could cause broken responses.
 
303
                # Newer clients use protocol v3, so will be fine.
 
304
                val = 'u:' + str_or_unicode.encode('utf-8')
 
305
            else:
 
306
                val = 's:' + str_or_unicode.encode('base64')
 
307
            # This handles UnicodeEncodeError or UnicodeDecodeError
 
308
            return FailedSmartServerResponse((e.__class__.__name__,
 
309
                    e.encoding, val, str(e.start), str(e.end), e.reason))
 
310
        except errors.TransportNotPossible, e:
 
311
            if e.msg == "readonly transport":
 
312
                return FailedSmartServerResponse(('ReadOnlyError', ))
 
313
            else:
 
314
                raise
 
315
        except errors.ReadError, e:
 
316
            # cannot read the file
 
317
            return FailedSmartServerResponse(('ReadError', e.path))
 
318
        except errors.PermissionDenied, e:
 
319
            return FailedSmartServerResponse(
 
320
                ('PermissionDenied', e.path, e.extra))
 
321
 
 
322
    def headers_received(self, headers):
 
323
        # Just a no-op at the moment.
 
324
        pass
 
325
 
 
326
    def args_received(self, args):
 
327
        cmd = args[0]
 
328
        args = args[1:]
 
329
        try:
 
330
            command = self._commands.get(cmd)
 
331
        except LookupError:
 
332
            raise errors.UnknownSmartMethod(cmd)
 
333
        self._command = command(self._backing_transport)
 
334
        self._run_handler_code(self._command.execute, args, {})
 
335
 
 
336
    def end_received(self):
 
337
        self._run_handler_code(self._command.do_end, (), {})
 
338
 
 
339
    def post_body_error_received(self, error_args):
 
340
        # Just a no-op at the moment.
 
341
        pass
 
342
 
 
343
 
 
344
class HelloRequest(SmartServerRequest):
 
345
    """Answer a version request with the highest protocol version this server
 
346
    supports.
 
347
    """
 
348
 
 
349
    def do(self):
 
350
        return SuccessfulSmartServerResponse(('ok', '2'))
 
351
 
 
352
 
 
353
class GetBundleRequest(SmartServerRequest):
 
354
    """Get a bundle of from the null revision to the specified revision."""
 
355
 
 
356
    def do(self, path, revision_id):
 
357
        # open transport relative to our base
 
358
        t = self.transport_from_client_path(path)
 
359
        control, extra_path = bzrdir.BzrDir.open_containing_from_transport(t)
 
360
        repo = control.open_repository()
 
361
        tmpf = tempfile.TemporaryFile()
 
362
        base_revision = revision.NULL_REVISION
 
363
        serializer.write_bundle(repo, revision_id, base_revision, tmpf)
 
364
        tmpf.seek(0)
 
365
        return SuccessfulSmartServerResponse((), tmpf.read())
 
366
 
 
367
 
 
368
class SmartServerIsReadonly(SmartServerRequest):
 
369
    # XXX: this request method belongs somewhere else.
 
370
 
 
371
    def do(self):
 
372
        if self._backing_transport.is_readonly():
 
373
            answer = 'yes'
 
374
        else:
 
375
            answer = 'no'
 
376
        return SuccessfulSmartServerResponse((answer,))
 
377
 
 
378
 
 
379
request_handlers = registry.Registry()
 
380
request_handlers.register_lazy(
 
381
    'append', 'bzrlib.smart.vfs', 'AppendRequest')
 
382
request_handlers.register_lazy(
 
383
    'Branch.get_config_file', 'bzrlib.smart.branch', 'SmartServerBranchGetConfigFile')
 
384
request_handlers.register_lazy(
 
385
    'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
 
386
request_handlers.register_lazy(
 
387
    'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
 
388
request_handlers.register_lazy(
 
389
    'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
 
390
request_handlers.register_lazy(
 
391
    'Branch.revision_history', 'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
 
392
request_handlers.register_lazy(
 
393
    'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
 
394
request_handlers.register_lazy(
 
395
    'Branch.set_last_revision_info', 'bzrlib.smart.branch',
 
396
    'SmartServerBranchRequestSetLastRevisionInfo')
 
397
request_handlers.register_lazy(
 
398
    'Branch.set_last_revision_ex', 'bzrlib.smart.branch',
 
399
    'SmartServerBranchRequestSetLastRevisionEx')
 
400
request_handlers.register_lazy(
 
401
    'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
 
402
request_handlers.register_lazy(
 
403
    'BzrDir.find_repository', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV1')
 
404
request_handlers.register_lazy(
 
405
    'BzrDir.find_repositoryV2', 'bzrlib.smart.bzrdir', 'SmartServerRequestFindRepositoryV2')
 
406
request_handlers.register_lazy(
 
407
    'BzrDirFormat.initialize', 'bzrlib.smart.bzrdir', 'SmartServerRequestInitializeBzrDir')
 
408
request_handlers.register_lazy(
 
409
    'BzrDir.open_branch', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBranch')
 
410
request_handlers.register_lazy(
 
411
    'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
 
412
request_handlers.register_lazy(
 
413
    'get', 'bzrlib.smart.vfs', 'GetRequest')
 
414
request_handlers.register_lazy(
 
415
    'get_bundle', 'bzrlib.smart.request', 'GetBundleRequest')
 
416
request_handlers.register_lazy(
 
417
    'has', 'bzrlib.smart.vfs', 'HasRequest')
 
418
request_handlers.register_lazy(
 
419
    'hello', 'bzrlib.smart.request', 'HelloRequest')
 
420
request_handlers.register_lazy(
 
421
    'iter_files_recursive', 'bzrlib.smart.vfs', 'IterFilesRecursiveRequest')
 
422
request_handlers.register_lazy(
 
423
    'list_dir', 'bzrlib.smart.vfs', 'ListDirRequest')
 
424
request_handlers.register_lazy(
 
425
    'mkdir', 'bzrlib.smart.vfs', 'MkdirRequest')
 
426
request_handlers.register_lazy(
 
427
    'move', 'bzrlib.smart.vfs', 'MoveRequest')
 
428
request_handlers.register_lazy(
 
429
    'put', 'bzrlib.smart.vfs', 'PutRequest')
 
430
request_handlers.register_lazy(
 
431
    'put_non_atomic', 'bzrlib.smart.vfs', 'PutNonAtomicRequest')
 
432
request_handlers.register_lazy(
 
433
    'readv', 'bzrlib.smart.vfs', 'ReadvRequest')
 
434
request_handlers.register_lazy(
 
435
    'rename', 'bzrlib.smart.vfs', 'RenameRequest')
 
436
request_handlers.register_lazy(
 
437
    'PackRepository.autopack', 'bzrlib.smart.packrepository',
 
438
    'SmartServerPackRepositoryAutopack')
 
439
request_handlers.register_lazy('Repository.gather_stats',
 
440
                               'bzrlib.smart.repository',
 
441
                               'SmartServerRepositoryGatherStats')
 
442
request_handlers.register_lazy('Repository.get_parent_map',
 
443
                               'bzrlib.smart.repository',
 
444
                               'SmartServerRepositoryGetParentMap')
 
445
request_handlers.register_lazy(
 
446
    'Repository.get_revision_graph', 'bzrlib.smart.repository', 'SmartServerRepositoryGetRevisionGraph')
 
447
request_handlers.register_lazy(
 
448
    'Repository.has_revision', 'bzrlib.smart.repository', 'SmartServerRequestHasRevision')
 
449
request_handlers.register_lazy(
 
450
    'Repository.is_shared', 'bzrlib.smart.repository', 'SmartServerRepositoryIsShared')
 
451
request_handlers.register_lazy(
 
452
    'Repository.lock_write', 'bzrlib.smart.repository', 'SmartServerRepositoryLockWrite')
 
453
request_handlers.register_lazy(
 
454
    'Repository.unlock', 'bzrlib.smart.repository', 'SmartServerRepositoryUnlock')
 
455
request_handlers.register_lazy(
 
456
    'Repository.tarball', 'bzrlib.smart.repository',
 
457
    'SmartServerRepositoryTarball')
 
458
request_handlers.register_lazy(
 
459
    'rmdir', 'bzrlib.smart.vfs', 'RmdirRequest')
 
460
request_handlers.register_lazy(
 
461
    'stat', 'bzrlib.smart.vfs', 'StatRequest')
 
462
request_handlers.register_lazy(
 
463
    'Transport.is_readonly', 'bzrlib.smart.request', 'SmartServerIsReadonly')
 
464
request_handlers.register_lazy(
 
465
    'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')