1534.4.9
by Robert Collins
Add a readonly decorator for transports. |
1 |
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""Implementation of Transport that adapts another transport to be readonly."""
|
|
18 |
||
19 |
from bzrlib.errors import TransportNotPossible |
|
20 |
from bzrlib.transport import get_transport, Transport, Server |
|
21 |
||
22 |
||
23 |
class ReadonlyTransportDecorator(Transport): |
|
24 |
"""A decorator that can convert any transport to be readonly.
|
|
25 |
|
|
26 |
This does not use __getattr__ hacks as we need to ensure that
|
|
27 |
new writable methods are overridden correctly.
|
|
28 |
"""
|
|
29 |
||
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) |
|
60 |
||
61 |
def delete(self, relpath): |
|
62 |
"""See Transport.delete()."""
|
|
63 |
raise TransportNotPossible('readonly transport') |
|
64 |
||
1534.4.15
by Robert Collins
Remove shutil dependency in upgrade - create a delete_tree method for transports. |
65 |
def delete_tree(self, relpath): |
66 |
"""See Transport.delete_tree()."""
|
|
67 |
raise TransportNotPossible('readonly transport') |
|
68 |
||
1534.4.9
by Robert Collins
Add a readonly decorator for transports. |
69 |
def get(self, relpath): |
70 |
"""See Transport.get()."""
|
|
71 |
return self._decorated.get(relpath) |
|
72 |
||
73 |
def put(self, relpath, f, mode=None): |
|
74 |
"""See Transport.put()."""
|
|
75 |
raise TransportNotPossible('readonly transport') |
|
76 |
||
77 |
def mkdir(self, relpath, mode=None): |
|
78 |
"""See Transport.mkdir()."""
|
|
79 |
raise TransportNotPossible('readonly transport') |
|
80 |
||
81 |
def is_readonly(self): |
|
82 |
"""See Transport.is_readonly."""
|
|
83 |
return True |
|
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 |
||
1534.4.15
by Robert Collins
Remove shutil dependency in upgrade - create a delete_tree method for transports. |
97 |
def rmdir(self, relpath): |
98 |
"""See Transport.rmdir."""
|
|
99 |
raise TransportNotPossible('readonly transport') |
|
100 |
||
1534.4.9
by Robert Collins
Add a readonly decorator for transports. |
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 |
||
1534.4.26
by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create. |
109 |
def lock_read(self, relpath): |
110 |
"""See Transport.lock_read."""
|
|
111 |
return self._decorated.lock_read(relpath) |
|
112 |
||
113 |
def lock_write(self, relpath): |
|
114 |
"""See Transport.lock_write."""
|
|
115 |
raise TransportNotPossible('readonly transport') |
|
1534.4.9
by Robert Collins
Add a readonly decorator for transports. |
116 |
|
117 |
||
118 |
class ReadonlyServer(Server): |
|
119 |
"""Server for the ReadonlyTransportDecorator for testing with."""
|
|
120 |
||
1534.4.10
by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs. |
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() |
|
1534.4.9
by Robert Collins
Add a readonly decorator for transports. |
135 |
|
136 |
def tearDown(self): |
|
137 |
"""See bzrlib.transport.Server.tearDown."""
|
|
1534.4.10
by Robert Collins
Add TestCaseWithTransport class that provides tests with read and write transport pairs. |
138 |
if self._made_server: |
139 |
self._server.tearDown() |
|
1534.4.9
by Robert Collins
Add a readonly decorator for transports. |
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() |
|
148 |
||
149 |
||
150 |
def get_test_permutations(): |
|
151 |
"""Return the permutations to be used in testing."""
|
|
152 |
return [(ReadonlyTransportDecorator, ReadonlyServer), |
|
153 |
]
|