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

  • Committer: Jelmer Vernooij
  • Date: 2020-02-05 01:40:59 UTC
  • mto: This revision was merged to the branch mainline in revision 7480.
  • Revision ID: jelmer@jelmer.uk-20200205014059-1jrhjaphw5vh9i7s
Fix Python 2.7 build.

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 bzrlib import errors
30
 
from bzrlib import urlutils
31
 
from bzrlib.smart import request
 
31
from ... import urlutils
 
32
from . import request
32
33
 
33
34
 
34
35
def _deserialise_optional_mode(mode):
35
36
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
36
37
    # might serialise modes differently.
37
 
    if mode == '':
 
38
    if mode == b'':
38
39
        return None
39
40
    else:
40
41
        return int(mode)
43
44
def vfs_enabled():
44
45
    """Is the VFS enabled ?
45
46
 
46
 
    the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
 
47
    the VFS is disabled when the BRZ_NO_SMART_VFS environment variable is set.
47
48
 
48
 
    :return: True if it is enabled.
 
49
    :return: ``True`` if it is enabled.
49
50
    """
50
 
    return not 'BZR_NO_SMART_VFS' in os.environ
 
51
    return 'BRZ_NO_SMART_VFS' not in os.environ
51
52
 
52
53
 
53
54
class VfsRequest(request.SmartServerRequest):
58
59
 
59
60
    def _check_enabled(self):
60
61
        if not vfs_enabled():
61
 
            raise errors.DisabledMethod(self.__class__.__name__)
 
62
            raise request.DisabledMethod(self.__class__.__name__)
62
63
 
63
64
    def translate_client_path(self, relpath):
64
65
        # VFS requests are made with escaped paths so the escaping done in
73
74
 
74
75
    def do(self, relpath):
75
76
        relpath = self.translate_client_path(relpath)
76
 
        r = self._backing_transport.has(relpath) and 'yes' or 'no'
 
77
        r = self._backing_transport.has(relpath) and b'yes' or b'no'
77
78
        return request.SuccessfulSmartServerResponse((r,))
78
79
 
79
80
 
82
83
    def do(self, relpath):
83
84
        relpath = self.translate_client_path(relpath)
84
85
        backing_bytes = self._backing_transport.get_bytes(relpath)
85
 
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
 
86
        return request.SuccessfulSmartServerResponse((b'ok',), backing_bytes)
86
87
 
87
88
 
88
89
class AppendRequest(VfsRequest):
95
96
    def do_body(self, body_bytes):
96
97
        old_length = self._backing_transport.append_bytes(
97
98
            self._relpath, body_bytes, self._mode)
98
 
        return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
 
99
        return request.SuccessfulSmartServerResponse((b'appended', str(old_length).encode('ascii')))
99
100
 
100
101
 
101
102
class DeleteRequest(VfsRequest):
103
104
    def do(self, relpath):
104
105
        relpath = self.translate_client_path(relpath)
105
106
        self._backing_transport.delete(relpath)
106
 
        return request.SuccessfulSmartServerResponse(('ok', ))
 
107
        return request.SuccessfulSmartServerResponse((b'ok', ))
107
108
 
108
109
 
109
110
class IterFilesRecursiveRequest(VfsRequest):
110
111
 
111
112
    def do(self, relpath):
112
 
        if not relpath.endswith('/'):
113
 
            relpath += '/'
 
113
        if not relpath.endswith(b'/'):
 
114
            relpath += b'/'
114
115
        relpath = self.translate_client_path(relpath)
115
116
        transport = self._backing_transport.clone(relpath)
116
117
        filenames = transport.iter_files_recursive()
117
 
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
 
118
        return request.SuccessfulSmartServerResponse((b'names',) + tuple(filenames))
118
119
 
119
120
 
120
121
class ListDirRequest(VfsRequest):
121
122
 
122
123
    def do(self, relpath):
123
 
        if not relpath.endswith('/'):
124
 
            relpath += '/'
 
124
        if not relpath.endswith(b'/'):
 
125
            relpath += b'/'
125
126
        relpath = self.translate_client_path(relpath)
126
127
        filenames = self._backing_transport.list_dir(relpath)
127
 
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
 
128
        return request.SuccessfulSmartServerResponse((b'names',) + tuple([filename.encode('utf-8') for filename in filenames]))
128
129
 
129
130
 
130
131
class MkdirRequest(VfsRequest):
133
134
        relpath = self.translate_client_path(relpath)
134
135
        self._backing_transport.mkdir(relpath,
135
136
                                      _deserialise_optional_mode(mode))
136
 
        return request.SuccessfulSmartServerResponse(('ok',))
 
137
        return request.SuccessfulSmartServerResponse((b'ok',))
137
138
 
138
139
 
139
140
class MoveRequest(VfsRequest):
142
143
        rel_from = self.translate_client_path(rel_from)
143
144
        rel_to = self.translate_client_path(rel_to)
144
145
        self._backing_transport.move(rel_from, rel_to)
145
 
        return request.SuccessfulSmartServerResponse(('ok',))
 
146
        return request.SuccessfulSmartServerResponse((b'ok',))
146
147
 
147
148
 
148
149
class PutRequest(VfsRequest):
153
154
        self._mode = _deserialise_optional_mode(mode)
154
155
 
155
156
    def do_body(self, body_bytes):
156
 
        self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
157
 
        return request.SuccessfulSmartServerResponse(('ok',))
 
157
        self._backing_transport.put_bytes(
 
158
            self._relpath, body_bytes, self._mode)
 
159
        return request.SuccessfulSmartServerResponse((b'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 == 'T')
 
170
        self._create_parent = (create_parent == b'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(('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((b'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 = ''.join(bytes for offset, bytes in
189
 
            self._backing_transport.readv(self._relpath, offsets))
190
 
        return request.SuccessfulSmartServerResponse(('readv',), backing_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)
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('\n'):
 
197
        for line in text.split(b'\n'):
196
198
            if not line:
197
199
                continue
198
 
            start, length = line.split(',')
 
200
            start, length = line.split(b',')
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(('ok', ))
 
211
        return request.SuccessfulSmartServerResponse((b'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(('ok', ))
 
219
        return request.SuccessfulSmartServerResponse((b'ok', ))
218
220
 
219
221
 
220
222
class StatRequest(VfsRequest):
221
223
 
222
224
    def do(self, relpath):
223
 
        if not relpath.endswith('/'):
224
 
            relpath += '/'
 
225
        if not relpath.endswith(b'/'):
 
226
            relpath += b'/'
225
227
        relpath = self.translate_client_path(relpath)
226
228
        stat = self._backing_transport.stat(relpath)
227
229
        return request.SuccessfulSmartServerResponse(
228
 
            ('stat', str(stat.st_size), oct(stat.st_mode)))
229
 
 
 
230
            (b'stat', str(stat.st_size).encode('ascii'), oct(stat.st_mode).encode('ascii')))