/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4988.10.3 by John Arbash Meinel
Merge bzr.dev 5007, resolve conflict, update NEWS
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
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
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
16
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
17
"""Transport decorator that logs transport operations to .brz.log."""
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
18
6379.6.3 by Jelmer Vernooij
Use absolute_import.
19
from __future__ import absolute_import
20
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from ..trace import mutter
28
from ..transport import decorator
5017.3.11 by Vincent Ladeuil
Move LogDecoratorServer to bzrlib.tests.test_server
29
30
31
class TransportLogDecorator(decorator.TransportDecorator):
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
32
    """Decorator for Transports that logs interesting operations to .brz.log.
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
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.
4491.2.2 by Martin Pool
TransportLogDecorator.readv must return a generator
38
39
    See also TransportTraceDecorator, that records a machine-readable log in 
40
    memory for eg testing.
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
41
    """
42
43
    def __init__(self, *args, **kw):
44
        super(TransportLogDecorator, self).__init__(*args, **kw)
45
        def _make_hook(hookname):
46
            def _hook(relpath, *args, **kw):
3675.1.2 by Martin Pool
log+ decorator needs special handling for iter_files_recursive, which does not take a path
47
                return self._log_and_call(hookname, relpath, *args, **kw)
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
48
            return _hook
6621.2.27 by Martin
Fix failure in per_transport tests from treating list as a path
49
        # GZ 2017-05-21: Not all methods take relpath as first argument, for
50
        # instance copy_to takes list of relpaths. Also, unclear on url vs
51
        # filesystem path split. Needs tidying up.
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
52
        for methodname in (
53
            'append_bytes',
54
            'append_file',
55
            'copy_to',
56
            'delete',
57
            'get',
58
            'has',
59
            'open_write_stream',
60
            'mkdir',
61
            'move',
62
            'put_bytes', 'put_bytes_non_atomic', 'put_file put_file_non_atomic',
63
            'list_dir', 'lock_read', 'lock_write',
64
            'readv', 'rename', 'rmdir',
65
            'stat',
66
            'ulock',
67
            ):
68
            setattr(self, methodname, _make_hook(methodname))
69
70
    @classmethod
71
    def _get_url_prefix(self):
72
        return 'log+'
73
3675.1.2 by Martin Pool
log+ decorator needs special handling for iter_files_recursive, which does not take a path
74
    def iter_files_recursive(self):
75
        # needs special handling because it does not have a relpath parameter
76
        mutter("%s %s"
77
            % ('iter_files_recursive', self._decorated.base))
78
        return self._call_and_log_result('iter_files_recursive', (), {})
79
80
    def _log_and_call(self, methodname, relpath, *args, **kwargs):
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
81
        if kwargs:
82
            kwargs_str = dict(kwargs)
83
        else:
84
            kwargs_str = ''
85
        mutter("%s %s %s %s"
6621.2.27 by Martin
Fix failure in per_transport tests from treating list as a path
86
               % (methodname, relpath,
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
87
                  self._shorten(self._strip_tuple_parens(args)),
88
                  kwargs_str))
3675.1.2 by Martin Pool
log+ decorator needs special handling for iter_files_recursive, which does not take a path
89
        return self._call_and_log_result(methodname, (relpath,) + args, kwargs)
90
91
    def _call_and_log_result(self, methodname, args, kwargs):
92
        before = time.time()
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
93
        try:
3675.1.2 by Martin Pool
log+ decorator needs special handling for iter_files_recursive, which does not take a path
94
            result = getattr(self._decorated, methodname)(*args, **kwargs)
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
95
        except Exception as e:
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
96
            mutter("  --> %s" % e)
97
            mutter("      %.03fs" % (time.time() - before))
98
            raise
99
        return self._show_result(before, methodname, result)
100
101
    def _show_result(self, before, methodname, result):
102
        result_len = None
103
        if isinstance(result, types.GeneratorType):
4491.2.2 by Martin Pool
TransportLogDecorator.readv must return a generator
104
            # We now consume everything from the generator so that we can show
105
            # the results and the time it took to get them.  However, to keep
106
            # compatibility with callers that may specifically expect a result
107
            # (see <https://launchpad.net/bugs/340347>) we also return a new
108
            # generator, reset to the starting position.
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
109
            result = list(result)
4491.2.2 by Martin Pool
TransportLogDecorator.readv must return a generator
110
            return_result = iter(result)
111
        else:
112
            return_result = result
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
113
        # Is this an io object with a getvalue() method?
114
        getvalue = getattr(result, 'getvalue', None)
115
        if getvalue is not None:
116
            val = repr(getvalue())
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
117
            result_len = len(val)
118
            shown_result = "%s(%s) (%d bytes)" % (result.__class__.__name__,
119
                self._shorten(val), result_len)
120
        elif methodname == 'readv':
121
            num_hunks = len(result)
122
            total_bytes = sum((len(d) for o,d in result))
123
            shown_result = "readv response, %d hunks, %d total bytes" % (
124
                num_hunks, total_bytes)
125
            result_len = total_bytes
126
        else:
127
            shown_result = self._shorten(self._strip_tuple_parens(result))
128
        mutter("  --> %s" % shown_result)
4491.2.7 by Martin Pool
Suppress now-redundant rate and time display in log+ output
129
        # The log decorator no longer shows the elapsed time or transfer rate
130
        # because they're available in the log prefixes and the transport
131
        # activity display respectively.
132
        if False:
133
            elapsed = time.time() - before
134
            if result_len and elapsed > 0:
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
135
                # this is the rate of higher-level data, not the raw network
136
                # speed using base-10 units (see HACKING.txt).
137
                mutter("      %9.03fs %8dkB/s"
138
                       % (elapsed, result_len/elapsed/1000))
4491.2.7 by Martin Pool
Suppress now-redundant rate and time display in log+ output
139
            else:
140
                mutter("      %9.03fs" % (elapsed))
4491.2.2 by Martin Pool
TransportLogDecorator.readv must return a generator
141
        return return_result
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
142
143
    def _shorten(self, x):
144
        if len(x) > 70:
145
            x = x[:67] + '...'
146
        return x
147
148
    def _strip_tuple_parens(self, t):
149
        t = repr(t)
150
        if t[0] == '(' and t[-1] == ')':
151
            t = t[1:-1]
152
        return t
153
154
155
def get_test_permutations():
156
    """Return the permutations to be used in testing."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
157
    from ..tests import test_server
5017.3.11 by Vincent Ladeuil
Move LogDecoratorServer to bzrlib.tests.test_server
158
    return [(TransportLogDecorator, test_server.LogDecoratorServer)]