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

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

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
 
from . import (
 
29
from bzrlib import (
28
30
    errors,
29
31
    osutils,
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
 
 
36
 
 
37
 
class NoSuchView(errors.BzrError):
38
 
    """A view does not exist.
39
 
    """
40
 
 
41
 
    _fmt = u"No such view: %(view_name)s."
42
 
 
43
 
    def __init__(self, view_name):
44
 
        self.view_name = view_name
45
 
 
46
 
 
47
 
class ViewsNotSupported(errors.BzrError):
48
 
    """Views are not supported by a tree format.
49
 
    """
50
 
 
51
 
    _fmt = ("Views are not supported by %(tree)s;"
52
 
            " use 'brz upgrade' to change your tree to a later format.")
53
 
 
54
 
    def __init__(self, tree):
55
 
        self.tree = tree
56
 
 
57
 
 
58
 
class FileOutsideView(errors.BzrError):
59
 
 
60
 
    _fmt = ('Specified file "%(file_name)s" is outside the current view: '
61
 
            '%(view_str)s')
62
 
 
63
 
    def __init__(self, file_name, view_files):
64
 
        self.file_name = file_name
65
 
        self.view_str = ", ".join(view_files)
 
35
_VIEWS_FORMAT_MARKER_RE = re.compile(r'Bazaar views format (\d+)')
 
36
_VIEWS_FORMAT1_MARKER = "Bazaar views format 1\n"
66
37
 
67
38
 
68
39
class _Views(object):
124
95
        :param views: a map from view name to list of files/directories
125
96
        """
126
97
        if current is not None and current not in views:
127
 
            raise NoSuchView(current)
128
 
        with self.tree.lock_write():
 
98
            raise errors.NoSuchView(current)
 
99
        self.tree.lock_write()
 
100
        try:
129
101
            self._current = current
130
102
            self._views = views
131
103
            self._save_view_info()
 
104
        finally:
 
105
            self.tree.unlock()
132
106
 
133
107
    def lookup_view(self, view_name=None):
134
108
        """Return the contents of a view.
145
119
                    return []
146
120
            return self._views[view_name]
147
121
        except KeyError:
148
 
            raise NoSuchView(view_name)
 
122
            raise errors.NoSuchView(view_name)
149
123
 
150
124
    def set_view(self, view_name, view_files, make_current=True):
151
125
        """Add or update a view definition.
154
128
        :param view_files: the list of files/directories in the view
155
129
        :param make_current: make this view the current one or not
156
130
        """
157
 
        with self.tree.lock_write():
 
131
        self.tree.lock_write()
 
132
        try:
158
133
            self._load_view_info()
159
134
            self._views[view_name] = view_files
160
135
            if make_current:
161
136
                self._current = view_name
162
137
            self._save_view_info()
 
138
        finally:
 
139
            self.tree.unlock()
163
140
 
164
141
    def delete_view(self, view_name):
165
142
        """Delete a view definition.
166
143
 
167
144
        If the view deleted is the current one, the current view is reset.
168
145
        """
169
 
        with self.tree.lock_write():
 
146
        self.tree.lock_write()
 
147
        try:
170
148
            self._load_view_info()
171
149
            try:
172
150
                del self._views[view_name]
173
151
            except KeyError:
174
 
                raise NoSuchView(view_name)
 
152
                raise errors.NoSuchView(view_name)
175
153
            if view_name == self._current:
176
154
                self._current = None
177
155
            self._save_view_info()
 
156
        finally:
 
157
            self.tree.unlock()
178
158
 
179
159
    def _save_view_info(self):
180
160
        """Save the current view and all view definitions.
182
162
        Be sure to have initialised self._current and self._views before
183
163
        calling this method.
184
164
        """
185
 
        with self.tree.lock_write():
 
165
        self.tree.lock_write()
 
166
        try:
186
167
            if self._current is None:
187
168
                keywords = {}
188
169
            else:
189
170
                keywords = {'current': self._current}
190
 
            self.tree._transport.put_bytes(
191
 
                'views', self._serialize_view_content(keywords, self._views))
 
171
            self.tree._transport.put_bytes('views',
 
172
                self._serialize_view_content(keywords, self._views))
 
173
        finally:
 
174
            self.tree.unlock()
192
175
 
193
176
    def _load_view_info(self):
194
177
        """Load the current view and dictionary of view definitions."""
195
178
        if not self._loaded:
196
 
            with self.tree.lock_read():
 
179
            self.tree.lock_read()
 
180
            try:
197
181
                try:
198
182
                    view_content = self.tree._transport.get_bytes('views')
199
 
                except errors.NoSuchFile:
 
183
                except errors.NoSuchFile, e:
200
184
                    self._current, self._views = None, {}
201
185
                else:
202
186
                    keywords, self._views = \
203
187
                        self._deserialize_view_content(view_content)
204
188
                    self._current = keywords.get('current')
 
189
            finally:
 
190
                self.tree.unlock()
205
191
            self._loaded = True
206
192
 
207
193
    def _serialize_view_content(self, keywords, view_dict):
208
194
        """Convert view keywords and a view dictionary into a stream."""
209
195
        lines = [_VIEWS_FORMAT1_MARKER]
210
196
        for key in keywords:
211
 
            line = "%s=%s\n" % (key, keywords[key])
 
197
            line = "%s=%s\n" % (key,keywords[key])
212
198
            lines.append(line.encode('utf-8'))
213
199
        if view_dict:
214
200
            lines.append("views:\n".encode('utf-8'))
215
201
            for view in sorted(view_dict):
216
202
                view_data = "%s\0%s\n" % (view, "\0".join(view_dict[view]))
217
203
                lines.append(view_data.encode('utf-8'))
218
 
        return b"".join(lines)
 
204
        return "".join(lines)
219
205
 
220
206
    def _deserialize_view_content(self, view_content):
221
207
        """Convert a stream into view keywords and a dictionary of views."""
222
208
        # as a special case to make initialization easy, an empty definition
223
209
        # maps to no current view and an empty view dictionary
224
 
        if view_content == b'':
 
210
        if view_content == '':
225
211
            return {}, {}
226
212
        lines = view_content.splitlines()
227
213
        match = _VIEWS_FORMAT_MARKER_RE.match(lines[0])
228
214
        if not match:
229
215
            raise ValueError(
230
216
                "format marker missing from top of views file")
231
 
        elif match.group(1) != b'1':
 
217
        elif match.group(1) != '1':
232
218
            raise ValueError(
233
219
                "cannot decode views format %s" % match.group(1))
234
220
        try:
250
236
                    keywords[keyword] = value
251
237
                else:
252
238
                    raise ValueError("failed to deserialize views line %s",
253
 
                                     text)
 
239
                        text)
254
240
            return keywords, views
255
 
        except ValueError as e:
 
241
        except ValueError, e:
256
242
            raise ValueError("failed to deserialize views content %r: %s"
257
 
                             % (view_content, e))
 
243
                % (view_content, e))
258
244
 
259
245
 
260
246
class DisabledViews(_Views):
270
256
        return False
271
257
 
272
258
    def _not_supported(self, *a, **k):
273
 
        raise ViewsNotSupported(self.tree)
 
259
        raise errors.ViewsNotSupported(self.tree)
274
260
 
275
261
    get_view_info = _not_supported
276
262
    set_view_info = _not_supported
295
281
    """If a working tree has a view enabled, check the path is within it."""
296
282
    if tree.supports_views():
297
283
        view_files = tree.views.lookup_view()
298
 
        if view_files and not osutils.is_inside_any(view_files, relpath):
299
 
            raise FileOutsideView(relpath, view_files)
 
284
        if  view_files and not osutils.is_inside_any(view_files, relpath):
 
285
            raise errors.FileOutsideView(relpath, view_files)