/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: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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