def normalize_paths(self, stats): import os.path from pstats import add_func_stats, func_std_string python_paths = sorted(sys.path, reverse=True) def rel_filename(filename): for path in python_paths: if filename.startswith(path): return filename[len(path) + 1:] return os.path.basename(filename) def func_strip_path(func_name): filename, line, name = func_name return rel_filename(filename), line, name oldstats = stats.stats stats.stats = newstats = {} max_name_len = 0 for func, (cc, nc, tt, ct, callers) in oldstats.iteritems(): newfunc = func_strip_path(func) if len(func_std_string(newfunc)) > max_name_len: max_name_len = len(func_std_string(newfunc)) newcallers = {} for func2, caller in callers.iteritems(): newcallers[func_strip_path(func2)] = caller if newfunc in newstats: newstats[newfunc] = add_func_stats( newstats[newfunc], (cc, nc, tt, ct, newcallers)) else: newstats[newfunc] = (cc, nc, tt, ct, newcallers) old_top = stats.top_level stats.top_level = new_top = {} for func in old_top: new_top[func_strip_path(func)] = None stats.max_name_len = max_name_len stats.fcn_list = None stats.all_callees = None return self
def normalize_paths(self, stats): import os.path from pstats import add_func_stats, func_std_string python_paths = sorted(sys.path, reverse=True) def rel_filename(filename): for path in python_paths: if filename.startswith(path): return filename[len(path) + 1:] return os.path.basename(filename) def func_strip_path(func_name): filename, line, name = func_name return rel_filename(filename), line, name oldstats = stats.stats stats.stats = newstats = {} max_name_len = 0 for func, (cc, nc, tt, ct, callers) in six.iteritems(oldstats): newfunc = func_strip_path(func) if len(func_std_string(newfunc)) > max_name_len: max_name_len = len(func_std_string(newfunc)) newcallers = {} for func2, caller in six.iteritems(callers): newcallers[func_strip_path(func2)] = caller if newfunc in newstats: newstats[newfunc] = add_func_stats( newstats[newfunc], (cc, nc, tt, ct, newcallers)) else: newstats[newfunc] = (cc, nc, tt, ct, newcallers) old_top = stats.top_level stats.top_level = new_top = {} for func in old_top: new_top[func_strip_path(func)] = None stats.max_name_len = max_name_len stats.fcn_list = None stats.all_callees = None return self
def strip_dirs(self, additional_system_paths=()): # def strip_func(func_name): # return strip_dir_from_module_regex(func_name, pattern) # path_prefixes = set() # for path in chain(sys.path, additional_system_paths): # if not path: # continue # if path[-1] != '/': # path = path + '/' # path_prefixes.add(path) strip_func = create_strip_func(additional_system_paths) oldstats = self.stats self.stats = newstats = {} max_name_len = 0 for func, (cc, nc, tt, ct, callers) in oldstats.iteritems(): newfunc = strip_func(func) if len(func_std_string(newfunc)) > max_name_len: max_name_len = len(func_std_string(newfunc)) newcallers = {} for func2, caller in callers.iteritems(): newcallers[strip_func(func2)] = caller if newfunc in newstats: newstats[newfunc] = add_func_stats( newstats[newfunc], (cc, nc, tt, ct, newcallers)) else: newstats[newfunc] = (cc, nc, tt, ct, newcallers) old_top = self.top_level self.top_level = new_top = {} for func in old_top: new_top[strip_func(func)] = None self.max_name_len = max_name_len self.fcn_list = None self.all_callees = None return self