1
# Copyright (C) 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
Views are contained within a working tree and normally constructed
20
when first accessed. Clients should do, for example, ...
22
tree.views.lookup_view()
25
from __future__ import absolute_import
35
_VIEWS_FORMAT_MARKER_RE = re.compile(r'Bazaar views format (\d+)')
36
_VIEWS_FORMAT1_MARKER = "Bazaar views format 1\n"
40
"""Base class for View managers."""
42
def supports_views(self):
43
raise NotImplementedError(self.supports_views)
46
class PathBasedViews(_Views):
47
"""View storage in an unversioned tree control file.
49
Views are stored in terms of paths relative to the tree root.
51
The top line of the control file is a format marker in the format:
55
where X is an integer number. After this top line, version 1 format is
58
* optional name-values pairs in the format 'name=value'
60
* optional view definitions, one per line in the format
66
where the fields are separated by a nul character (\0). The views file
67
is encoded in utf-8. The only supported keyword in version 1 is
68
'current' which stores the name of the current view, if any.
71
def __init__(self, tree):
77
def supports_views(self):
80
def get_view_info(self):
81
"""Get the current view and dictionary of views.
83
:return: current, views where
84
current = the name of the current view or None if no view is enabled
85
views = a map from view name to list of files/directories
87
self._load_view_info()
88
return self._current, self._views
90
def set_view_info(self, current, views):
91
"""Set the current view and dictionary of views.
93
:param current: the name of the current view or None if no view is
95
:param views: a map from view name to list of files/directories
97
if current is not None and current not in views:
98
raise errors.NoSuchView(current)
99
self.tree.lock_write()
101
self._current = current
103
self._save_view_info()
107
def lookup_view(self, view_name=None):
108
"""Return the contents of a view.
110
:param view_Name: name of the view or None to lookup the current view
111
:return: the list of files/directories in the requested view
113
self._load_view_info()
115
if view_name is None:
117
view_name = self._current
120
return self._views[view_name]
122
raise errors.NoSuchView(view_name)
124
def set_view(self, view_name, view_files, make_current=True):
125
"""Add or update a view definition.
127
:param view_name: the name of the view
128
:param view_files: the list of files/directories in the view
129
:param make_current: make this view the current one or not
131
self.tree.lock_write()
133
self._load_view_info()
134
self._views[view_name] = view_files
136
self._current = view_name
137
self._save_view_info()
141
def delete_view(self, view_name):
142
"""Delete a view definition.
144
If the view deleted is the current one, the current view is reset.
146
self.tree.lock_write()
148
self._load_view_info()
150
del self._views[view_name]
152
raise errors.NoSuchView(view_name)
153
if view_name == self._current:
155
self._save_view_info()
159
def _save_view_info(self):
160
"""Save the current view and all view definitions.
162
Be sure to have initialised self._current and self._views before
165
self.tree.lock_write()
167
if self._current is None:
170
keywords = {'current': self._current}
171
self.tree._transport.put_bytes('views',
172
self._serialize_view_content(keywords, self._views))
176
def _load_view_info(self):
177
"""Load the current view and dictionary of view definitions."""
179
self.tree.lock_read()
182
view_content = self.tree._transport.get_bytes('views')
183
except errors.NoSuchFile, e:
184
self._current, self._views = None, {}
186
keywords, self._views = \
187
self._deserialize_view_content(view_content)
188
self._current = keywords.get('current')
193
def _serialize_view_content(self, keywords, view_dict):
194
"""Convert view keywords and a view dictionary into a stream."""
195
lines = [_VIEWS_FORMAT1_MARKER]
197
line = "%s=%s\n" % (key,keywords[key])
198
lines.append(line.encode('utf-8'))
200
lines.append("views:\n".encode('utf-8'))
201
for view in sorted(view_dict):
202
view_data = "%s\0%s\n" % (view, "\0".join(view_dict[view]))
203
lines.append(view_data.encode('utf-8'))
204
return "".join(lines)
206
def _deserialize_view_content(self, view_content):
207
"""Convert a stream into view keywords and a dictionary of views."""
208
# as a special case to make initialization easy, an empty definition
209
# maps to no current view and an empty view dictionary
210
if view_content == '':
212
lines = view_content.splitlines()
213
match = _VIEWS_FORMAT_MARKER_RE.match(lines[0])
216
"format marker missing from top of views file")
217
elif match.group(1) != '1':
219
"cannot decode views format %s" % match.group(1))
224
for line in lines[1:]:
225
text = line.decode('utf-8')
227
parts = text.split('\0')
230
elif text == 'views:':
233
elif text.find('=') >= 0:
234
# must be a name-value pair
235
keyword, value = text.split('=', 1)
236
keywords[keyword] = value
238
raise ValueError("failed to deserialize views line %s",
240
return keywords, views
241
except ValueError, e:
242
raise ValueError("failed to deserialize views content %r: %s"
246
class DisabledViews(_Views):
247
"""View storage that refuses to store anything.
249
This is used by older formats that can't store views.
252
def __init__(self, tree):
255
def supports_views(self):
258
def _not_supported(self, *a, **k):
259
raise errors.ViewsNotSupported(self.tree)
261
get_view_info = _not_supported
262
set_view_info = _not_supported
263
lookup_view = _not_supported
264
set_view = _not_supported
265
delete_view = _not_supported
268
def view_display_str(view_files, encoding=None):
269
"""Get the display string for a list of view files.
271
:param view_files: the list of file names
272
:param encoding: the encoding to display the files in
275
return ", ".join(view_files)
277
return ", ".join([v.encode(encoding, 'replace') for v in view_files])
280
def check_path_in_view(tree, relpath):
281
"""If a working tree has a view enabled, check the path is within it."""
282
if tree.supports_views():
283
view_files = tree.views.lookup_view()
284
if view_files and not osutils.is_inside_any(view_files, relpath):
285
raise errors.FileOutsideView(relpath, view_files)