/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: Canonical.com Patch Queue Manager
  • Date: 2009-12-21 06:03:07 UTC
  • mfrom: (4665.7.3 serve-init)
  • Revision ID: pqm@pqm.ubuntu.com-20091221060307-uvja3vdy1o6dzzy0
(mbp) example debian init script

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
from bzrlib import (
38
38
    bzrdir,
 
39
    debug,
39
40
    errors,
 
41
    osutils,
40
42
    registry,
41
43
    revision,
42
44
    trace,
86
88
    # XXX: rename this class to BaseSmartServerRequestHandler ?  A request
87
89
    # *handler* is a different concept to the request.
88
90
 
89
 
    def __init__(self, backing_transport, root_client_path='/'):
 
91
    def __init__(self, backing_transport, root_client_path='/', jail_root=None):
90
92
        """Constructor.
91
93
 
92
94
        :param backing_transport: the base transport to be used when performing
96
98
            from the client.  Clients will not be able to refer to paths above
97
99
            this root.  If root_client_path is None, then no translation will
98
100
            be performed on client paths.  Default is '/'.
 
101
        :param jail_root: if specified, the root of the BzrDir.open jail to use
 
102
            instead of backing_transport.
99
103
        """
100
104
        self._backing_transport = backing_transport
 
105
        if jail_root is None:
 
106
            jail_root = backing_transport
 
107
        self._jail_root = jail_root
101
108
        if root_client_path is not None:
102
109
            if not root_client_path.startswith('/'):
103
110
                root_client_path = '/' + root_client_path
155
162
        return self.do_body(body_bytes)
156
163
 
157
164
    def setup_jail(self):
158
 
        jail_info.transports = [self._backing_transport]
 
165
        jail_info.transports = [self._jail_root]
159
166
 
160
167
    def teardown_jail(self):
161
168
        jail_info.transports = None
183
190
            relpath = urlutils.joinpath('/', path)
184
191
            if not relpath.startswith('/'):
185
192
                raise ValueError(relpath)
186
 
            return '.' + relpath
 
193
            return urlutils.escape('.' + relpath)
187
194
        else:
188
195
            raise errors.PathNotChild(client_path, self._root_client_path)
189
196
 
265
272
    # TODO: Better way of representing the body for commands that take it,
266
273
    # and allow it to be streamed into the server.
267
274
 
268
 
    def __init__(self, backing_transport, commands, root_client_path):
 
275
    def __init__(self, backing_transport, commands, root_client_path,
 
276
        jail_root=None):
269
277
        """Constructor.
270
278
 
271
279
        :param backing_transport: a Transport to handle requests for.
275
283
        self._backing_transport = backing_transport
276
284
        self._root_client_path = root_client_path
277
285
        self._commands = commands
 
286
        if jail_root is None:
 
287
            jail_root = backing_transport
 
288
        self._jail_root = jail_root
278
289
        self.response = None
279
290
        self.finished_reading = False
280
291
        self._command = None
 
292
        if 'hpss' in debug.debug_flags:
 
293
            self._request_start_time = osutils.timer_func()
 
294
            cur_thread = threading.currentThread()
 
295
            self._thread_id = getattr(cur_thread, 'ident', None)
 
296
            if self._thread_id is None:
 
297
                self._thread_id = cur_thread.getName()
 
298
 
 
299
    def _trace(self, action, message, extra_bytes=None, include_time=False):
 
300
        # It is a bit of a shame that this functionality overlaps with that of 
 
301
        # ProtocolThreeRequester._trace. However, there is enough difference
 
302
        # that just putting it in a helper doesn't help a lot. And some state
 
303
        # is taken from the instance.
 
304
        if include_time:
 
305
            t = '%5.3fs ' % (osutils.timer_func() - self._request_start_time)
 
306
        else:
 
307
            t = ''
 
308
        if extra_bytes is None:
 
309
            extra = ''
 
310
        else:
 
311
            extra = ' ' + repr(extra_bytes[:40])
 
312
            if len(extra) > 33:
 
313
                extra = extra[:29] + extra[-1] + '...'
 
314
        trace.mutter('%12s: [%s] %s%s%s'
 
315
                     % (action, self._thread_id, t, message, extra))
281
316
 
282
317
    def accept_body(self, bytes):
283
318
        """Accept body data."""
285
320
            # no active command object, so ignore the event.
286
321
            return
287
322
        self._run_handler_code(self._command.do_chunk, (bytes,), {})
 
323
        if 'hpss' in debug.debug_flags:
 
324
            self._trace('accept body',
 
325
                        '%d bytes' % (len(bytes),), bytes)
288
326
 
289
327
    def end_of_body(self):
290
328
        """No more body data will be received."""
291
329
        self._run_handler_code(self._command.do_end, (), {})
292
330
        # cannot read after this.
293
331
        self.finished_reading = 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, {})
 
332
        if 'hpss' in debug.debug_flags:
 
333
            self._trace('end of body', '', include_time=True)
303
334
 
304
335
    def _run_handler_code(self, callable, args, kwargs):
305
336
        """Run some handler specific code 'callable'.
334
365
 
335
366
    def headers_received(self, headers):
336
367
        # Just a no-op at the moment.
337
 
        pass
 
368
        if 'hpss' in debug.debug_flags:
 
369
            self._trace('headers', repr(headers))
338
370
 
339
371
    def args_received(self, args):
340
372
        cmd = args[0]
342
374
        try:
343
375
            command = self._commands.get(cmd)
344
376
        except LookupError:
 
377
            if 'hpss' in debug.debug_flags:
 
378
                self._trace('hpss unknown request', 
 
379
                            cmd, repr(args)[1:-1])
345
380
            raise errors.UnknownSmartMethod(cmd)
346
 
        self._command = command(self._backing_transport)
 
381
        if 'hpss' in debug.debug_flags:
 
382
            from bzrlib.smart import vfs
 
383
            if issubclass(command, vfs.VfsRequest):
 
384
                action = 'hpss vfs req'
 
385
            else:
 
386
                action = 'hpss request'
 
387
            self._trace(action, 
 
388
                        '%s %s' % (cmd, repr(args)[1:-1]))
 
389
        self._command = command(
 
390
            self._backing_transport, self._root_client_path, self._jail_root)
347
391
        self._run_handler_code(self._command.execute, args, {})
348
392
 
349
393
    def end_received(self):
351
395
            # no active command object, so ignore the event.
352
396
            return
353
397
        self._run_handler_code(self._command.do_end, (), {})
 
398
        if 'hpss' in debug.debug_flags:
 
399
            self._trace('end', '', include_time=True)
354
400
 
355
401
    def post_body_error_received(self, error_args):
356
402
        # Just a no-op at the moment.
509
555
request_handlers.register_lazy(
510
556
    'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')
511
557
request_handlers.register_lazy(
 
558
    'BzrDir.open_2.1', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir_2_1')
 
559
request_handlers.register_lazy(
512
560
    'BzrDir.open_branch', 'bzrlib.smart.bzrdir',
513
561
    'SmartServerRequestOpenBranch')
514
562
request_handlers.register_lazy(