/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

Fix formatting, remove catch-all for exceptions when opening local repositories.

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