~bzr-pqm/bzr/bzr.dev

2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1
# Copyright (C) 2007 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
16
17
"""Implementation of Transport that traces transport operations.
18
2745.5.4 by Robert Collins
Review feedback.
19
This does not change the transport behaviour at all, merely records every call
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
20
and then delegates it.
21
"""
22
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
23
from __future__ import absolute_import
24
5017.3.10 by Vincent Ladeuil
Move TraceServer to bzrlib.tests.test_server
25
from bzrlib.transport import decorator
26
27
28
class TransportTraceDecorator(decorator.TransportDecorator):
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
29
    """A tracing decorator for Transports.
30
31
    Calls that potentially perform IO are logged to self._activity. The
32
    _activity attribute is shared as the transport is cloned, but not if a new
33
    transport is created without cloning.
2745.5.4 by Robert Collins
Review feedback.
34
35
    Not all operations are logged at this point, if you need an unlogged
36
    operation please add a test to the tests of this transport, for the logging
37
    of the operation you want logged.
4491.2.2 by Martin Pool
TransportLogDecorator.readv must return a generator
38
39
    See also TransportLogDecorator, that records a machine-readable log in 
40
    memory for eg testing.
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
41
    """
42
43
    def __init__(self, url, _decorated=None, _from_transport=None):
44
        """Set the 'base' path where files will be stored.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
45
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
46
        _decorated is a private parameter for cloning.
47
        """
5017.3.19 by Vincent Ladeuil
Move TestingPathFilteringServer to bzrlib.tests.test_server
48
        super(TransportTraceDecorator, self).__init__(url, _decorated)
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
49
        if _from_transport is None:
50
            # newly created
51
            self._activity = []
52
        else:
53
            # cloned
54
            self._activity = _from_transport._activity
55
56
    def append_file(self, relpath, f, mode=None):
57
        """See Transport.append_file()."""
58
        return self._decorated.append_file(relpath, f, mode=mode)
59
60
    def append_bytes(self, relpath, bytes, mode=None):
61
        """See Transport.append_bytes()."""
62
        return self._decorated.append_bytes(relpath, bytes, mode=mode)
63
64
    def delete(self, relpath):
65
        """See Transport.delete()."""
3515.3.1 by Robert Collins
(robertc) More tracing for trace+ transport. (Robert Collins)
66
        self._activity.append(('delete', relpath))
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
67
        return self._decorated.delete(relpath)
68
69
    def delete_tree(self, relpath):
70
        """See Transport.delete_tree()."""
71
        return self._decorated.delete_tree(relpath)
72
73
    @classmethod
74
    def _get_url_prefix(self):
75
        """Tracing transports are identified by 'trace+'"""
76
        return 'trace+'
77
78
    def get(self, relpath):
79
        """See Transport.get()."""
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
80
        self._trace(('get', relpath))
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
81
        return self._decorated.get(relpath)
82
83
    def get_smart_client(self):
84
        return self._decorated.get_smart_client()
85
86
    def has(self, relpath):
87
        """See Transport.has()."""
88
        return self._decorated.has(relpath)
89
90
    def is_readonly(self):
91
        """See Transport.is_readonly."""
92
        return self._decorated.is_readonly()
93
94
    def mkdir(self, relpath, mode=None):
95
        """See Transport.mkdir()."""
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
96
        self._trace(('mkdir', relpath, mode))
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
97
        return self._decorated.mkdir(relpath, mode)
98
99
    def open_write_stream(self, relpath, mode=None):
100
        """See Transport.open_write_stream."""
101
        return self._decorated.open_write_stream(relpath, mode=mode)
102
103
    def put_file(self, relpath, f, mode=None):
104
        """See Transport.put_file()."""
105
        return self._decorated.put_file(relpath, f, mode)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
106
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
107
    def put_bytes(self, relpath, bytes, mode=None):
108
        """See Transport.put_bytes()."""
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
109
        self._trace(('put_bytes', relpath, len(bytes), mode))
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
110
        return self._decorated.put_bytes(relpath, bytes, mode)
111
4063.2.1 by Robert Collins
Teach trace+ to log rmdir and put_bytes_non_atomic calls.
112
    def put_bytes_non_atomic(self, relpath, bytes, mode=None,
113
        create_parent_dir=False, dir_mode=None):
114
        """See Transport.put_bytes_non_atomic."""
115
        self._trace(('put_bytes_non_atomic', relpath, len(bytes), mode,
116
            create_parent_dir, dir_mode))
117
        return self._decorated.put_bytes_non_atomic(relpath, bytes, mode=mode,
118
            create_parent_dir=create_parent_dir, dir_mode=dir_mode)
119
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
120
    def listable(self):
121
        """See Transport.listable."""
122
        return self._decorated.listable()
123
124
    def iter_files_recursive(self):
125
        """See Transport.iter_files_recursive()."""
126
        return self._decorated.iter_files_recursive()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
127
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
128
    def list_dir(self, relpath):
129
        """See Transport.list_dir()."""
130
        return self._decorated.list_dir(relpath)
131
132
    def readv(self, relpath, offsets, adjust_for_latency=False,
133
        upper_limit=None):
4491.2.2 by Martin Pool
TransportLogDecorator.readv must return a generator
134
        # we override at the readv() level rather than _readv() so that any
135
        # latency adjustments will be done by the underlying transport
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
136
        self._trace(('readv', relpath, offsets, adjust_for_latency,
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
137
            upper_limit))
138
        return self._decorated.readv(relpath, offsets, adjust_for_latency,
139
            upper_limit)
140
141
    def recommended_page_size(self):
142
        """See Transport.recommended_page_size()."""
143
        return self._decorated.recommended_page_size()
144
145
    def rename(self, rel_from, rel_to):
3515.3.1 by Robert Collins
(robertc) More tracing for trace+ transport. (Robert Collins)
146
        self._activity.append(('rename', rel_from, rel_to))
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
147
        return self._decorated.rename(rel_from, rel_to)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
148
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
149
    def rmdir(self, relpath):
150
        """See Transport.rmdir."""
4063.2.1 by Robert Collins
Teach trace+ to log rmdir and put_bytes_non_atomic calls.
151
        self._trace(('rmdir', relpath))
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
152
        return self._decorated.rmdir(relpath)
153
154
    def stat(self, relpath):
155
        """See Transport.stat()."""
156
        return self._decorated.stat(relpath)
157
158
    def lock_read(self, relpath):
159
        """See Transport.lock_read."""
160
        return self._decorated.lock_read(relpath)
161
162
    def lock_write(self, relpath):
163
        """See Transport.lock_write."""
164
        return self._decorated.lock_write(relpath)
165
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
166
    def _trace(self, operation_tuple):
167
        """Record that a transport operation occured.
168
169
        :param operation: Tuple of transport call name and arguments.
170
        """
171
        self._activity.append(operation_tuple)
172
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
173
174
def get_test_permutations():
2745.5.4 by Robert Collins
Review feedback.
175
    """Return the permutations to be used in testing."""
5017.3.10 by Vincent Ladeuil
Move TraceServer to bzrlib.tests.test_server
176
    from bzrlib.tests import test_server
177
    return [(TransportTraceDecorator, test_server.TraceServer)]