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

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
  tree.views.lookup_view()
23
23
"""
24
24
 
 
25
from __future__ import absolute_import
 
26
 
25
27
import re
26
28
 
27
29
from . import (
30
32
    )
31
33
 
32
34
 
33
 
_VIEWS_FORMAT_MARKER_RE = re.compile(b'Bazaar views format (\\d+)')
34
 
_VIEWS_FORMAT1_MARKER = b"Bazaar views format 1\n"
 
35
_VIEWS_FORMAT_MARKER_RE = re.compile(r'Bazaar views format (\d+)')
 
36
_VIEWS_FORMAT1_MARKER = "Bazaar views format 1\n"
35
37
 
36
38
 
37
39
class NoSuchView(errors.BzrError):
125
127
        """
126
128
        if current is not None and current not in views:
127
129
            raise NoSuchView(current)
128
 
        with self.tree.lock_write():
 
130
        self.tree.lock_write()
 
131
        try:
129
132
            self._current = current
130
133
            self._views = views
131
134
            self._save_view_info()
 
135
        finally:
 
136
            self.tree.unlock()
132
137
 
133
138
    def lookup_view(self, view_name=None):
134
139
        """Return the contents of a view.
154
159
        :param view_files: the list of files/directories in the view
155
160
        :param make_current: make this view the current one or not
156
161
        """
157
 
        with self.tree.lock_write():
 
162
        self.tree.lock_write()
 
163
        try:
158
164
            self._load_view_info()
159
165
            self._views[view_name] = view_files
160
166
            if make_current:
161
167
                self._current = view_name
162
168
            self._save_view_info()
 
169
        finally:
 
170
            self.tree.unlock()
163
171
 
164
172
    def delete_view(self, view_name):
165
173
        """Delete a view definition.
166
174
 
167
175
        If the view deleted is the current one, the current view is reset.
168
176
        """
169
 
        with self.tree.lock_write():
 
177
        self.tree.lock_write()
 
178
        try:
170
179
            self._load_view_info()
171
180
            try:
172
181
                del self._views[view_name]
175
184
            if view_name == self._current:
176
185
                self._current = None
177
186
            self._save_view_info()
 
187
        finally:
 
188
            self.tree.unlock()
178
189
 
179
190
    def _save_view_info(self):
180
191
        """Save the current view and all view definitions.
182
193
        Be sure to have initialised self._current and self._views before
183
194
        calling this method.
184
195
        """
185
 
        with self.tree.lock_write():
 
196
        self.tree.lock_write()
 
197
        try:
186
198
            if self._current is None:
187
199
                keywords = {}
188
200
            else:
189
201
                keywords = {'current': self._current}
190
 
            self.tree._transport.put_bytes(
191
 
                'views', self._serialize_view_content(keywords, self._views))
 
202
            self.tree._transport.put_bytes('views',
 
203
                self._serialize_view_content(keywords, self._views))
 
204
        finally:
 
205
            self.tree.unlock()
192
206
 
193
207
    def _load_view_info(self):
194
208
        """Load the current view and dictionary of view definitions."""
196
210
            with self.tree.lock_read():
197
211
                try:
198
212
                    view_content = self.tree._transport.get_bytes('views')
199
 
                except errors.NoSuchFile:
 
213
                except errors.NoSuchFile as e:
200
214
                    self._current, self._views = None, {}
201
215
                else:
202
216
                    keywords, self._views = \
215
229
            for view in sorted(view_dict):
216
230
                view_data = "%s\0%s\n" % (view, "\0".join(view_dict[view]))
217
231
                lines.append(view_data.encode('utf-8'))
218
 
        return b"".join(lines)
 
232
        return "".join(lines)
219
233
 
220
234
    def _deserialize_view_content(self, view_content):
221
235
        """Convert a stream into view keywords and a dictionary of views."""
222
236
        # as a special case to make initialization easy, an empty definition
223
237
        # maps to no current view and an empty view dictionary
224
 
        if view_content == b'':
 
238
        if view_content == '':
225
239
            return {}, {}
226
240
        lines = view_content.splitlines()
227
241
        match = _VIEWS_FORMAT_MARKER_RE.match(lines[0])
228
242
        if not match:
229
243
            raise ValueError(
230
244
                "format marker missing from top of views file")
231
 
        elif match.group(1) != b'1':
 
245
        elif match.group(1) != '1':
232
246
            raise ValueError(
233
247
                "cannot decode views format %s" % match.group(1))
234
248
        try:
250
264
                    keywords[keyword] = value
251
265
                else:
252
266
                    raise ValueError("failed to deserialize views line %s",
253
 
                                     text)
 
267
                        text)
254
268
            return keywords, views
255
269
        except ValueError as e:
256
270
            raise ValueError("failed to deserialize views content %r: %s"
257
 
                             % (view_content, e))
 
271
                % (view_content, e))
258
272
 
259
273
 
260
274
class DisabledViews(_Views):
295
309
    """If a working tree has a view enabled, check the path is within it."""
296
310
    if tree.supports_views():
297
311
        view_files = tree.views.lookup_view()
298
 
        if view_files and not osutils.is_inside_any(view_files, relpath):
 
312
        if  view_files and not osutils.is_inside_any(view_files, relpath):
299
313
            raise FileOutsideView(relpath, view_files)