~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright (C) 2006 Canonical Ltd

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Implementation of Transport that adapts another transport to be readonly."""

from bzrlib.errors import TransportNotPossible
from bzrlib.transport import get_transport, Transport, Server


class ReadonlyTransportDecorator(Transport):
    """A decorator that can convert any transport to be readonly.
    
    This does not use __getattr__ hacks as we need to ensure that
    new writable methods are overridden correctly.
    """

    def __init__(self, url, _decorated=None):
        """Set the 'base' path where files will be stored.
        
        _decorated is a private parameter for cloning."""
        assert url.startswith('readonly+')
        decorated_url = url[len('readonly+'):]
        if _decorated is None:
            self._decorated = get_transport(decorated_url)
        else:
            self._decorated = _decorated
        super(ReadonlyTransportDecorator, self).__init__(
            "readonly+" + self._decorated.base)

    def clone(self, offset=None):
        """See Transport.clone()."""
        decorated_clone = self._decorated.clone(offset)
        return ReadonlyTransportDecorator("readonly+" + decorated_clone.base,
                                          decorated_clone)

    def abspath(self, relpath):
        """See Transport.abspath()."""
        return "readonly+" + self._decorated.abspath(relpath)

    def append(self, relpath, f):
        """See Transport.append()."""
        raise TransportNotPossible('readonly transport')

    def has(self, relpath):
        """See Transport.has()."""
        return self._decorated.has(relpath)

    def delete(self, relpath):
        """See Transport.delete()."""
        raise TransportNotPossible('readonly transport')

    def delete_tree(self, relpath):
        """See Transport.delete_tree()."""
        raise TransportNotPossible('readonly transport')

    def get(self, relpath):
        """See Transport.get()."""
        return self._decorated.get(relpath)

    def put(self, relpath, f, mode=None):
        """See Transport.put()."""
        raise TransportNotPossible('readonly transport')

    def mkdir(self, relpath, mode=None):
        """See Transport.mkdir()."""
        raise TransportNotPossible('readonly transport')

    def is_readonly(self):
        """See Transport.is_readonly."""
        return True

    def listable(self):
        """See Transport.listable."""
        return self._decorated.listable()

    def iter_files_recursive(self):
        """See Transport.iter_files_recursive()."""
        return self._decorated.iter_files_recursive()
    
    def list_dir(self, relpath):
        """See Transport.list_dir()."""
        return self._decorated.list_dir(relpath)
    
    def rmdir(self, relpath):
        """See Transport.rmdir."""
        raise TransportNotPossible('readonly transport')

    def should_cache(self):
        """See Transport.should_cache()."""
        return self._decorated.should_cache()

    def stat(self, relpath):
        """See Transport.stat()."""
        return self._decorated.stat(relpath)

    def lock_read(self, relpath):
        """See Transport.lock_read."""
        return self._decorated.lock_read(relpath)

    def lock_write(self, relpath):
        """See Transport.lock_write."""
        raise TransportNotPossible('readonly transport')


class ReadonlyServer(Server):
    """Server for the ReadonlyTransportDecorator for testing with."""

    def setUp(self, server=None):
        """See bzrlib.transport.Server.setUp.

        :server: decorate the urls given by server. If not provided a
        LocalServer is created.
        """
        if server is not None:
            self._made_server = False
            self._server = server
        else:
            from bzrlib.transport.local import LocalRelpathServer
            self._made_server = True
            self._server = LocalRelpathServer()
            self._server.setUp()

    def tearDown(self):
        """See bzrlib.transport.Server.tearDown."""
        if self._made_server:
            self._server.tearDown()

    def get_bogus_url(self):
        """See bzrlib.transport.Server.get_bogus_url."""
        return "readonly+" + self._server.get_bogus_url()

    def get_url(self):
        """See bzrlib.transport.Server.get_url."""
        return "readonly+" + self._server.get_url()


def get_test_permutations():
    """Return the permutations to be used in testing."""
    return [(ReadonlyTransportDecorator, ReadonlyServer),
            ]