📄 trace.py
字号:
#!/usr/bin/env python# The contents of this file are subject to the MonetDB Public License# Version 1.1 (the "License"); you may not use this file except in# compliance with the License. You may obtain a copy of the License at# http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html## Software distributed under the License is distributed on an "AS IS"# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the# License for the specific language governing rights and limitations# under the License.## The Original Code is the MonetDB Database System.## The Initial Developer of the Original Code is CWI.# Portions created by CWI are Copyright (C) 1997-2007 CWI.# All Rights Reserved.# portions copyright 2001, Autonomous Zones Industries, Inc., all rights...# err... reserved and offered to the public under the terms of the# Python 2.2 license.# Author: Zooko O'Whielacronx# http://zooko.com/# mailto:zooko@zooko.com## Copyright 2000, Mojam Media, Inc., all rights reserved.# Author: Skip Montanaro## Copyright 1999, Bioreason, Inc., all rights reserved.# Author: Andrew Dalke## Copyright 1995-1997, Automatrix, Inc., all rights reserved.# Author: Skip Montanaro## Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved.### Permission to use, copy, modify, and distribute this Python software and# its associated documentation for any purpose without fee is hereby# granted, provided that the above copyright notice appears in all copies,# and that both that copyright notice and this permission notice appear in# supporting documentation, and that the name of neither Automatrix,# Bioreason or Mojam Media be used in advertising or publicity pertaining to# distribution of the software without specific, written prior permission.### Cleaned up the usage message --GvR 11/28/01## Summary of even more recent changes, --Zooko 2001-10-14# Used new `inspect' module for better (?) determination of file<->module# mappings, line numbers, and source code.# Used new local trace function for faster (and better?) operation.# Removed "speed hack", which, as far as I can tell, meant that it would# ignore all files ??? (When I tried it, it would ignore only *most* of my# files. In any case with the speed hack removed in favor of actually# calling `Ignore.names()', it ignores only those files that I told it to# ignore, so I am happy.)# Rolled the `Coverage' class into `Trace', which now does either tracing or# counting or both according to constructor flags.# Moved the construction of the `Ignore' object inside the constructor of# `Trace', simplifying usage.# Changed function `create_results_log()' into method# `CoverageResults.write_results()'.# Add new mode "countfuncs" which is faster and which just reports which# functions were invoked.# Made `write_results' create `coverdir' if it doesn't already exist.# Moved the `run' funcs into `Trace' for simpler usage.# Use pickle instead of marshal for persistence.## Summary of recent changes:# Support for files with the same basename (submodules in packages)# Expanded the idea of how to ignore files or modules# Split tracing and counting into different classes# Extracted count information and reporting from the count class# Added some ability to detect which missing lines could be executed# Added pseudo-pragma to prohibit complaining about unexecuted lines# Rewrote the main program# Summary of older changes:# Added run-time display of statements being executed# Incorporated portability and performance fixes from Greg Stein# Incorporated main program from Michael Scharf"""program/module to trace Python program or function executionSample use, command line: trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs trace.py -t --ignore-dir '$prefix' spam.py eggsSample use, programmatically # create a Trace object, telling it what to ignore, and whether to do tracing # or line-counting or both. trace = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, count=1) # run the new command using the given trace trace.run(coverage.globaltrace, 'main()') # make a report, telling it where you want output trace.print_results(show_missing=1)"""import sys, os, string, tempfile, types, copy, operator, inspect, exceptions, marshaltry: import cPickle pickle = cPickleexcept ImportError: import pickletrue = 1false = None# DEBUG_MODE=1 # make this true to get printouts which help you understand what's going ondef usage(outfile): outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]Meta-options:--help Display this help then exit.--version Output version information then exit.Otherwise, exactly one of the following three options must be given:-t, --trace Print each line to sys.stdout before it is executed.-c, --count Count the number of times each line is executed and write the counts to <module>.cover for each module executed, in the module's directory. See also `--coverdir', `--file', `--no-report' below.-r, --report Generate a report from a counts file; do not execute any code. `--file' must specify the results file to read, which must have been created in a previous run with `--count --file=FILE'.Modifiers:-f, --file=<file> File to accumulate counts over several runs.-R, --no-report Do not generate the coverage report files. Useful if you want to accumulate over several runs.-C, --coverdir=<dir> Directory where the report files. The coverage report for <package>.<module> is written to file <dir>/<package>/<module>.cover.-m, --missing Annotate executable lines that were not executed with '>>>>>> '.-s, --summary Write a brief summary on stdout for each file. (Can only be used with --count or --report.)Filters, may be repeated multiple times:--ignore-module=<mod> Ignore the given module and its submodules (if it is a package).--ignore-dir=<dir> Ignore files in the given directory (multiple directories can be joined by os.pathsep).""" % sys.argv[0])class Ignore: def __init__(self, modules = None, dirs = None): self._mods = modules or [] self._dirs = dirs or [] self._dirs = map(os.path.normpath, self._dirs) self._ignore = { '<string>': 1 } def names(self, filename, modulename): if self._ignore.has_key(modulename): return self._ignore[modulename] # haven't seen this one before, so see if the module name is # on the ignore list. Need to take some care since ignoring # "cmp" musn't mean ignoring "cmpcache" but ignoring # "Spam" must also mean ignoring "Spam.Eggs". for mod in self._mods: if mod == modulename: # Identical names, so ignore self._ignore[modulename] = 1 return 1 # check if the module is a proper submodule of something on # the ignore list n = len(mod) # (will not overflow since if the first n characters are the # same and the name has not already occured, then the size # of "name" is greater than that of "mod") if mod == modulename[:n] and modulename[n] == '.': self._ignore[modulename] = 1 return 1 # Now check that __file__ isn't in one of the directories if filename is None: # must be a built-in, so we must ignore self._ignore[modulename] = 1 return 1 # Ignore a file when it contains one of the ignorable paths for d in self._dirs: # The '+ os.sep' is to ensure that d is a parent directory, # as compared to cases like: # d = "/usr/local" # filename = "/usr/local.py" # or # d = "/usr/local.py" # filename = "/usr/local.py" if string.find(filename, d + os.sep) == 0: self._ignore[modulename] = 1 return 1 # Tried the different ways, so we don't ignore this module self._ignore[modulename] = 0 return 0class CoverageResults: def __init__(self, counts=None, calledfuncs=None, infile=None, outfile=None): self.counts = counts if self.counts is None: self.counts = {} self.counter = self.counts.copy() # map (filename, lineno) to count self.calledfuncs = calledfuncs if self.calledfuncs is None: self.calledfuncs = {} self.calledfuncs = self.calledfuncs.copy() self.infile = infile self.outfile = outfile if self.infile: # try and merge existing counts file try: thingie = pickle.load(open(self.infile, 'r')) if type(thingie) is types.DictType: # backwards compatibility for old trace.py after Zooko touched it but before calledfuncs --Zooko 2001-10-24 self.update(self.__class__(thingie)) elif type(thingie) is types.TupleType and len(thingie) == 2: (counts, calledfuncs,) = thingie self.update(self.__class__(counts, calledfuncs)) except (IOError, EOFError,): pass except pickle.UnpicklingError: # backwards compatibility for old trace.py before Zooko touched it --Zooko 2001-10-24 self.update(self.__class__(marshal.load(open(self.infile)))) def update(self, other): """Merge in the data from another CoverageResults""" counts = self.counts calledfuncs = self.calledfuncs other_counts = other.counts other_calledfuncs = other.calledfuncs for key in other_counts.keys(): if key != 'calledfuncs': # backwards compatibility for abortive attempt to stuff calledfuncs into self.counts, by Zooko --Zooko 2001-10-24 counts[key] = counts.get(key, 0) + other_counts[key] for key in other_calledfuncs.keys(): calledfuncs[key] = 1 def write_results(self, show_missing = 1, summary = 0, coverdir = None): """ @param coverdir """ for (filename, modulename, funcname,) in self.calledfuncs.keys(): print "filename: %s, modulename: %s, funcname: %s" % (filename, modulename, funcname,) import re # turn the counts data ("(filename, lineno) = count") into something # accessible on a per-file basis per_file = {} for thingie in self.counts.keys(): if thingie != "calledfuncs": # backwards compatibility for abortive attempt to stuff calledfuncs into self.counts, by Zooko --Zooko 2001-10-24 (filename, lineno,) = thingie lines_hit = per_file[filename] = per_file.get(filename, {})
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -