~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/pathfilter.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

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