~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/trace.py

  • Committer: Martin Pool
  • Date: 2009-03-03 01:45:32 UTC
  • mto: (4070.4.5 gnu-changelog)
  • mto: This revision was merged to the branch mainline in revision 4081.
  • Revision ID: mbp@sourcefrog.net-20090303014532-d223fxy97cb1og8f
Recommend setting timestamp in BranchBuilder

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Implementation of Transport that traces transport operations.
 
18
 
 
19
This does not change the transport behaviour at all, merely records every call
 
20
and then delegates it.
 
21
"""
 
22
 
 
23
from bzrlib.transport.decorator import TransportDecorator, DecoratorServer
 
24
 
 
25
 
 
26
class TransportTraceDecorator(TransportDecorator):
 
27
    """A tracing decorator for Transports.
 
28
 
 
29
    Calls that potentially perform IO are logged to self._activity. The
 
30
    _activity attribute is shared as the transport is cloned, but not if a new
 
31
    transport is created without cloning.
 
32
 
 
33
    Not all operations are logged at this point, if you need an unlogged
 
34
    operation please add a test to the tests of this transport, for the logging
 
35
    of the operation you want logged.
 
36
    """
 
37
 
 
38
    def __init__(self, url, _decorated=None, _from_transport=None):
 
39
        """Set the 'base' path where files will be stored.
 
40
 
 
41
        _decorated is a private parameter for cloning.
 
42
        """
 
43
        TransportDecorator.__init__(self, url, _decorated)
 
44
        if _from_transport is None:
 
45
            # newly created
 
46
            self._activity = []
 
47
        else:
 
48
            # cloned
 
49
            self._activity = _from_transport._activity
 
50
 
 
51
    def append_file(self, relpath, f, mode=None):
 
52
        """See Transport.append_file()."""
 
53
        return self._decorated.append_file(relpath, f, mode=mode)
 
54
 
 
55
    def append_bytes(self, relpath, bytes, mode=None):
 
56
        """See Transport.append_bytes()."""
 
57
        return self._decorated.append_bytes(relpath, bytes, mode=mode)
 
58
 
 
59
    def delete(self, relpath):
 
60
        """See Transport.delete()."""
 
61
        self._activity.append(('delete', relpath))
 
62
        return self._decorated.delete(relpath)
 
63
 
 
64
    def delete_tree(self, relpath):
 
65
        """See Transport.delete_tree()."""
 
66
        return self._decorated.delete_tree(relpath)
 
67
 
 
68
    @classmethod
 
69
    def _get_url_prefix(self):
 
70
        """Tracing transports are identified by 'trace+'"""
 
71
        return 'trace+'
 
72
 
 
73
    def get(self, relpath):
 
74
        """See Transport.get()."""
 
75
        self._trace(('get', relpath))
 
76
        return self._decorated.get(relpath)
 
77
 
 
78
    def get_smart_client(self):
 
79
        return self._decorated.get_smart_client()
 
80
 
 
81
    def has(self, relpath):
 
82
        """See Transport.has()."""
 
83
        return self._decorated.has(relpath)
 
84
 
 
85
    def is_readonly(self):
 
86
        """See Transport.is_readonly."""
 
87
        return self._decorated.is_readonly()
 
88
 
 
89
    def mkdir(self, relpath, mode=None):
 
90
        """See Transport.mkdir()."""
 
91
        self._trace(('mkdir', relpath, mode))
 
92
        return self._decorated.mkdir(relpath, mode)
 
93
 
 
94
    def open_write_stream(self, relpath, mode=None):
 
95
        """See Transport.open_write_stream."""
 
96
        return self._decorated.open_write_stream(relpath, mode=mode)
 
97
 
 
98
    def put_file(self, relpath, f, mode=None):
 
99
        """See Transport.put_file()."""
 
100
        return self._decorated.put_file(relpath, f, mode)
 
101
 
 
102
    def put_bytes(self, relpath, bytes, mode=None):
 
103
        """See Transport.put_bytes()."""
 
104
        self._trace(('put_bytes', relpath, len(bytes), mode))
 
105
        return self._decorated.put_bytes(relpath, bytes, mode)
 
106
 
 
107
    def listable(self):
 
108
        """See Transport.listable."""
 
109
        return self._decorated.listable()
 
110
 
 
111
    def iter_files_recursive(self):
 
112
        """See Transport.iter_files_recursive()."""
 
113
        return self._decorated.iter_files_recursive()
 
114
 
 
115
    def list_dir(self, relpath):
 
116
        """See Transport.list_dir()."""
 
117
        return self._decorated.list_dir(relpath)
 
118
 
 
119
    def readv(self, relpath, offsets, adjust_for_latency=False,
 
120
        upper_limit=None):
 
121
        """See Transport.readv."""
 
122
        self._trace(('readv', relpath, offsets, adjust_for_latency,
 
123
            upper_limit))
 
124
        return self._decorated.readv(relpath, offsets, adjust_for_latency,
 
125
            upper_limit)
 
126
 
 
127
    def recommended_page_size(self):
 
128
        """See Transport.recommended_page_size()."""
 
129
        return self._decorated.recommended_page_size()
 
130
 
 
131
    def rename(self, rel_from, rel_to):
 
132
        self._activity.append(('rename', rel_from, rel_to))
 
133
        return self._decorated.rename(rel_from, rel_to)
 
134
 
 
135
    def rmdir(self, relpath):
 
136
        """See Transport.rmdir."""
 
137
        return self._decorated.rmdir(relpath)
 
138
 
 
139
    def stat(self, relpath):
 
140
        """See Transport.stat()."""
 
141
        return self._decorated.stat(relpath)
 
142
 
 
143
    def lock_read(self, relpath):
 
144
        """See Transport.lock_read."""
 
145
        return self._decorated.lock_read(relpath)
 
146
 
 
147
    def lock_write(self, relpath):
 
148
        """See Transport.lock_write."""
 
149
        return self._decorated.lock_write(relpath)
 
150
 
 
151
    def _trace(self, operation_tuple):
 
152
        """Record that a transport operation occured.
 
153
 
 
154
        :param operation: Tuple of transport call name and arguments.
 
155
        """
 
156
        self._activity.append(operation_tuple)
 
157
 
 
158
 
 
159
class TraceServer(DecoratorServer):
 
160
    """Server for the TransportTraceDecorator for testing with."""
 
161
 
 
162
    def get_decorator_class(self):
 
163
        return TransportTraceDecorator
 
164
 
 
165
 
 
166
def get_test_permutations():
 
167
    """Return the permutations to be used in testing."""
 
168
    return [(TransportTraceDecorator, TraceServer)]