~mhammond/bzr/update-r

« back to all changes in this revision

Viewing changes to bzrlib/transport/trace.py

  • Committer: Mark Hammond
  • Date: 2008-08-19 01:09:37 UTC
  • mfrom: (2009.1.1629 +trunk)
  • Revision ID: mhammond@skippinet.com.au-20080819010937-aohklatall0s6m3z
First attempt to merge .dev and resolve the conflicts (but tests are 
failing)

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
    Another future enhancement would be to log to bzrlib.trace.mutter when
 
38
    trace+ is used from the command line (or perhaps as well/instead use
 
39
    -Dtransport), to make tracing operations of the entire program easily.
 
40
    """
 
41
 
 
42
    def __init__(self, url, _decorated=None, _from_transport=None):
 
43
        """Set the 'base' path where files will be stored.
 
44
        
 
45
        _decorated is a private parameter for cloning.
 
46
        """
 
47
        TransportDecorator.__init__(self, url, _decorated)
 
48
        if _from_transport is None:
 
49
            # newly created
 
50
            self._activity = []
 
51
        else:
 
52
            # cloned
 
53
            self._activity = _from_transport._activity
 
54
 
 
55
    def append_file(self, relpath, f, mode=None):
 
56
        """See Transport.append_file()."""
 
57
        return self._decorated.append_file(relpath, f, mode=mode)
 
58
 
 
59
    def append_bytes(self, relpath, bytes, mode=None):
 
60
        """See Transport.append_bytes()."""
 
61
        return self._decorated.append_bytes(relpath, bytes, mode=mode)
 
62
 
 
63
    def delete(self, relpath):
 
64
        """See Transport.delete()."""
 
65
        self._activity.append(('delete', relpath))
 
66
        return self._decorated.delete(relpath)
 
67
 
 
68
    def delete_tree(self, relpath):
 
69
        """See Transport.delete_tree()."""
 
70
        return self._decorated.delete_tree(relpath)
 
71
 
 
72
    @classmethod
 
73
    def _get_url_prefix(self):
 
74
        """Tracing transports are identified by 'trace+'"""
 
75
        return 'trace+'
 
76
 
 
77
    def get(self, relpath):
 
78
        """See Transport.get()."""
 
79
        self._activity.append(('get', relpath))
 
80
        return self._decorated.get(relpath)
 
81
 
 
82
    def get_smart_client(self):
 
83
        return self._decorated.get_smart_client()
 
84
 
 
85
    def has(self, relpath):
 
86
        """See Transport.has()."""
 
87
        return self._decorated.has(relpath)
 
88
 
 
89
    def is_readonly(self):
 
90
        """See Transport.is_readonly."""
 
91
        return self._decorated.is_readonly()
 
92
 
 
93
    def mkdir(self, relpath, mode=None):
 
94
        """See Transport.mkdir()."""
 
95
        return self._decorated.mkdir(relpath, mode)
 
96
 
 
97
    def open_write_stream(self, relpath, mode=None):
 
98
        """See Transport.open_write_stream."""
 
99
        return self._decorated.open_write_stream(relpath, mode=mode)
 
100
 
 
101
    def put_file(self, relpath, f, mode=None):
 
102
        """See Transport.put_file()."""
 
103
        return self._decorated.put_file(relpath, f, mode)
 
104
    
 
105
    def put_bytes(self, relpath, bytes, mode=None):
 
106
        """See Transport.put_bytes()."""
 
107
        self._activity.append(('put_bytes', relpath, len(bytes), mode))
 
108
        return self._decorated.put_bytes(relpath, bytes, mode)
 
109
 
 
110
    def listable(self):
 
111
        """See Transport.listable."""
 
112
        return self._decorated.listable()
 
113
 
 
114
    def iter_files_recursive(self):
 
115
        """See Transport.iter_files_recursive()."""
 
116
        return self._decorated.iter_files_recursive()
 
117
    
 
118
    def list_dir(self, relpath):
 
119
        """See Transport.list_dir()."""
 
120
        return self._decorated.list_dir(relpath)
 
121
 
 
122
    def readv(self, relpath, offsets, adjust_for_latency=False,
 
123
        upper_limit=None):
 
124
        """See Transport.readv."""
 
125
        self._activity.append(('readv', relpath, offsets, adjust_for_latency,
 
126
            upper_limit))
 
127
        return self._decorated.readv(relpath, offsets, adjust_for_latency,
 
128
            upper_limit)
 
129
 
 
130
    def recommended_page_size(self):
 
131
        """See Transport.recommended_page_size()."""
 
132
        return self._decorated.recommended_page_size()
 
133
 
 
134
    def rename(self, rel_from, rel_to):
 
135
        self._activity.append(('rename', rel_from, rel_to))
 
136
        return self._decorated.rename(rel_from, rel_to)
 
137
    
 
138
    def rmdir(self, relpath):
 
139
        """See Transport.rmdir."""
 
140
        return self._decorated.rmdir(relpath)
 
141
 
 
142
    def stat(self, relpath):
 
143
        """See Transport.stat()."""
 
144
        return self._decorated.stat(relpath)
 
145
 
 
146
    def lock_read(self, relpath):
 
147
        """See Transport.lock_read."""
 
148
        return self._decorated.lock_read(relpath)
 
149
 
 
150
    def lock_write(self, relpath):
 
151
        """See Transport.lock_write."""
 
152
        return self._decorated.lock_write(relpath)
 
153
 
 
154
 
 
155
class TraceServer(DecoratorServer):
 
156
    """Server for the TransportTraceDecorator for testing with."""
 
157
 
 
158
    def get_decorator_class(self):
 
159
        return TransportTraceDecorator
 
160
 
 
161
 
 
162
def get_test_permutations():
 
163
    """Return the permutations to be used in testing."""
 
164
    return [(TransportTraceDecorator, TraceServer)]