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."""
19
from __future__ import absolute_import
21
from bzrlib import urlutils
23
from bzrlib.transport import (
31
class PathFilteringServer(Server):
32
"""Transport server for PathFilteringTransport.
34
It holds the backing_transport and filter_func for PathFilteringTransports.
35
All paths will be passed through filter_func before calling into the
38
Note that paths returned from the backing transport are *not* altered in
39
anyway. So, depending on the filter_func, PathFilteringTransports might
40
not conform to the usual expectations of Transport behaviour; e.g. 'name'
41
in t.list_dir('dir') might not imply t.has('dir/name') is True! A filter
42
that merely prefixes a constant path segment will be essentially
43
transparent, whereas a filter that does rot13 to paths will break
44
expectations and probably cause confusing errors. So choose your
45
filter_func with care.
48
def __init__(self, backing_transport, filter_func):
51
:param backing_transport: a transport
52
:param filter_func: a callable that takes paths, and translates them
53
into paths for use with the backing transport.
55
self.backing_transport = backing_transport
56
self.filter_func = filter_func
58
def _factory(self, url):
59
return PathFilteringTransport(self, url)
64
def start_server(self):
65
self.scheme = 'filtered-%d:///' % id(self)
66
register_transport(self.scheme, self._factory)
68
def stop_server(self):
69
unregister_transport(self.scheme, self._factory)
72
class PathFilteringTransport(Transport):
73
"""A PathFilteringTransport.
75
Please see PathFilteringServer for details.
78
def __init__(self, server, base):
80
if not base.endswith('/'):
82
Transport.__init__(self, base)
83
self.base_path = self.base[len(self.server.scheme)-1:]
84
self.scheme = self.server.scheme
86
def _relpath_from_server_root(self, relpath):
87
unfiltered_path = urlutils.URL._combine_paths(self.base_path, relpath)
88
if not unfiltered_path.startswith('/'):
89
raise ValueError(unfiltered_path)
90
return unfiltered_path[1:]
92
def _filter(self, relpath):
93
return self.server.filter_func(self._relpath_from_server_root(relpath))
95
def _call(self, methodname, relpath, *args):
96
"""Helper for Transport methods of the form:
97
operation(path, [other args ...])
99
backing_method = getattr(self.server.backing_transport, methodname)
100
return backing_method(self._filter(relpath), *args)
103
def abspath(self, relpath):
104
# We do *not* want to filter at this point; e.g if the filter is
105
# homedir expansion, self.base == 'this:///' and relpath == '~/foo',
106
# then the abspath should be this:///~/foo (not this:///home/user/foo).
107
# Instead filtering should happen when self's base is passed to the
109
return self.scheme + self._relpath_from_server_root(relpath)
111
def append_file(self, relpath, f, mode=None):
112
return self._call('append_file', relpath, f, mode)
114
def _can_roundtrip_unix_modebits(self):
115
return self.server.backing_transport._can_roundtrip_unix_modebits()
117
def clone(self, relpath):
118
return self.__class__(self.server, self.abspath(relpath))
120
def delete(self, relpath):
121
return self._call('delete', relpath)
123
def delete_tree(self, relpath):
124
return self._call('delete_tree', relpath)
126
def external_url(self):
127
"""See bzrlib.transport.Transport.external_url."""
128
# PathFilteringTransports, like MemoryTransport, depend on in-process
129
# state and thus the base cannot simply be handed out. See the base
130
# class docstring for more details and possible directions. For now we
131
# return the path-filtered URL.
132
return self.server.backing_transport.external_url()
134
def get(self, relpath):
135
return self._call('get', relpath)
137
def has(self, relpath):
138
return self._call('has', relpath)
140
def is_readonly(self):
141
return self.server.backing_transport.is_readonly()
143
def iter_files_recursive(self):
144
backing_transport = self.server.backing_transport.clone(
146
return backing_transport.iter_files_recursive()
149
return self.server.backing_transport.listable()
151
def list_dir(self, relpath):
152
return self._call('list_dir', relpath)
154
def lock_read(self, relpath):
155
return self._call('lock_read', relpath)
157
def lock_write(self, relpath):
158
return self._call('lock_write', relpath)
160
def mkdir(self, relpath, mode=None):
161
return self._call('mkdir', relpath, mode)
163
def open_write_stream(self, relpath, mode=None):
164
return self._call('open_write_stream', relpath, mode)
166
def put_file(self, relpath, f, mode=None):
167
return self._call('put_file', relpath, f, mode)
169
def rename(self, rel_from, rel_to):
170
return self._call('rename', rel_from, self._filter(rel_to))
172
def rmdir(self, relpath):
173
return self._call('rmdir', relpath)
175
def stat(self, relpath):
176
return self._call('stat', relpath)
179
def get_test_permutations():
180
"""Return the permutations to be used in testing."""
181
from bzrlib.tests import test_server
182
return [(PathFilteringTransport, test_server.TestingPathFilteringServer)]