/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/vfs.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
protocol, as implemented in bzr 0.11 and later.
25
25
"""
26
26
 
27
 
from __future__ import absolute_import
28
 
 
29
27
import os
30
28
 
31
 
from ... import urlutils
32
 
from . import request
 
29
from bzrlib import errors
 
30
from bzrlib import urlutils
 
31
from bzrlib.smart import request
33
32
 
34
33
 
35
34
def _deserialise_optional_mode(mode):
36
35
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
37
36
    # might serialise modes differently.
38
 
    if mode == b'':
 
37
    if mode == '':
39
38
        return None
40
39
    else:
41
40
        return int(mode)
44
43
def vfs_enabled():
45
44
    """Is the VFS enabled ?
46
45
 
47
 
    the VFS is disabled when the BRZ_NO_SMART_VFS environment variable is set.
 
46
    the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
48
47
 
49
 
    :return: ``True`` if it is enabled.
 
48
    :return: True if it is enabled.
50
49
    """
51
 
    return 'BRZ_NO_SMART_VFS' not in os.environ
 
50
    return not 'BZR_NO_SMART_VFS' in os.environ
52
51
 
53
52
 
54
53
class VfsRequest(request.SmartServerRequest):
59
58
 
60
59
    def _check_enabled(self):
61
60
        if not vfs_enabled():
62
 
            raise request.DisabledMethod(self.__class__.__name__)
 
61
            raise errors.DisabledMethod(self.__class__.__name__)
63
62
 
64
63
    def translate_client_path(self, relpath):
65
64
        # VFS requests are made with escaped paths so the escaping done in
74
73
 
75
74
    def do(self, relpath):
76
75
        relpath = self.translate_client_path(relpath)
77
 
        r = self._backing_transport.has(relpath) and b'yes' or b'no'
 
76
        r = self._backing_transport.has(relpath) and 'yes' or 'no'
78
77
        return request.SuccessfulSmartServerResponse((r,))
79
78
 
80
79
 
83
82
    def do(self, relpath):
84
83
        relpath = self.translate_client_path(relpath)
85
84
        backing_bytes = self._backing_transport.get_bytes(relpath)
86
 
        return request.SuccessfulSmartServerResponse((b'ok',), backing_bytes)
 
85
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
87
86
 
88
87
 
89
88
class AppendRequest(VfsRequest):
96
95
    def do_body(self, body_bytes):
97
96
        old_length = self._backing_transport.append_bytes(
98
97
            self._relpath, body_bytes, self._mode)
99
 
        return request.SuccessfulSmartServerResponse((b'appended', str(old_length).encode('ascii')))
 
98
        return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
100
99
 
101
100
 
102
101
class DeleteRequest(VfsRequest):
104
103
    def do(self, relpath):
105
104
        relpath = self.translate_client_path(relpath)
106
105
        self._backing_transport.delete(relpath)
107
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
106
        return request.SuccessfulSmartServerResponse(('ok', ))
108
107
 
109
108
 
110
109
class IterFilesRecursiveRequest(VfsRequest):
111
110
 
112
111
    def do(self, relpath):
113
 
        if not relpath.endswith(b'/'):
114
 
            relpath += b'/'
 
112
        if not relpath.endswith('/'):
 
113
            relpath += '/'
115
114
        relpath = self.translate_client_path(relpath)
116
115
        transport = self._backing_transport.clone(relpath)
117
116
        filenames = transport.iter_files_recursive()
118
 
        return request.SuccessfulSmartServerResponse((b'names',) + tuple(filenames))
 
117
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
119
118
 
120
119
 
121
120
class ListDirRequest(VfsRequest):
122
121
 
123
122
    def do(self, relpath):
124
 
        if not relpath.endswith(b'/'):
125
 
            relpath += b'/'
 
123
        if not relpath.endswith('/'):
 
124
            relpath += '/'
126
125
        relpath = self.translate_client_path(relpath)
127
126
        filenames = self._backing_transport.list_dir(relpath)
128
 
        return request.SuccessfulSmartServerResponse((b'names',) + tuple([filename.encode('utf-8') for filename in filenames]))
 
127
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
129
128
 
130
129
 
131
130
class MkdirRequest(VfsRequest):
134
133
        relpath = self.translate_client_path(relpath)
135
134
        self._backing_transport.mkdir(relpath,
136
135
                                      _deserialise_optional_mode(mode))
137
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
136
        return request.SuccessfulSmartServerResponse(('ok',))
138
137
 
139
138
 
140
139
class MoveRequest(VfsRequest):
143
142
        rel_from = self.translate_client_path(rel_from)
144
143
        rel_to = self.translate_client_path(rel_to)
145
144
        self._backing_transport.move(rel_from, rel_to)
146
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
145
        return request.SuccessfulSmartServerResponse(('ok',))
147
146
 
148
147
 
149
148
class PutRequest(VfsRequest):
154
153
        self._mode = _deserialise_optional_mode(mode)
155
154
 
156
155
    def do_body(self, body_bytes):
157
 
        self._backing_transport.put_bytes(
158
 
            self._relpath, body_bytes, self._mode)
159
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
156
        self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
 
157
        return request.SuccessfulSmartServerResponse(('ok',))
160
158
 
161
159
 
162
160
class PutNonAtomicRequest(VfsRequest):
167
165
        self._dir_mode = _deserialise_optional_mode(dir_mode)
168
166
        self._mode = _deserialise_optional_mode(mode)
169
167
        # a boolean would be nicer XXX
170
 
        self._create_parent = (create_parent == b'T')
 
168
        self._create_parent = (create_parent == 'T')
171
169
 
172
170
    def do_body(self, body_bytes):
173
171
        self._backing_transport.put_bytes_non_atomic(self._relpath,
174
 
                                                     body_bytes,
175
 
                                                     mode=self._mode,
176
 
                                                     create_parent_dir=self._create_parent,
177
 
                                                     dir_mode=self._dir_mode)
178
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
172
                body_bytes,
 
173
                mode=self._mode,
 
174
                create_parent_dir=self._create_parent,
 
175
                dir_mode=self._dir_mode)
 
176
        return request.SuccessfulSmartServerResponse(('ok',))
179
177
 
180
178
 
181
179
class ReadvRequest(VfsRequest):
187
185
    def do_body(self, body_bytes):
188
186
        """accept offsets for a readv request."""
189
187
        offsets = self._deserialise_offsets(body_bytes)
190
 
        backing_bytes = b''.join(bytes for offset, bytes in
191
 
                                 self._backing_transport.readv(self._relpath, offsets))
192
 
        return request.SuccessfulSmartServerResponse((b'readv',), backing_bytes)
 
188
        backing_bytes = ''.join(bytes for offset, bytes in
 
189
            self._backing_transport.readv(self._relpath, offsets))
 
190
        return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
193
191
 
194
192
    def _deserialise_offsets(self, text):
195
193
        # XXX: FIXME this should be on the protocol object.
196
194
        offsets = []
197
 
        for line in text.split(b'\n'):
 
195
        for line in text.split('\n'):
198
196
            if not line:
199
197
                continue
200
 
            start, length = line.split(b',')
 
198
            start, length = line.split(',')
201
199
            offsets.append((int(start), int(length)))
202
200
        return offsets
203
201
 
208
206
        rel_from = self.translate_client_path(rel_from)
209
207
        rel_to = self.translate_client_path(rel_to)
210
208
        self._backing_transport.rename(rel_from, rel_to)
211
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
209
        return request.SuccessfulSmartServerResponse(('ok', ))
212
210
 
213
211
 
214
212
class RmdirRequest(VfsRequest):
216
214
    def do(self, relpath):
217
215
        relpath = self.translate_client_path(relpath)
218
216
        self._backing_transport.rmdir(relpath)
219
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
217
        return request.SuccessfulSmartServerResponse(('ok', ))
220
218
 
221
219
 
222
220
class StatRequest(VfsRequest):
223
221
 
224
222
    def do(self, relpath):
225
 
        if not relpath.endswith(b'/'):
226
 
            relpath += b'/'
 
223
        if not relpath.endswith('/'):
 
224
            relpath += '/'
227
225
        relpath = self.translate_client_path(relpath)
228
226
        stat = self._backing_transport.stat(relpath)
229
227
        return request.SuccessfulSmartServerResponse(
230
 
            (b'stat', str(stat.st_size).encode('ascii'), oct(stat.st_mode).encode('ascii')))
 
228
            ('stat', str(stat.st_size), oct(stat.st_mode)))
 
229