1
1
# Copyright (C) 2006 Canonical Ltd
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.
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.
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
17
17
"""Implementation of Transport that adapts another transport to be readonly."""
19
from bzrlib.errors import TransportNotPossible, NoSmartServer, NoSmartMedium
20
from bzrlib.transport.decorator import TransportDecorator, DecoratorServer
23
class ReadonlyTransportDecorator(TransportDecorator):
19
from bzrlib.errors import TransportNotPossible
20
from bzrlib.transport import get_transport, Transport, Server
23
class ReadonlyTransportDecorator(Transport):
24
24
"""A decorator that can convert any transport to be readonly.
26
This is requested via the 'readonly+' prefix to get_transport().
26
This does not use __getattr__ hacks as we need to ensure that
27
new writable methods are overridden correctly.
29
def append_file(self, relpath, f, mode=None):
30
"""See Transport.append_file()."""
31
raise TransportNotPossible('readonly transport')
33
def append_bytes(self, relpath, bytes, mode=None):
34
"""See Transport.append_bytes()."""
35
raise TransportNotPossible('readonly transport')
38
def _get_url_prefix(self):
39
"""Readonly transport decorators are invoked via 'readonly+'"""
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
def append(self, relpath, f):
54
"""See Transport.append()."""
55
raise TransportNotPossible('readonly transport')
57
def has(self, relpath):
58
"""See Transport.has()."""
59
return self._decorated.has(relpath)
42
61
def delete(self, relpath):
43
62
"""See Transport.delete()."""
47
66
"""See Transport.delete_tree()."""
48
67
raise TransportNotPossible('readonly transport')
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)
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')
58
77
def mkdir(self, relpath, mode=None):
63
82
"""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)
66
97
def rmdir(self, relpath):
67
98
"""See Transport.rmdir."""
68
99
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)
70
113
def lock_write(self, relpath):
71
114
"""See Transport.lock_write."""
72
115
raise TransportNotPossible('readonly transport')
74
def get_smart_client(self):
75
raise NoSmartServer(self.base)
77
def get_smart_medium(self):
78
raise NoSmartMedium(self)
82
class ReadonlyServer(DecoratorServer):
118
class ReadonlyServer(Server):
83
119
"""Server for the ReadonlyTransportDecorator for testing with."""
85
def get_decorator_class(self):
86
return ReadonlyTransportDecorator
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()
89
150
def get_test_permutations():