~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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Implementation of Transport that traces transport operations.

This does not change the transport behaviour at all, merely records every call
and then delegates it.
"""

from bzrlib.transport.decorator import TransportDecorator, DecoratorServer


class TransportTraceDecorator(TransportDecorator):
    """A tracing decorator for Transports.

    Calls that potentially perform IO are logged to self._activity. The
    _activity attribute is shared as the transport is cloned, but not if a new
    transport is created without cloning.

    Not all operations are logged at this point, if you need an unlogged
    operation please add a test to the tests of this transport, for the logging
    of the operation you want logged.

    See also TransportLogDecorator, that records a machine-readable log in 
    memory for eg testing.
    """

    def __init__(self, url, _decorated=None, _from_transport=None):
        """Set the 'base' path where files will be stored.

        _decorated is a private parameter for cloning.
        """
        TransportDecorator.__init__(self, url, _decorated)
        if _from_transport is None:
            # newly created
            self._activity = []
        else:
            # cloned
            self._activity = _from_transport._activity

    def append_file(self, relpath, f, mode=None):
        """See Transport.append_file()."""
        return self._decorated.append_file(relpath, f, mode=mode)

    def append_bytes(self, relpath, bytes, mode=None):
        """See Transport.append_bytes()."""
        return self._decorated.append_bytes(relpath, bytes, mode=mode)

    def delete(self, relpath):
        """See Transport.delete()."""
        self._activity.append(('delete', relpath))
        return self._decorated.delete(relpath)

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

    @classmethod
    def _get_url_prefix(self):
        """Tracing transports are identified by 'trace+'"""
        return 'trace+'

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

    def get_smart_client(self):
        return self._decorated.get_smart_client()

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

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

    def mkdir(self, relpath, mode=None):
        """See Transport.mkdir()."""
        self._trace(('mkdir', relpath, mode))
        return self._decorated.mkdir(relpath, mode)

    def open_write_stream(self, relpath, mode=None):
        """See Transport.open_write_stream."""
        return self._decorated.open_write_stream(relpath, mode=mode)

    def put_file(self, relpath, f, mode=None):
        """See Transport.put_file()."""
        return self._decorated.put_file(relpath, f, mode)

    def put_bytes(self, relpath, bytes, mode=None):
        """See Transport.put_bytes()."""
        self._trace(('put_bytes', relpath, len(bytes), mode))
        return self._decorated.put_bytes(relpath, bytes, mode)

    def put_bytes_non_atomic(self, relpath, bytes, mode=None,
        create_parent_dir=False, dir_mode=None):
        """See Transport.put_bytes_non_atomic."""
        self._trace(('put_bytes_non_atomic', relpath, len(bytes), mode,
            create_parent_dir, dir_mode))
        return self._decorated.put_bytes_non_atomic(relpath, bytes, mode=mode,
            create_parent_dir=create_parent_dir, dir_mode=dir_mode)

    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 readv(self, relpath, offsets, adjust_for_latency=False,
        upper_limit=None):
        # we override at the readv() level rather than _readv() so that any
        # latency adjustments will be done by the underlying transport
        self._trace(('readv', relpath, offsets, adjust_for_latency,
            upper_limit))
        return self._decorated.readv(relpath, offsets, adjust_for_latency,
            upper_limit)

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

    def rename(self, rel_from, rel_to):
        self._activity.append(('rename', rel_from, rel_to))
        return self._decorated.rename(rel_from, rel_to)

    def rmdir(self, relpath):
        """See Transport.rmdir."""
        self._trace(('rmdir', relpath))
        return self._decorated.rmdir(relpath)

    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."""
        return self._decorated.lock_write(relpath)

    def _trace(self, operation_tuple):
        """Record that a transport operation occured.

        :param operation: Tuple of transport call name and arguments.
        """
        self._activity.append(operation_tuple)


class TraceServer(DecoratorServer):
    """Server for the TransportTraceDecorator for testing with."""

    def get_decorator_class(self):
        return TransportTraceDecorator


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