~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/request.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-07-04 14:02:09 UTC
  • mfrom: (2584.1.1 Aaron's integration)
  • Revision ID: pqm@pqm.ubuntu.com-20070704140209-ldl1njlpcclszadu
Fix #102019 by not asking strace to follow children

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 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
77
77
 
78
78
 
79
79
class SmartServerResponse(object):
80
 
    """Response generated by SmartServerRequestHandler."""
 
80
    """A response to a client request.
 
81
    
 
82
    This base class should not be used. Instead use
 
83
    SuccessfulSmartServerResponse and FailedSmartServerResponse as appropriate.
 
84
    """
81
85
 
82
86
    def __init__(self, args, body=None):
83
87
        self.args = args
89
93
        return other.args == self.args and other.body == self.body
90
94
 
91
95
    def __repr__(self):
92
 
        return "<SmartServerResponse args=%r body=%r>" % (self.args, self.body)
 
96
        return "<SmartServerResponse args=%r body=%r>" % (self.is_successful(), 
 
97
            self.args, self.body)
 
98
 
 
99
 
 
100
class FailedSmartServerResponse(SmartServerResponse):
 
101
    """A SmartServerResponse for a request which failed."""
 
102
 
 
103
    def is_successful(self):
 
104
        """FailedSmartServerResponse are not successful."""
 
105
        return False
 
106
 
 
107
 
 
108
class SuccessfulSmartServerResponse(SmartServerResponse):
 
109
    """A SmartServerResponse for a successfully completed request."""
 
110
 
 
111
    def is_successful(self):
 
112
        """SuccessfulSmartServerResponse are successful."""
 
113
        return True
93
114
 
94
115
 
95
116
class SmartServerRequestHandler(object):
171
192
        try:
172
193
            return callable(*args, **kwargs)
173
194
        except errors.NoSuchFile, e:
174
 
            return SmartServerResponse(('NoSuchFile', e.path))
 
195
            return FailedSmartServerResponse(('NoSuchFile', e.path))
175
196
        except errors.FileExists, e:
176
 
            return SmartServerResponse(('FileExists', e.path))
 
197
            return FailedSmartServerResponse(('FileExists', e.path))
177
198
        except errors.DirectoryNotEmpty, e:
178
 
            return SmartServerResponse(('DirectoryNotEmpty', e.path))
 
199
            return FailedSmartServerResponse(('DirectoryNotEmpty', e.path))
179
200
        except errors.ShortReadvError, e:
180
 
            return SmartServerResponse(('ShortReadvError',
 
201
            return FailedSmartServerResponse(('ShortReadvError',
181
202
                e.path, str(e.offset), str(e.length), str(e.actual)))
182
203
        except UnicodeError, e:
183
204
            # If it is a DecodeError, than most likely we are starting
190
211
            else:
191
212
                val = 's:' + str_or_unicode.encode('base64')
192
213
            # This handles UnicodeEncodeError or UnicodeDecodeError
193
 
            return SmartServerResponse((e.__class__.__name__,
 
214
            return FailedSmartServerResponse((e.__class__.__name__,
194
215
                    e.encoding, val, str(e.start), str(e.end), e.reason))
195
216
        except errors.TransportNotPossible, e:
196
217
            if e.msg == "readonly transport":
197
 
                return SmartServerResponse(('ReadOnlyError', ))
 
218
                return FailedSmartServerResponse(('ReadOnlyError', ))
198
219
            else:
199
220
                raise
200
221
 
201
222
 
202
223
class HelloRequest(SmartServerRequest):
203
 
    """Answer a version request with my version."""
 
224
    """Answer a version request with the highest protocol version this server
 
225
    supports.
 
226
    """
204
227
 
205
228
    def do(self):
206
 
        return SmartServerResponse(('ok', '1'))
 
229
        return SuccessfulSmartServerResponse(('ok', '2'))
207
230
 
208
231
 
209
232
class GetBundleRequest(SmartServerRequest):
218
241
        base_revision = revision.NULL_REVISION
219
242
        write_bundle(repo, revision_id, base_revision, tmpf)
220
243
        tmpf.seek(0)
221
 
        return SmartServerResponse((), tmpf.read())
222
 
 
223
 
 
224
 
# This exists solely to help RemoteObjectHacking.  It should be removed
225
 
# eventually.  It should not be considered part of the real smart server
226
 
# protocol!
227
 
class ProbeDontUseRequest(SmartServerRequest):
228
 
 
229
 
    def do(self, path):
230
 
        from bzrlib.bzrdir import BzrDirFormat
231
 
        t = self._backing_transport.clone(path)
232
 
        default_format = BzrDirFormat.get_default_format()
233
 
        real_bzrdir = default_format.open(t, _found=True)
234
 
        try:
235
 
            real_bzrdir._format.probe_transport(t)
236
 
        except (errors.NotBranchError, errors.UnknownFormatError):
237
 
            answer = 'no'
238
 
        else:
239
 
            answer = 'yes'
240
 
        return SmartServerResponse((answer,))
 
244
        return SuccessfulSmartServerResponse((), tmpf.read())
241
245
 
242
246
 
243
247
class SmartServerIsReadonly(SmartServerRequest):
248
252
            answer = 'yes'
249
253
        else:
250
254
            answer = 'no'
251
 
        return SmartServerResponse((answer,))
 
255
        return SuccessfulSmartServerResponse((answer,))
252
256
 
253
257
 
254
258
request_handlers = registry.Registry()
321
325
request_handlers.register_lazy(
322
326
    'Transport.is_readonly', 'bzrlib.smart.request', 'SmartServerIsReadonly')
323
327
request_handlers.register_lazy(
324
 
    'probe_dont_use', 'bzrlib.smart.request', 'ProbeDontUseRequest')
 
328
    'BzrDir.open', 'bzrlib.smart.bzrdir', 'SmartServerRequestOpenBzrDir')