~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/readonly.py

  • Committer: Jamie Wilkinson
  • Date: 2006-03-27 12:44:47 UTC
  • mto: This revision was merged to the branch mainline in revision 1874.
  • Revision ID: jaq@spacepants.org-20060327124447-96b90762e112e280
test that revision accessors act idempotently

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006 Canonical Ltd
2
 
#
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Implementation of Transport that adapts another transport to be readonly."""
18
18
 
19
 
from bzrlib.errors import TransportNotPossible, NoSmartServer, NoSmartMedium
20
 
from bzrlib.transport.decorator import TransportDecorator, DecoratorServer
21
 
 
22
 
 
23
 
class ReadonlyTransportDecorator(TransportDecorator):
 
19
from bzrlib.errors import TransportNotPossible
 
20
from bzrlib.transport import get_transport, Transport, Server
 
21
 
 
22
 
 
23
class ReadonlyTransportDecorator(Transport):
24
24
    """A decorator that can convert any transport to be readonly.
25
 
 
26
 
    This is requested via the 'readonly+' prefix to get_transport().
 
25
    
 
26
    This does not use __getattr__ hacks as we need to ensure that
 
27
    new writable methods are overridden correctly.
27
28
    """
28
29
 
29
 
    def append_file(self, relpath, f, mode=None):
30
 
        """See Transport.append_file()."""
31
 
        raise TransportNotPossible('readonly transport')
32
 
    
33
 
    def append_bytes(self, relpath, bytes, mode=None):
34
 
        """See Transport.append_bytes()."""
35
 
        raise TransportNotPossible('readonly transport')
36
 
    
37
 
    @classmethod
38
 
    def _get_url_prefix(self):
39
 
        """Readonly transport decorators are invoked via 'readonly+'"""
40
 
        return 'readonly+'
 
30
    def __init__(self, url, _decorated=None):
 
31
        """Set the 'base' path where files will be stored.
 
32
        
 
33
        _decorated is a private parameter for cloning."""
 
34
        assert url.startswith('readonly+')
 
35
        decorated_url = url[len('readonly+'):]
 
36
        if _decorated is None:
 
37
            self._decorated = get_transport(decorated_url)
 
38
        else:
 
39
            self._decorated = _decorated
 
40
        super(ReadonlyTransportDecorator, self).__init__(
 
41
            "readonly+" + self._decorated.base)
 
42
 
 
43
    def clone(self, offset=None):
 
44
        """See Transport.clone()."""
 
45
        decorated_clone = self._decorated.clone(offset)
 
46
        return ReadonlyTransportDecorator("readonly+" + decorated_clone.base,
 
47
                                          decorated_clone)
 
48
 
 
49
    def abspath(self, relpath):
 
50
        """See Transport.abspath()."""
 
51
        return "readonly+" + self._decorated.abspath(relpath)
 
52
 
 
53
    def append(self, relpath, f):
 
54
        """See Transport.append()."""
 
55
        raise TransportNotPossible('readonly transport')
 
56
 
 
57
    def has(self, relpath):
 
58
        """See Transport.has()."""
 
59
        return self._decorated.has(relpath)
41
60
 
42
61
    def delete(self, relpath):
43
62
        """See Transport.delete()."""
47
66
        """See Transport.delete_tree()."""
48
67
        raise TransportNotPossible('readonly transport')
49
68
 
50
 
    def put_file(self, relpath, f, mode=None):
51
 
        """See Transport.put_file()."""
52
 
        raise TransportNotPossible('readonly transport')
 
69
    def get(self, relpath):
 
70
        """See Transport.get()."""
 
71
        return self._decorated.get(relpath)
53
72
 
54
 
    def put_bytes(self, relpath, bytes, mode=None):
55
 
        """See Transport.put_bytes()."""
 
73
    def put(self, relpath, f, mode=None):
 
74
        """See Transport.put()."""
56
75
        raise TransportNotPossible('readonly transport')
57
76
 
58
77
    def mkdir(self, relpath, mode=None):
63
82
        """See Transport.is_readonly."""
64
83
        return True
65
84
 
 
85
    def listable(self):
 
86
        """See Transport.listable."""
 
87
        return self._decorated.listable()
 
88
 
 
89
    def iter_files_recursive(self):
 
90
        """See Transport.iter_files_recursive()."""
 
91
        return self._decorated.iter_files_recursive()
 
92
    
 
93
    def list_dir(self, relpath):
 
94
        """See Transport.list_dir()."""
 
95
        return self._decorated.list_dir(relpath)
 
96
    
66
97
    def rmdir(self, relpath):
67
98
        """See Transport.rmdir."""
68
99
        raise TransportNotPossible('readonly transport')
69
100
 
 
101
    def should_cache(self):
 
102
        """See Transport.should_cache()."""
 
103
        return self._decorated.should_cache()
 
104
 
 
105
    def stat(self, relpath):
 
106
        """See Transport.stat()."""
 
107
        return self._decorated.stat(relpath)
 
108
 
 
109
    def lock_read(self, relpath):
 
110
        """See Transport.lock_read."""
 
111
        return self._decorated.lock_read(relpath)
 
112
 
70
113
    def lock_write(self, relpath):
71
114
        """See Transport.lock_write."""
72
115
        raise TransportNotPossible('readonly transport')
73
116
 
74
 
    def get_smart_client(self):
75
 
        raise NoSmartServer(self.base)
76
 
 
77
 
    def get_smart_medium(self):
78
 
        raise NoSmartMedium(self)
79
 
 
80
 
 
81
 
 
82
 
class ReadonlyServer(DecoratorServer):
 
117
 
 
118
class ReadonlyServer(Server):
83
119
    """Server for the ReadonlyTransportDecorator for testing with."""
84
120
 
85
 
    def get_decorator_class(self):
86
 
        return ReadonlyTransportDecorator
 
121
    def setUp(self, server=None):
 
122
        """See bzrlib.transport.Server.setUp.
 
123
 
 
124
        :server: decorate the urls given by server. If not provided a
 
125
        LocalServer is created.
 
126
        """
 
127
        if server is not None:
 
128
            self._made_server = False
 
129
            self._server = server
 
130
        else:
 
131
            from bzrlib.transport.local import LocalRelpathServer
 
132
            self._made_server = True
 
133
            self._server = LocalRelpathServer()
 
134
            self._server.setUp()
 
135
 
 
136
    def tearDown(self):
 
137
        """See bzrlib.transport.Server.tearDown."""
 
138
        if self._made_server:
 
139
            self._server.tearDown()
 
140
 
 
141
    def get_bogus_url(self):
 
142
        """See bzrlib.transport.Server.get_bogus_url."""
 
143
        return "readonly+" + self._server.get_bogus_url()
 
144
 
 
145
    def get_url(self):
 
146
        """See bzrlib.transport.Server.get_url."""
 
147
        return "readonly+" + self._server.get_url()
87
148
 
88
149
 
89
150
def get_test_permutations():