~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/pathfilter.py

  • Committer: Danny van Heumen
  • Date: 2010-03-09 21:42:11 UTC
  • mto: (4634.139.5 2.0)
  • mto: This revision was merged to the branch mainline in revision 5160.
  • Revision ID: danny@dannyvanheumen.nl-20100309214211-iqh42x6qcikgd9p3
Reverted now-useless TODO list.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
16
 
 
17
 
"""A transport decorator that filters all paths that are passed to it."""
18
 
 
19
 
 
20
 
from bzrlib.transport import (
21
 
    get_transport,
22
 
    register_transport,
23
 
    Server,
24
 
    Transport,
25
 
    unregister_transport,
26
 
    )
27
 
 
28
 
 
29
 
class PathFilteringServer(Server):
30
 
    """Transport server for PathFilteringTransport.
31
 
 
32
 
    It holds the backing_transport and filter_func for PathFilteringTransports.
33
 
    All paths will be passed through filter_func before calling into the
34
 
    backing_transport.
35
 
 
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.
44
 
    """
45
 
 
46
 
    def __init__(self, backing_transport, filter_func):
47
 
        """Constructor.
48
 
 
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.
52
 
        """
53
 
        self.backing_transport = backing_transport
54
 
        self.filter_func = filter_func
55
 
 
56
 
    def _factory(self, url):
57
 
        return PathFilteringTransport(self, url)
58
 
 
59
 
    def get_url(self):
60
 
        return self.scheme
61
 
 
62
 
    def start_server(self):
63
 
        self.scheme = 'filtered-%d:///' % id(self)
64
 
        register_transport(self.scheme, self._factory)
65
 
 
66
 
    def stop_server(self):
67
 
        unregister_transport(self.scheme, self._factory)
68
 
 
69
 
 
70
 
class PathFilteringTransport(Transport):
71
 
    """A PathFilteringTransport.
72
 
 
73
 
    Please see PathFilteringServer for details.
74
 
    """
75
 
 
76
 
    def __init__(self, server, base):
77
 
        self.server = server
78
 
        if not base.endswith('/'):
79
 
            base += '/'
80
 
        Transport.__init__(self, base)
81
 
        self.base_path = self.base[len(self.server.scheme)-1:]
82
 
        self.scheme = self.server.scheme
83
 
 
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:]
89
 
 
90
 
    def _filter(self, relpath):
91
 
        return self.server.filter_func(self._relpath_from_server_root(relpath))
92
 
 
93
 
    def _call(self, methodname, relpath, *args):
94
 
        """Helper for Transport methods of the form:
95
 
            operation(path, [other args ...])
96
 
        """
97
 
        backing_method = getattr(self.server.backing_transport, methodname)
98
 
        return backing_method(self._filter(relpath), *args)
99
 
 
100
 
    # Transport methods
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
106
 
        # backing.
107
 
        return self.scheme + self._relpath_from_server_root(relpath)
108
 
 
109
 
    def append_file(self, relpath, f, mode=None):
110
 
        return self._call('append_file', relpath, f, mode)
111
 
 
112
 
    def _can_roundtrip_unix_modebits(self):
113
 
        return self.server.backing_transport._can_roundtrip_unix_modebits()
114
 
 
115
 
    def clone(self, relpath):
116
 
        return self.__class__(self.server, self.abspath(relpath))
117
 
 
118
 
    def delete(self, relpath):
119
 
        return self._call('delete', relpath)
120
 
 
121
 
    def delete_tree(self, relpath):
122
 
        return self._call('delete_tree', relpath)
123
 
 
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()
131
 
 
132
 
    def get(self, relpath):
133
 
        return self._call('get', relpath)
134
 
 
135
 
    def has(self, relpath):
136
 
        return self._call('has', relpath)
137
 
 
138
 
    def is_readonly(self):
139
 
        return self.server.backing_transport.is_readonly()
140
 
 
141
 
    def iter_files_recursive(self):
142
 
        backing_transport = self.server.backing_transport.clone(
143
 
            self._filter('.'))
144
 
        return backing_transport.iter_files_recursive()
145
 
 
146
 
    def listable(self):
147
 
        return self.server.backing_transport.listable()
148
 
 
149
 
    def list_dir(self, relpath):
150
 
        return self._call('list_dir', relpath)
151
 
 
152
 
    def lock_read(self, relpath):
153
 
        return self._call('lock_read', relpath)
154
 
 
155
 
    def lock_write(self, relpath):
156
 
        return self._call('lock_write', relpath)
157
 
 
158
 
    def mkdir(self, relpath, mode=None):
159
 
        return self._call('mkdir', relpath, mode)
160
 
 
161
 
    def open_write_stream(self, relpath, mode=None):
162
 
        return self._call('open_write_stream', relpath, mode)
163
 
 
164
 
    def put_file(self, relpath, f, mode=None):
165
 
        return self._call('put_file', relpath, f, mode)
166
 
 
167
 
    def rename(self, rel_from, rel_to):
168
 
        return self._call('rename', rel_from, self._filter(rel_to))
169
 
 
170
 
    def rmdir(self, relpath):
171
 
        return self._call('rmdir', relpath)
172
 
 
173
 
    def stat(self, relpath):
174
 
        return self._call('stat', relpath)
175
 
 
176
 
 
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)]