1
# Copyright (C) 2009, 2010 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
17
"""A transport decorator that filters all paths that are passed to it."""
20
from bzrlib.transport import (
29
class PathFilteringServer(Server):
30
"""Transport server for PathFilteringTransport.
32
It holds the backing_transport and filter_func for PathFilteringTransports.
33
All paths will be passed through filter_func before calling into the
36
Note that paths returned from the backing transport are *not* altered in
37
anyway. So, depending on the filter_func, PathFilteringTransports might
38
not conform to the usual expectations of Transport behaviour; e.g. 'name'
39
in t.list_dir('dir') might not imply t.has('dir/name') is True! A filter
40
that merely prefixes a constant path segment will be essentially
41
transparent, whereas a filter that does rot13 to paths will break
42
expectations and probably cause confusing errors. So choose your
43
filter_func with care.
46
def __init__(self, backing_transport, filter_func):
49
:param backing_transport: a transport
50
:param filter_func: a callable that takes paths, and translates them
51
into paths for use with the backing transport.
53
self.backing_transport = backing_transport
54
self.filter_func = filter_func
56
def _factory(self, url):
57
return PathFilteringTransport(self, url)
62
def start_server(self):
63
self.scheme = 'filtered-%d:///' % id(self)
64
register_transport(self.scheme, self._factory)
66
def stop_server(self):
67
unregister_transport(self.scheme, self._factory)
70
class PathFilteringTransport(Transport):
71
"""A PathFilteringTransport.
73
Please see PathFilteringServer for details.
76
def __init__(self, server, base):
78
if not base.endswith('/'):
80
Transport.__init__(self, base)
81
self.base_path = self.base[len(self.server.scheme)-1:]
82
self.scheme = self.server.scheme
84
def _relpath_from_server_root(self, relpath):
85
unfiltered_path = self._combine_paths(self.base_path, relpath)
86
if not unfiltered_path.startswith('/'):
87
raise ValueError(unfiltered_path)
88
return unfiltered_path[1:]
90
def _filter(self, relpath):
91
return self.server.filter_func(self._relpath_from_server_root(relpath))
93
def _call(self, methodname, relpath, *args):
94
"""Helper for Transport methods of the form:
95
operation(path, [other args ...])
97
backing_method = getattr(self.server.backing_transport, methodname)
98
return backing_method(self._filter(relpath), *args)
101
def abspath(self, relpath):
102
# We do *not* want to filter at this point; e.g if the filter is
103
# homedir expansion, self.base == 'this:///' and relpath == '~/foo',
104
# then the abspath should be this:///~/foo (not this:///home/user/foo).
105
# Instead filtering should happen when self's base is passed to the
107
return self.scheme + self._relpath_from_server_root(relpath)
109
def append_file(self, relpath, f, mode=None):
110
return self._call('append_file', relpath, f, mode)
112
def _can_roundtrip_unix_modebits(self):
113
return self.server.backing_transport._can_roundtrip_unix_modebits()
115
def clone(self, relpath):
116
return self.__class__(self.server, self.abspath(relpath))
118
def delete(self, relpath):
119
return self._call('delete', relpath)
121
def delete_tree(self, relpath):
122
return self._call('delete_tree', relpath)
124
def external_url(self):
125
"""See bzrlib.transport.Transport.external_url."""
126
# PathFilteringTransports, like MemoryTransport, depend on in-process
127
# state and thus the base cannot simply be handed out. See the base
128
# class docstring for more details and possible directions. For now we
129
# return the path-filtered URL.
130
return self.server.backing_transport.external_url()
132
def get(self, relpath):
133
return self._call('get', relpath)
135
def has(self, relpath):
136
return self._call('has', relpath)
138
def is_readonly(self):
139
return self.server.backing_transport.is_readonly()
141
def iter_files_recursive(self):
142
backing_transport = self.server.backing_transport.clone(
144
return backing_transport.iter_files_recursive()
147
return self.server.backing_transport.listable()
149
def list_dir(self, relpath):
150
return self._call('list_dir', relpath)
152
def lock_read(self, relpath):
153
return self._call('lock_read', relpath)
155
def lock_write(self, relpath):
156
return self._call('lock_write', relpath)
158
def mkdir(self, relpath, mode=None):
159
return self._call('mkdir', relpath, mode)
161
def open_write_stream(self, relpath, mode=None):
162
return self._call('open_write_stream', relpath, mode)
164
def put_file(self, relpath, f, mode=None):
165
return self._call('put_file', relpath, f, mode)
167
def rename(self, rel_from, rel_to):
168
return self._call('rename', rel_from, self._filter(rel_to))
170
def rmdir(self, relpath):
171
return self._call('rmdir', relpath)
173
def stat(self, relpath):
174
return self._call('stat', relpath)
177
def get_test_permutations():
178
"""Return the permutations to be used in testing."""
179
from bzrlib.tests import test_server
180
return [(PathFilteringTransport, test_server.TestingPathFilteringServer)]