17
17
"""Implementation of Transport that adapts another transport to be readonly."""
19
19
from bzrlib.errors import TransportNotPossible
20
from bzrlib.transport import get_transport, Transport, Server
23
class ReadonlyTransportDecorator(Transport):
20
from bzrlib.transport.decorator import TransportDecorator, DecoratorServer
23
class ReadonlyTransportDecorator(TransportDecorator):
24
24
"""A decorator that can convert any transport to be readonly.
26
This does not use __getattr__ hacks as we need to ensure that
27
new writable methods are overridden correctly.
26
This is requested via the 'readonly+' prefix to get_transport().
30
def __init__(self, url, _decorated=None):
31
"""Set the 'base' path where files will be stored.
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)
39
self._decorated = _decorated
40
super(ReadonlyTransportDecorator, self).__init__(
41
"readonly+" + self._decorated.base)
43
def clone(self, offset=None):
44
"""See Transport.clone()."""
45
decorated_clone = self._decorated.clone(offset)
46
return ReadonlyTransportDecorator("readonly+" + decorated_clone.base,
49
def abspath(self, relpath):
50
"""See Transport.abspath()."""
51
return "readonly+" + self._decorated.abspath(relpath)
53
29
def append(self, relpath, f):
54
30
"""See Transport.append()."""
55
31
raise TransportNotPossible('readonly transport')
57
def has(self, relpath):
58
"""See Transport.has()."""
59
return self._decorated.has(relpath)
34
def _get_url_prefix(self):
35
"""Readonly transport decorators are invoked via 'readonly+'"""
61
38
def delete(self, relpath):
62
39
"""See Transport.delete()."""
66
43
"""See Transport.delete_tree()."""
67
44
raise TransportNotPossible('readonly transport')
69
def get(self, relpath):
70
"""See Transport.get()."""
71
return self._decorated.get(relpath)
73
46
def put(self, relpath, f, mode=None):
74
47
"""See Transport.put()."""
75
48
raise TransportNotPossible('readonly transport')
82
55
"""See Transport.is_readonly."""
86
"""See Transport.listable."""
87
return self._decorated.listable()
89
def iter_files_recursive(self):
90
"""See Transport.iter_files_recursive()."""
91
return self._decorated.iter_files_recursive()
93
def list_dir(self, relpath):
94
"""See Transport.list_dir()."""
95
return self._decorated.list_dir(relpath)
97
58
def rmdir(self, relpath):
98
59
"""See Transport.rmdir."""
99
60
raise TransportNotPossible('readonly transport')
101
def should_cache(self):
102
"""See Transport.should_cache()."""
103
return self._decorated.should_cache()
105
def stat(self, relpath):
106
"""See Transport.stat()."""
107
return self._decorated.stat(relpath)
109
def lock_read(self, relpath):
110
"""See Transport.lock_read."""
111
return self._decorated.lock_read(relpath)
113
62
def lock_write(self, relpath):
114
63
"""See Transport.lock_write."""
115
64
raise TransportNotPossible('readonly transport')
118
class ReadonlyServer(Server):
67
class ReadonlyServer(DecoratorServer):
119
68
"""Server for the ReadonlyTransportDecorator for testing with."""
121
def setUp(self, server=None):
122
"""See bzrlib.transport.Server.setUp.
124
:server: decorate the urls given by server. If not provided a
125
LocalServer is created.
127
if server is not None:
128
self._made_server = False
129
self._server = server
131
from bzrlib.transport.local import LocalRelpathServer
132
self._made_server = True
133
self._server = LocalRelpathServer()
137
"""See bzrlib.transport.Server.tearDown."""
138
if self._made_server:
139
self._server.tearDown()
141
def get_bogus_url(self):
142
"""See bzrlib.transport.Server.get_bogus_url."""
143
return "readonly+" + self._server.get_bogus_url()
146
"""See bzrlib.transport.Server.get_url."""
147
return "readonly+" + self._server.get_url()
70
def get_decorator_class(self):
71
return ReadonlyTransportDecorator
150
74
def get_test_permutations():