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

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

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