~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: Benoît Pierre
  • Date: 2009-11-02 22:24:29 UTC
  • mto: (4634.96.1 integration-2.0)
  • mto: This revision was merged to the branch mainline in revision 4798.
  • Revision ID: benoit.pierre@gmail.com-20091102222429-xqdyo6n8odh3xbbd
Small fix for handling of short option names in shellcomplete_on_options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
31
31
# of a SmartServerRequest subclass.
32
32
 
33
33
 
 
34
import tempfile
34
35
import threading
35
36
 
36
37
from bzrlib import (
37
38
    bzrdir,
38
 
    debug,
39
39
    errors,
40
 
    osutils,
41
40
    registry,
42
41
    revision,
43
42
    trace,
46
45
from bzrlib.lazy_import import lazy_import
47
46
lazy_import(globals(), """
48
47
from bzrlib.bundle import serializer
49
 
 
50
 
import tempfile
51
 
import thread
52
48
""")
53
49
 
54
50
 
90
86
    # XXX: rename this class to BaseSmartServerRequestHandler ?  A request
91
87
    # *handler* is a different concept to the request.
92
88
 
93
 
    def __init__(self, backing_transport, root_client_path='/', jail_root=None):
 
89
    def __init__(self, backing_transport, root_client_path='/'):
94
90
        """Constructor.
95
91
 
96
92
        :param backing_transport: the base transport to be used when performing
100
96
            from the client.  Clients will not be able to refer to paths above
101
97
            this root.  If root_client_path is None, then no translation will
102
98
            be performed on client paths.  Default is '/'.
103
 
        :param jail_root: if specified, the root of the BzrDir.open jail to use
104
 
            instead of backing_transport.
105
99
        """
106
100
        self._backing_transport = backing_transport
107
 
        if jail_root is None:
108
 
            jail_root = backing_transport
109
 
        self._jail_root = jail_root
110
101
        if root_client_path is not None:
111
102
            if not root_client_path.startswith('/'):
112
103
                root_client_path = '/' + root_client_path
135
126
        It will return a SmartServerResponse if the command does not expect a
136
127
        body.
137
128
 
138
 
        :param args: the arguments of the request.
 
129
        :param *args: the arguments of the request.
139
130
        """
140
131
        self._check_enabled()
141
132
        return self.do(*args)
164
155
        return self.do_body(body_bytes)
165
156
 
166
157
    def setup_jail(self):
167
 
        jail_info.transports = [self._jail_root]
 
158
        jail_info.transports = [self._backing_transport]
168
159
 
169
160
    def teardown_jail(self):
170
161
        jail_info.transports = None
192
183
            relpath = urlutils.joinpath('/', path)
193
184
            if not relpath.startswith('/'):
194
185
                raise ValueError(relpath)
195
 
            return urlutils.escape('.' + relpath)
 
186
            return '.' + relpath
196
187
        else:
197
188
            raise errors.PathNotChild(client_path, self._root_client_path)
198
189
 
274
265
    # TODO: Better way of representing the body for commands that take it,
275
266
    # and allow it to be streamed into the server.
276
267
 
277
 
    def __init__(self, backing_transport, commands, root_client_path,
278
 
        jail_root=None):
 
268
    def __init__(self, backing_transport, commands, root_client_path):
279
269
        """Constructor.
280
270
 
281
271
        :param backing_transport: a Transport to handle requests for.
285
275
        self._backing_transport = backing_transport
286
276
        self._root_client_path = root_client_path
287
277
        self._commands = commands
288
 
        if jail_root is None:
289
 
            jail_root = backing_transport
290
 
        self._jail_root = jail_root
291
278
        self.response = None
292
279
        self.finished_reading = False
293
280
        self._command = None
294
 
        if 'hpss' in debug.debug_flags:
295
 
            self._request_start_time = osutils.timer_func()
296
 
            self._thread_id = thread.get_ident()
297
 
 
298
 
    def _trace(self, action, message, extra_bytes=None, include_time=False):
299
 
        # It is a bit of a shame that this functionality overlaps with that of 
300
 
        # ProtocolThreeRequester._trace. However, there is enough difference
301
 
        # that just putting it in a helper doesn't help a lot. And some state
302
 
        # is taken from the instance.
303
 
        if include_time:
304
 
            t = '%5.3fs ' % (osutils.timer_func() - self._request_start_time)
305
 
        else:
306
 
            t = ''
307
 
        if extra_bytes is None:
308
 
            extra = ''
309
 
        else:
310
 
            extra = ' ' + repr(extra_bytes[:40])
311
 
            if len(extra) > 33:
312
 
                extra = extra[:29] + extra[-1] + '...'
313
 
        trace.mutter('%12s: [%s] %s%s%s'
314
 
                     % (action, self._thread_id, t, message, extra))
315
281
 
316
282
    def accept_body(self, bytes):
317
283
        """Accept body data."""
319
285
            # no active command object, so ignore the event.
320
286
            return
321
287
        self._run_handler_code(self._command.do_chunk, (bytes,), {})
322
 
        if 'hpss' in debug.debug_flags:
323
 
            self._trace('accept body',
324
 
                        '%d bytes' % (len(bytes),), bytes)
325
288
 
326
289
    def end_of_body(self):
327
290
        """No more body data will be received."""
328
291
        self._run_handler_code(self._command.do_end, (), {})
329
292
        # cannot read after this.
330
293
        self.finished_reading = True
331
 
        if 'hpss' in debug.debug_flags:
332
 
            self._trace('end of body', '', include_time=True)
 
294
 
 
295
    def dispatch_command(self, cmd, args):
 
296
        """Deprecated compatibility method.""" # XXX XXX
 
297
        try:
 
298
            command = self._commands.get(cmd)
 
299
        except LookupError:
 
300
            raise errors.UnknownSmartMethod(cmd)
 
301
        self._command = command(self._backing_transport, self._root_client_path)
 
302
        self._run_handler_code(self._command.execute, args, {})
333
303
 
334
304
    def _run_handler_code(self, callable, args, kwargs):
335
305
        """Run some handler specific code 'callable'.
364
334
 
365
335
    def headers_received(self, headers):
366
336
        # Just a no-op at the moment.
367
 
        if 'hpss' in debug.debug_flags:
368
 
            self._trace('headers', repr(headers))
 
337
        pass
369
338
 
370
339
    def args_received(self, args):
371
340
        cmd = args[0]
373
342
        try:
374
343
            command = self._commands.get(cmd)
375
344
        except LookupError:
376
 
            if 'hpss' in debug.debug_flags:
377
 
                self._trace('hpss unknown request', 
378
 
                            cmd, repr(args)[1:-1])
379
345
            raise errors.UnknownSmartMethod(cmd)
380
 
        if 'hpss' in debug.debug_flags:
381
 
            from bzrlib.smart import vfs
382
 
            if issubclass(command, vfs.VfsRequest):
383
 
                action = 'hpss vfs req'
384
 
            else:
385
 
                action = 'hpss request'
386
 
            self._trace(action, 
387
 
                        '%s %s' % (cmd, repr(args)[1:-1]))
388
 
        self._command = command(
389
 
            self._backing_transport, self._root_client_path, self._jail_root)
 
346
        self._command = command(self._backing_transport)
390
347
        self._run_handler_code(self._command.execute, args, {})
391
348
 
392
349
    def end_received(self):
394
351
            # no active command object, so ignore the event.
395
352
            return
396
353
        self._run_handler_code(self._command.do_end, (), {})
397
 
        if 'hpss' in debug.debug_flags:
398
 
            self._trace('end', '', include_time=True)
399
354
 
400
355
    def post_body_error_received(self, error_args):
401
356
        # Just a no-op at the moment.
447
402
        return ('TokenMismatch', err.given_token, err.lock_token)
448
403
    elif isinstance(err, errors.LockContention):
449
404
        return ('LockContention',)
450
 
    elif isinstance(err, MemoryError):
451
 
        # GZ 2011-02-24: Copy bzrlib.trace -Dmem_dump functionality here?
452
 
        return ('MemoryError',)
453
405
    # Unserialisable error.  Log it, and return a generic error
454
406
    trace.log_exception_quietly()
455
 
    return ('error', trace._qualified_exception_name(err.__class__, True),
456
 
        str(err))
 
407
    return ('error', str(err))
457
408
 
458
409
 
459
410
class HelloRequest(SmartServerRequest):
506
457
    'Branch.set_tags_bytes', 'bzrlib.smart.branch',
507
458
    'SmartServerBranchSetTagsBytes')
508
459
request_handlers.register_lazy(
509
 
    'Branch.heads_to_fetch', 'bzrlib.smart.branch',
510
 
    'SmartServerBranchHeadsToFetch')
511
 
request_handlers.register_lazy(
512
460
    'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
513
461
request_handlers.register_lazy(
514
462
    'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
518
466
    'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
519
467
request_handlers.register_lazy( 'Branch.set_config_option',
520
468
    'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOption')
521
 
request_handlers.register_lazy( 'Branch.set_config_option_dict',
522
 
    'bzrlib.smart.branch', 'SmartServerBranchRequestSetConfigOptionDict')
523
469
request_handlers.register_lazy( 'Branch.set_last_revision',
524
470
    'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
525
471
request_handlers.register_lazy(
563
509
request_handlers.register_lazy(
564
510
    'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
565
511
request_handlers.register_lazy(
566
 
    'BzrDir.open_2.1', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir_2_1')
567
 
request_handlers.register_lazy(
568
512
    'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
569
513
    'SmartServerRequestOpenBranch')
570
514
request_handlers.register_lazy(
571
515
    'BzrDir.open_branchV2', 'bzrlib.smart.bzrdir',
572
516
    'SmartServerRequestOpenBranchV2')
573
517
request_handlers.register_lazy(
574
 
    'BzrDir.open_branchV3', 'bzrlib.smart.bzrdir',
575
 
    'SmartServerRequestOpenBranchV3')
576
 
request_handlers.register_lazy(
577
518
    'delete', 'bzrlib.smart.vfs', 'DeleteRequest')
578
519
request_handlers.register_lazy(
579
520
    'get', 'bzrlib.smart.vfs', 'GetRequest')