/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 profile_imports.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""A custom importer and regex compiler which logs time spent."""
19
19
 
 
20
import re
20
21
import sys
21
22
import time
22
23
 
23
24
 
24
 
if sys.version_info < (2, 5, 0):
25
 
    import sre
26
 
    re = sre
27
 
else:
28
 
    import re
29
 
 
30
 
 
31
25
_parent_stack = []
32
26
_total_stack = {}
33
27
_info = {}
47
41
        _total_stack[_parent_stack[-1]].append(this_stack)
48
42
    _total_stack[this_stack] = []
49
43
    _parent_stack.append(this_stack)
50
 
    _info[this_stack] = [len(_parent_stack)-1, frame_name, frame_lineno, scope_name]
 
44
    _info[this_stack] = [len(_parent_stack) - 1, frame_name, frame_lineno,
 
45
                         scope_name]
51
46
 
52
47
    return this_stack
53
48
 
64
59
 
65
60
def log_stack_info(out_file, sorted=True, hide_fast=True):
66
61
    # Find all of the roots with import = 0
67
 
    out_file.write('%5s %5s %-40s @ %s:%s\n'
68
 
        % ('cum', 'inline', 'name', 'file', 'line'))
69
 
    todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
 
62
    out_file.write(
 
63
        '%5s %5s %-40s @ %s:%s\n'
 
64
        % ('cum', 'local', 'name', 'file', 'line'))
 
65
    todo = [(value[-1], key) for key, value in _info.items() if value[0] == 0]
70
66
 
71
67
    if sorted:
72
68
        todo.sort()
90
86
 
91
87
        # indent, cum_time, mod_time, name,
92
88
        # scope_name, frame_name, frame_lineno
93
 
        out_file.write('%5.1f %5.1f %-40s @ %s:%d\n'
94
 
            % (info[-1]*1000., mod_time*1000.,
95
 
               ('+'*info[0] + cur[1]),
96
 
               info[1], info[2]))
 
89
        out_file.write(
 
90
            '%5.1f %5.1f %-40s @ %s:%d\n' % (
 
91
                info[-1] * 1000., mod_time * 1000.,
 
92
                ('+' * info[0] + cur[1]), info[1], info[2]))
97
93
 
98
94
        if sorted:
99
95
            c_times.sort()
104
100
 
105
101
_real_import = __import__
106
102
 
107
 
def timed_import(name, globals=None, locals=None, fromlist=None, level=None):
 
103
def timed_import(name, globals=None, locals=None, fromlist=None, level=0):
108
104
    """Wrap around standard importer to log import time"""
109
105
    # normally there are 4, but if this is called as __import__ eg by
110
106
    # /usr/lib/python2.6/email/__init__.py then there may be only one
111
107
    # parameter
112
 
    # level is only passed by python2.6
113
 
 
 
108
    # level has different default between Python 2 and 3, but codebase
114
109
    if globals is None:
115
110
        # can't determine the scope name afaics; we could peek up the stack to
116
111
        # see where this is being called from, but it should be a rare case.
122
117
        if scope_name is None:
123
118
            scope_name = globals.keys()
124
119
        else:
125
 
            # Trim out paths before bzrlib
126
 
            loc = scope_name.find('bzrlib')
127
 
            if loc != -1:
128
 
                scope_name = scope_name[loc:]
129
 
            # For stdlib, trim out early paths
130
 
            loc = scope_name.find('python2.4')
 
120
            # Trim out paths before breezy
 
121
            loc = scope_name.find('breezy')
131
122
            if loc != -1:
132
123
                scope_name = scope_name[loc:]
133
124
 
154
145
    tstart = _timer()
155
146
    try:
156
147
        # Do the import
157
 
        mod = _real_import(name, globals, locals, fromlist)
 
148
        return _real_import(name, globals, locals, fromlist, level=level)
158
149
    finally:
159
 
        tload = _timer()-tstart
 
150
        tload = _timer() - tstart
160
151
        stack_finish(this, tload)
161
152
 
162
 
    return mod
 
153
 
 
154
def _repr_regexp(pattern, max_len=30):
 
155
    """Present regexp pattern for logging, truncating if over max_len."""
 
156
    if len(pattern) > max_len:
 
157
        return repr(pattern[:max_len - 3]) + "..."
 
158
    return repr(pattern)
163
159
 
164
160
 
165
161
_real_compile = re._compile
179
175
        frame = sys._getframe(5)
180
176
        frame_name = frame.f_globals.get('__name__', '<unknown>')
181
177
    frame_lineno = frame.f_lineno
182
 
    this = stack_add(extra+repr(args[0]), frame_name, frame_lineno)
 
178
    this = stack_add(extra + _repr_regexp(args[0]), frame_name, frame_lineno)
183
179
 
184
180
    tstart = _timer()
185
181
    try:
202
198
    """Remove the import and regex compile timing hooks."""
203
199
    __builtins__['__import__'] = _real_import
204
200
    re._compile = _real_compile
205