/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/transport/log.py

[merge] robertc's integration, updated tests to check for retcode=3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Transport decorator that logs transport operations to brz.log."""
18
 
 
19
 
from __future__ import absolute_import
20
 
 
21
 
# see also the transportstats plugin, which gives you some summary information
22
 
# in a machine-readable dump
23
 
 
24
 
import time
25
 
import types
26
 
 
27
 
from ..trace import mutter
28
 
from ..transport import decorator
29
 
 
30
 
 
31
 
class TransportLogDecorator(decorator.TransportDecorator):
32
 
    """Decorator for Transports that logs interesting operations to brz.log.
33
 
 
34
 
    In general we want to log things that usually take a network round trip
35
 
    and may be slow.
36
 
 
37
 
    Not all operations are logged yet.
38
 
 
39
 
    See also TransportTraceDecorator, that records a machine-readable log in
40
 
    memory for eg testing.
41
 
    """
42
 
 
43
 
    def __init__(self, *args, **kw):
44
 
        super(TransportLogDecorator, self).__init__(*args, **kw)
45
 
 
46
 
        def _make_hook(hookname):
47
 
            def _hook(relpath, *args, **kw):
48
 
                return self._log_and_call(hookname, relpath, *args, **kw)
49
 
            return _hook
50
 
        # GZ 2017-05-21: Not all methods take relpath as first argument, for
51
 
        # instance copy_to takes list of relpaths. Also, unclear on url vs
52
 
        # filesystem path split. Needs tidying up.
53
 
        for methodname in (
54
 
                'append_bytes',
55
 
                'append_file',
56
 
                'copy_to',
57
 
                'delete',
58
 
                'get',
59
 
                'has',
60
 
                'open_write_stream',
61
 
                'mkdir',
62
 
                'move',
63
 
                'put_bytes', 'put_bytes_non_atomic', 'put_file put_file_non_atomic',
64
 
                'list_dir', 'lock_read', 'lock_write',
65
 
                'readv', 'rename', 'rmdir',
66
 
                'stat',
67
 
                'ulock',
68
 
                ):
69
 
            setattr(self, methodname, _make_hook(methodname))
70
 
 
71
 
    @classmethod
72
 
    def _get_url_prefix(self):
73
 
        return 'log+'
74
 
 
75
 
    def iter_files_recursive(self):
76
 
        # needs special handling because it does not have a relpath parameter
77
 
        mutter("%s %s"
78
 
               % ('iter_files_recursive', self._decorated.base))
79
 
        return self._call_and_log_result('iter_files_recursive', (), {})
80
 
 
81
 
    def _log_and_call(self, methodname, relpath, *args, **kwargs):
82
 
        if kwargs:
83
 
            kwargs_str = dict(kwargs)
84
 
        else:
85
 
            kwargs_str = ''
86
 
        mutter("%s %s %s %s"
87
 
               % (methodname, relpath,
88
 
                  self._shorten(self._strip_tuple_parens(args)),
89
 
                  kwargs_str))
90
 
        return self._call_and_log_result(methodname, (relpath,) + args, kwargs)
91
 
 
92
 
    def _call_and_log_result(self, methodname, args, kwargs):
93
 
        before = time.time()
94
 
        try:
95
 
            result = getattr(self._decorated, methodname)(*args, **kwargs)
96
 
        except Exception as e:
97
 
            mutter("  --> %s" % e)
98
 
            mutter("      %.03fs" % (time.time() - before))
99
 
            raise
100
 
        return self._show_result(before, methodname, result)
101
 
 
102
 
    def _show_result(self, before, methodname, result):
103
 
        result_len = None
104
 
        if isinstance(result, types.GeneratorType):
105
 
            # We now consume everything from the generator so that we can show
106
 
            # the results and the time it took to get them.  However, to keep
107
 
            # compatibility with callers that may specifically expect a result
108
 
            # (see <https://launchpad.net/bugs/340347>) we also return a new
109
 
            # generator, reset to the starting position.
110
 
            result = list(result)
111
 
            return_result = iter(result)
112
 
        else:
113
 
            return_result = result
114
 
        # Is this an io object with a getvalue() method?
115
 
        getvalue = getattr(result, 'getvalue', None)
116
 
        if getvalue is not None:
117
 
            val = repr(getvalue())
118
 
            result_len = len(val)
119
 
            shown_result = "%s(%s) (%d bytes)" % (result.__class__.__name__,
120
 
                                                  self._shorten(val), result_len)
121
 
        elif methodname == 'readv':
122
 
            num_hunks = len(result)
123
 
            total_bytes = sum((len(d) for o, d in result))
124
 
            shown_result = "readv response, %d hunks, %d total bytes" % (
125
 
                num_hunks, total_bytes)
126
 
            result_len = total_bytes
127
 
        else:
128
 
            shown_result = self._shorten(self._strip_tuple_parens(result))
129
 
        mutter("  --> %s" % shown_result)
130
 
        # The log decorator no longer shows the elapsed time or transfer rate
131
 
        # because they're available in the log prefixes and the transport
132
 
        # activity display respectively.
133
 
        if False:
134
 
            elapsed = time.time() - before
135
 
            if result_len and elapsed > 0:
136
 
                # this is the rate of higher-level data, not the raw network
137
 
                # speed using base-10 units (see HACKING.txt).
138
 
                mutter("      %9.03fs %8dkB/s"
139
 
                       % (elapsed, result_len / elapsed / 1000))
140
 
            else:
141
 
                mutter("      %9.03fs" % (elapsed))
142
 
        return return_result
143
 
 
144
 
    def _shorten(self, x):
145
 
        if len(x) > 70:
146
 
            x = x[:67] + '...'
147
 
        return x
148
 
 
149
 
    def _strip_tuple_parens(self, t):
150
 
        t = repr(t)
151
 
        if t[0] == '(' and t[-1] == ')':
152
 
            t = t[1:-1]
153
 
        return t
154
 
 
155
 
 
156
 
def get_test_permutations():
157
 
    """Return the permutations to be used in testing."""
158
 
    from ..tests import test_server
159
 
    return [(TransportLogDecorator, test_server.LogDecoratorServer)]