Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # portions copyright 2001, Autonomous Zones Industries, Inc., all rights... |
| 4 | # err... reserved and offered to the public under the terms of the |
| 5 | # Python 2.2 license. |
| 6 | # Author: Zooko O'Whielacronx |
| 7 | # http://zooko.com/ |
| 8 | # mailto:zooko@zooko.com |
| 9 | # |
| 10 | # Copyright 2000, Mojam Media, Inc., all rights reserved. |
| 11 | # Author: Skip Montanaro |
| 12 | # |
| 13 | # Copyright 1999, Bioreason, Inc., all rights reserved. |
| 14 | # Author: Andrew Dalke |
| 15 | # |
| 16 | # Copyright 1995-1997, Automatrix, Inc., all rights reserved. |
| 17 | # Author: Skip Montanaro |
| 18 | # |
| 19 | # Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved. |
| 20 | # |
| 21 | # |
| 22 | # Permission to use, copy, modify, and distribute this Python software and |
| 23 | # its associated documentation for any purpose without fee is hereby |
| 24 | # granted, provided that the above copyright notice appears in all copies, |
| 25 | # and that both that copyright notice and this permission notice appear in |
| 26 | # supporting documentation, and that the name of neither Automatrix, |
| 27 | # Bioreason or Mojam Media be used in advertising or publicity pertaining to |
| 28 | # distribution of the software without specific, written prior permission. |
| 29 | # |
| 30 | """program/module to trace Python program or function execution |
| 31 | |
| 32 | Sample use, command line: |
| 33 | trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs |
| 34 | trace.py -t --ignore-dir '$prefix' spam.py eggs |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 35 | trace.py --trackcalls spam.py eggs |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 36 | |
| 37 | Sample use, programmatically |
Skip Montanaro | 7b1559a | 2006-04-23 19:32:14 +0000 | [diff] [blame] | 38 | import sys |
| 39 | |
| 40 | # create a Trace object, telling it what to ignore, and whether to |
| 41 | # do tracing or line-counting or both. |
| 42 | tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, |
Tim Peters | be635cd | 2006-04-24 22:45:13 +0000 | [diff] [blame] | 43 | count=1) |
Skip Montanaro | 7b1559a | 2006-04-23 19:32:14 +0000 | [diff] [blame] | 44 | # run the new command using the given tracer |
| 45 | tracer.run('main()') |
| 46 | # make a report, placing output in /tmp |
| 47 | r = tracer.results() |
| 48 | r.write_results(show_missing=True, coverdir="/tmp") |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 49 | """ |
| 50 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 51 | import linecache |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 52 | import os |
| 53 | import re |
| 54 | import sys |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 55 | import threading |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 56 | import token |
| 57 | import tokenize |
| 58 | import types |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 59 | import gc |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 60 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 61 | try: |
| 62 | import cPickle |
| 63 | pickle = cPickle |
| 64 | except ImportError: |
| 65 | import pickle |
| 66 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 67 | def usage(outfile): |
| 68 | outfile.write("""Usage: %s [OPTIONS] <file> [ARGS] |
| 69 | |
| 70 | Meta-options: |
| 71 | --help Display this help then exit. |
| 72 | --version Output version information then exit. |
| 73 | |
| 74 | Otherwise, exactly one of the following three options must be given: |
| 75 | -t, --trace Print each line to sys.stdout before it is executed. |
| 76 | -c, --count Count the number of times each line is executed |
| 77 | and write the counts to <module>.cover for each |
| 78 | module executed, in the module's directory. |
| 79 | See also `--coverdir', `--file', `--no-report' below. |
Skip Montanaro | a7b8ac6 | 2003-06-27 19:09:33 +0000 | [diff] [blame] | 80 | -l, --listfuncs Keep track of which functions are executed at least |
Fred Drake | 01c623b | 2003-06-27 19:22:11 +0000 | [diff] [blame] | 81 | once and write the results to sys.stdout after the |
Skip Montanaro | a7b8ac6 | 2003-06-27 19:09:33 +0000 | [diff] [blame] | 82 | program exits. |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 83 | -T, --trackcalls Keep track of caller/called pairs and write the |
| 84 | results to sys.stdout after the program exits. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 85 | -r, --report Generate a report from a counts file; do not execute |
| 86 | any code. `--file' must specify the results file to |
| 87 | read, which must have been created in a previous run |
| 88 | with `--count --file=FILE'. |
| 89 | |
| 90 | Modifiers: |
| 91 | -f, --file=<file> File to accumulate counts over several runs. |
| 92 | -R, --no-report Do not generate the coverage report files. |
| 93 | Useful if you want to accumulate over several runs. |
| 94 | -C, --coverdir=<dir> Directory where the report files. The coverage |
| 95 | report for <package>.<module> is written to file |
| 96 | <dir>/<package>/<module>.cover. |
| 97 | -m, --missing Annotate executable lines that were not executed |
| 98 | with '>>>>>> '. |
| 99 | -s, --summary Write a brief summary on stdout for each file. |
| 100 | (Can only be used with --count or --report.) |
| 101 | |
| 102 | Filters, may be repeated multiple times: |
| 103 | --ignore-module=<mod> Ignore the given module and its submodules |
| 104 | (if it is a package). |
| 105 | --ignore-dir=<dir> Ignore files in the given directory (multiple |
| 106 | directories can be joined by os.pathsep). |
| 107 | """ % sys.argv[0]) |
| 108 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 109 | PRAGMA_NOCOVER = "#pragma NO COVER" |
| 110 | |
| 111 | # Simple rx to find lines with no code. |
| 112 | rx_blank = re.compile(r'^\s*(#.*)?$') |
| 113 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 114 | class Ignore: |
| 115 | def __init__(self, modules = None, dirs = None): |
| 116 | self._mods = modules or [] |
| 117 | self._dirs = dirs or [] |
| 118 | |
| 119 | self._dirs = map(os.path.normpath, self._dirs) |
| 120 | self._ignore = { '<string>': 1 } |
| 121 | |
| 122 | def names(self, filename, modulename): |
| 123 | if self._ignore.has_key(modulename): |
| 124 | return self._ignore[modulename] |
| 125 | |
| 126 | # haven't seen this one before, so see if the module name is |
| 127 | # on the ignore list. Need to take some care since ignoring |
| 128 | # "cmp" musn't mean ignoring "cmpcache" but ignoring |
| 129 | # "Spam" must also mean ignoring "Spam.Eggs". |
| 130 | for mod in self._mods: |
| 131 | if mod == modulename: # Identical names, so ignore |
| 132 | self._ignore[modulename] = 1 |
| 133 | return 1 |
| 134 | # check if the module is a proper submodule of something on |
| 135 | # the ignore list |
| 136 | n = len(mod) |
| 137 | # (will not overflow since if the first n characters are the |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 138 | # same and the name has not already occurred, then the size |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 139 | # of "name" is greater than that of "mod") |
| 140 | if mod == modulename[:n] and modulename[n] == '.': |
| 141 | self._ignore[modulename] = 1 |
| 142 | return 1 |
| 143 | |
| 144 | # Now check that __file__ isn't in one of the directories |
| 145 | if filename is None: |
| 146 | # must be a built-in, so we must ignore |
| 147 | self._ignore[modulename] = 1 |
| 148 | return 1 |
| 149 | |
| 150 | # Ignore a file when it contains one of the ignorable paths |
| 151 | for d in self._dirs: |
| 152 | # The '+ os.sep' is to ensure that d is a parent directory, |
| 153 | # as compared to cases like: |
| 154 | # d = "/usr/local" |
| 155 | # filename = "/usr/local.py" |
| 156 | # or |
| 157 | # d = "/usr/local.py" |
| 158 | # filename = "/usr/local.py" |
| 159 | if filename.startswith(d + os.sep): |
| 160 | self._ignore[modulename] = 1 |
| 161 | return 1 |
| 162 | |
| 163 | # Tried the different ways, so we don't ignore this module |
| 164 | self._ignore[modulename] = 0 |
| 165 | return 0 |
| 166 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 167 | def modname(path): |
| 168 | """Return a plausible module name for the patch.""" |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 169 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 170 | base = os.path.basename(path) |
| 171 | filename, ext = os.path.splitext(base) |
| 172 | return filename |
| 173 | |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 174 | def fullmodname(path): |
Jeremy Hylton | c8c8b94 | 2003-04-22 15:35:51 +0000 | [diff] [blame] | 175 | """Return a plausible module name for the path.""" |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 176 | |
| 177 | # If the file 'path' is part of a package, then the filename isn't |
| 178 | # enough to uniquely identify it. Try to do the right thing by |
| 179 | # looking in sys.path for the longest matching prefix. We'll |
| 180 | # assume that the rest is the package name. |
| 181 | |
| 182 | longest = "" |
| 183 | for dir in sys.path: |
| 184 | if path.startswith(dir) and path[len(dir)] == os.path.sep: |
| 185 | if len(dir) > len(longest): |
| 186 | longest = dir |
| 187 | |
Guido van Rossum | b427c00 | 2003-10-10 23:02:01 +0000 | [diff] [blame] | 188 | if longest: |
| 189 | base = path[len(longest) + 1:] |
| 190 | else: |
| 191 | base = path |
Guido van Rossum | bbca8da | 2004-02-19 19:16:50 +0000 | [diff] [blame] | 192 | base = base.replace(os.sep, ".") |
| 193 | if os.altsep: |
| 194 | base = base.replace(os.altsep, ".") |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 195 | filename, ext = os.path.splitext(base) |
| 196 | return filename |
| 197 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 198 | class CoverageResults: |
| 199 | def __init__(self, counts=None, calledfuncs=None, infile=None, |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 200 | callers=None, outfile=None): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 201 | self.counts = counts |
| 202 | if self.counts is None: |
| 203 | self.counts = {} |
| 204 | self.counter = self.counts.copy() # map (filename, lineno) to count |
| 205 | self.calledfuncs = calledfuncs |
| 206 | if self.calledfuncs is None: |
| 207 | self.calledfuncs = {} |
| 208 | self.calledfuncs = self.calledfuncs.copy() |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 209 | self.callers = callers |
| 210 | if self.callers is None: |
| 211 | self.callers = {} |
| 212 | self.callers = self.callers.copy() |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 213 | self.infile = infile |
| 214 | self.outfile = outfile |
| 215 | if self.infile: |
Jeremy Hylton | d7ce86d | 2003-07-07 16:08:47 +0000 | [diff] [blame] | 216 | # Try to merge existing counts file. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 217 | try: |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 218 | counts, calledfuncs, callers = \ |
| 219 | pickle.load(open(self.infile, 'rb')) |
| 220 | self.update(self.__class__(counts, calledfuncs, callers)) |
Jeremy Hylton | d7ce86d | 2003-07-07 16:08:47 +0000 | [diff] [blame] | 221 | except (IOError, EOFError, ValueError), err: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 222 | print >> sys.stderr, ("Skipping counts file %r: %s" |
| 223 | % (self.infile, err)) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 224 | |
| 225 | def update(self, other): |
| 226 | """Merge in the data from another CoverageResults""" |
| 227 | counts = self.counts |
| 228 | calledfuncs = self.calledfuncs |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 229 | callers = self.callers |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 230 | other_counts = other.counts |
| 231 | other_calledfuncs = other.calledfuncs |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 232 | other_callers = other.callers |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 233 | |
| 234 | for key in other_counts.keys(): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 235 | counts[key] = counts.get(key, 0) + other_counts[key] |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 236 | |
| 237 | for key in other_calledfuncs.keys(): |
| 238 | calledfuncs[key] = 1 |
| 239 | |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 240 | for key in other_callers.keys(): |
| 241 | callers[key] = 1 |
| 242 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 243 | def write_results(self, show_missing=True, summary=False, coverdir=None): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 244 | """ |
| 245 | @param coverdir |
| 246 | """ |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 247 | if self.calledfuncs: |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 248 | print |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 249 | print "functions called:" |
| 250 | calls = self.calledfuncs.keys() |
| 251 | calls.sort() |
| 252 | for filename, modulename, funcname in calls: |
| 253 | print ("filename: %s, modulename: %s, funcname: %s" |
| 254 | % (filename, modulename, funcname)) |
| 255 | |
| 256 | if self.callers: |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 257 | print |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 258 | print "calling relationships:" |
| 259 | calls = self.callers.keys() |
| 260 | calls.sort() |
| 261 | lastfile = lastcfile = "" |
| 262 | for ((pfile, pmod, pfunc), (cfile, cmod, cfunc)) in calls: |
| 263 | if pfile != lastfile: |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 264 | print |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 265 | print "***", pfile, "***" |
| 266 | lastfile = pfile |
| 267 | lastcfile = "" |
| 268 | if cfile != pfile and lastcfile != cfile: |
| 269 | print " -->", cfile |
| 270 | lastcfile = cfile |
| 271 | print " %s.%s -> %s.%s" % (pmod, pfunc, cmod, cfunc) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 272 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 273 | # turn the counts data ("(filename, lineno) = count") into something |
| 274 | # accessible on a per-file basis |
| 275 | per_file = {} |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 276 | for filename, lineno in self.counts.keys(): |
| 277 | lines_hit = per_file[filename] = per_file.get(filename, {}) |
| 278 | lines_hit[lineno] = self.counts[(filename, lineno)] |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 279 | |
| 280 | # accumulate summary info, if needed |
| 281 | sums = {} |
| 282 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 283 | for filename, count in per_file.iteritems(): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 284 | # skip some "files" we don't care about... |
| 285 | if filename == "<string>": |
| 286 | continue |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 287 | |
Georg Brandl | b2afe85 | 2006-06-09 20:43:48 +0000 | [diff] [blame] | 288 | if filename.endswith((".pyc", ".pyo")): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 289 | filename = filename[:-1] |
| 290 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 291 | if coverdir is None: |
| 292 | dir = os.path.dirname(os.path.abspath(filename)) |
Jeremy Hylton | c8c8b94 | 2003-04-22 15:35:51 +0000 | [diff] [blame] | 293 | modulename = modname(filename) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 294 | else: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 295 | dir = coverdir |
| 296 | if not os.path.exists(dir): |
| 297 | os.makedirs(dir) |
Jeremy Hylton | c8c8b94 | 2003-04-22 15:35:51 +0000 | [diff] [blame] | 298 | modulename = fullmodname(filename) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 299 | |
| 300 | # If desired, get a list of the line numbers which represent |
| 301 | # executable content (returned as a dict for better lookup speed) |
| 302 | if show_missing: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 303 | lnotab = find_executable_linenos(filename) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 304 | else: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 305 | lnotab = {} |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 306 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 307 | source = linecache.getlines(filename) |
| 308 | coverpath = os.path.join(dir, modulename + ".cover") |
| 309 | n_hits, n_lines = self.write_results_file(coverpath, source, |
| 310 | lnotab, count) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 311 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 312 | if summary and n_lines: |
| 313 | percent = int(100 * n_hits / n_lines) |
| 314 | sums[modulename] = n_lines, percent, modulename, filename |
| 315 | |
| 316 | if summary and sums: |
| 317 | mods = sums.keys() |
| 318 | mods.sort() |
| 319 | print "lines cov% module (path)" |
| 320 | for m in mods: |
| 321 | n_lines, percent, modulename, filename = sums[m] |
| 322 | print "%5d %3d%% %s (%s)" % sums[m] |
| 323 | |
| 324 | if self.outfile: |
| 325 | # try and store counts and module info into self.outfile |
| 326 | try: |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 327 | pickle.dump((self.counts, self.calledfuncs, self.callers), |
Jeremy Hylton | d0e2705 | 2003-10-14 20:12:06 +0000 | [diff] [blame] | 328 | open(self.outfile, 'wb'), 1) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 329 | except IOError, err: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 330 | print >> sys.stderr, "Can't save counts files because %s" % err |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 331 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 332 | def write_results_file(self, path, lines, lnotab, lines_hit): |
| 333 | """Return a coverage results file in path.""" |
| 334 | |
| 335 | try: |
| 336 | outfile = open(path, "w") |
| 337 | except IOError, err: |
| 338 | print >> sys.stderr, ("trace: Could not open %r for writing: %s" |
| 339 | "- skipping" % (path, err)) |
Guido van Rossum | bbca8da | 2004-02-19 19:16:50 +0000 | [diff] [blame] | 340 | return 0, 0 |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 341 | |
| 342 | n_lines = 0 |
| 343 | n_hits = 0 |
| 344 | for i, line in enumerate(lines): |
| 345 | lineno = i + 1 |
| 346 | # do the blank/comment match to try to mark more lines |
| 347 | # (help the reader find stuff that hasn't been covered) |
| 348 | if lineno in lines_hit: |
| 349 | outfile.write("%5d: " % lines_hit[lineno]) |
| 350 | n_hits += 1 |
| 351 | n_lines += 1 |
| 352 | elif rx_blank.match(line): |
Walter Dörwald | c171172 | 2003-07-15 10:34:02 +0000 | [diff] [blame] | 353 | outfile.write(" ") |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 354 | else: |
| 355 | # lines preceded by no marks weren't hit |
| 356 | # Highlight them if so indicated, unless the line contains |
| 357 | # #pragma: NO COVER |
| 358 | if lineno in lnotab and not PRAGMA_NOCOVER in lines[i]: |
| 359 | outfile.write(">>>>>> ") |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 360 | n_lines += 1 |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 361 | else: |
| 362 | outfile.write(" ") |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 363 | outfile.write(lines[i].expandtabs(8)) |
| 364 | outfile.close() |
| 365 | |
| 366 | return n_hits, n_lines |
| 367 | |
| 368 | def find_lines_from_code(code, strs): |
| 369 | """Return dict where keys are lines in the line number table.""" |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 370 | linenos = {} |
| 371 | |
| 372 | line_increments = [ord(c) for c in code.co_lnotab[1::2]] |
| 373 | table_length = len(line_increments) |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 374 | docstring = False |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 375 | |
| 376 | lineno = code.co_firstlineno |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 377 | for li in line_increments: |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 378 | lineno += li |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 379 | if lineno not in strs: |
| 380 | linenos[lineno] = 1 |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 381 | |
| 382 | return linenos |
| 383 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 384 | def find_lines(code, strs): |
| 385 | """Return lineno dict for all code objects reachable from code.""" |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 386 | # get all of the lineno information from the code of this scope level |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 387 | linenos = find_lines_from_code(code, strs) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 388 | |
| 389 | # and check the constants for references to other code objects |
| 390 | for c in code.co_consts: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 391 | if isinstance(c, types.CodeType): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 392 | # find another code object, so recurse into it |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 393 | linenos.update(find_lines(c, strs)) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 394 | return linenos |
| 395 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 396 | def find_strings(filename): |
| 397 | """Return a dict of possible docstring positions. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 398 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 399 | The dict maps line numbers to strings. There is an entry for |
| 400 | line that contains only a string or a part of a triple-quoted |
| 401 | string. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 402 | """ |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 403 | d = {} |
| 404 | # If the first token is a string, then it's the module docstring. |
| 405 | # Add this special case so that the test in the loop passes. |
| 406 | prev_ttype = token.INDENT |
| 407 | f = open(filename) |
| 408 | for ttype, tstr, start, end, line in tokenize.generate_tokens(f.readline): |
| 409 | if ttype == token.STRING: |
| 410 | if prev_ttype == token.INDENT: |
| 411 | sline, scol = start |
| 412 | eline, ecol = end |
| 413 | for i in range(sline, eline + 1): |
| 414 | d[i] = 1 |
| 415 | prev_ttype = ttype |
| 416 | f.close() |
| 417 | return d |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 418 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 419 | def find_executable_linenos(filename): |
| 420 | """Return dict where keys are line numbers in the line number table.""" |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 421 | try: |
Skip Montanaro | c00fc84 | 2004-04-16 03:28:19 +0000 | [diff] [blame] | 422 | prog = open(filename, "rU").read() |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 423 | except IOError, err: |
| 424 | print >> sys.stderr, ("Not printing coverage data for %r: %s" |
| 425 | % (filename, err)) |
| 426 | return {} |
| 427 | code = compile(prog, filename, "exec") |
| 428 | strs = find_strings(filename) |
| 429 | return find_lines(code, strs) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 430 | |
| 431 | class Trace: |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 432 | def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0, |
| 433 | ignoremods=(), ignoredirs=(), infile=None, outfile=None): |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 434 | """ |
| 435 | @param count true iff it should count number of times each |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 436 | line is executed |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 437 | @param trace true iff it should print out each line that is |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 438 | being counted |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 439 | @param countfuncs true iff it should just output a list of |
| 440 | (filename, modulename, funcname,) for functions |
| 441 | that were called at least once; This overrides |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 442 | `count' and `trace' |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 443 | @param ignoremods a list of the names of modules to ignore |
| 444 | @param ignoredirs a list of the names of directories to ignore |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 445 | all of the (recursive) contents of |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 446 | @param infile file from which to read stored counts to be |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 447 | added into the results |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 448 | @param outfile file in which to write the results |
| 449 | """ |
| 450 | self.infile = infile |
| 451 | self.outfile = outfile |
| 452 | self.ignore = Ignore(ignoremods, ignoredirs) |
| 453 | self.counts = {} # keys are (filename, linenumber) |
| 454 | self.blabbed = {} # for debugging |
| 455 | self.pathtobasename = {} # for memoizing os.path.basename |
| 456 | self.donothing = 0 |
| 457 | self.trace = trace |
| 458 | self._calledfuncs = {} |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 459 | self._callers = {} |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 460 | self._caller_cache = {} |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 461 | if countcallers: |
| 462 | self.globaltrace = self.globaltrace_trackcallers |
| 463 | elif countfuncs: |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 464 | self.globaltrace = self.globaltrace_countfuncs |
| 465 | elif trace and count: |
| 466 | self.globaltrace = self.globaltrace_lt |
| 467 | self.localtrace = self.localtrace_trace_and_count |
| 468 | elif trace: |
| 469 | self.globaltrace = self.globaltrace_lt |
| 470 | self.localtrace = self.localtrace_trace |
| 471 | elif count: |
| 472 | self.globaltrace = self.globaltrace_lt |
| 473 | self.localtrace = self.localtrace_count |
| 474 | else: |
| 475 | # Ahem -- do nothing? Okay. |
| 476 | self.donothing = 1 |
| 477 | |
| 478 | def run(self, cmd): |
| 479 | import __main__ |
| 480 | dict = __main__.__dict__ |
| 481 | if not self.donothing: |
| 482 | sys.settrace(self.globaltrace) |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 483 | threading.settrace(self.globaltrace) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 484 | try: |
| 485 | exec cmd in dict, dict |
| 486 | finally: |
| 487 | if not self.donothing: |
| 488 | sys.settrace(None) |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 489 | threading.settrace(None) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 490 | |
| 491 | def runctx(self, cmd, globals=None, locals=None): |
| 492 | if globals is None: globals = {} |
| 493 | if locals is None: locals = {} |
| 494 | if not self.donothing: |
| 495 | sys.settrace(self.globaltrace) |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 496 | threading.settrace(self.globaltrace) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 497 | try: |
| 498 | exec cmd in globals, locals |
| 499 | finally: |
| 500 | if not self.donothing: |
| 501 | sys.settrace(None) |
Jeremy Hylton | 546e34b | 2003-06-26 14:56:17 +0000 | [diff] [blame] | 502 | threading.settrace(None) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 503 | |
| 504 | def runfunc(self, func, *args, **kw): |
| 505 | result = None |
| 506 | if not self.donothing: |
| 507 | sys.settrace(self.globaltrace) |
| 508 | try: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 509 | result = func(*args, **kw) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 510 | finally: |
| 511 | if not self.donothing: |
| 512 | sys.settrace(None) |
| 513 | return result |
| 514 | |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 515 | def file_module_function_of(self, frame): |
| 516 | code = frame.f_code |
| 517 | filename = code.co_filename |
| 518 | if filename: |
| 519 | modulename = modname(filename) |
| 520 | else: |
| 521 | modulename = None |
| 522 | |
| 523 | funcname = code.co_name |
| 524 | clsname = None |
| 525 | if code in self._caller_cache: |
| 526 | if self._caller_cache[code] is not None: |
| 527 | clsname = self._caller_cache[code] |
| 528 | else: |
| 529 | self._caller_cache[code] = None |
| 530 | ## use of gc.get_referrers() was suggested by Michael Hudson |
| 531 | # all functions which refer to this code object |
| 532 | funcs = [f for f in gc.get_referrers(code) |
| 533 | if hasattr(f, "func_doc")] |
| 534 | # require len(func) == 1 to avoid ambiguity caused by calls to |
| 535 | # new.function(): "In the face of ambiguity, refuse the |
| 536 | # temptation to guess." |
| 537 | if len(funcs) == 1: |
| 538 | dicts = [d for d in gc.get_referrers(funcs[0]) |
| 539 | if isinstance(d, dict)] |
| 540 | if len(dicts) == 1: |
| 541 | classes = [c for c in gc.get_referrers(dicts[0]) |
| 542 | if hasattr(c, "__bases__")] |
| 543 | if len(classes) == 1: |
| 544 | # ditto for new.classobj() |
| 545 | clsname = str(classes[0]) |
| 546 | # cache the result - assumption is that new.* is |
| 547 | # not called later to disturb this relationship |
| 548 | # _caller_cache could be flushed if functions in |
| 549 | # the new module get called. |
| 550 | self._caller_cache[code] = clsname |
| 551 | if clsname is not None: |
| 552 | # final hack - module name shows up in str(cls), but we've already |
| 553 | # computed module name, so remove it |
| 554 | clsname = clsname.split(".")[1:] |
| 555 | clsname = ".".join(clsname) |
| 556 | funcname = "%s.%s" % (clsname, funcname) |
| 557 | |
| 558 | return filename, modulename, funcname |
| 559 | |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 560 | def globaltrace_trackcallers(self, frame, why, arg): |
| 561 | """Handler for call events. |
| 562 | |
| 563 | Adds information about who called who to the self._callers dict. |
| 564 | """ |
| 565 | if why == 'call': |
| 566 | # XXX Should do a better job of identifying methods |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 567 | this_func = self.file_module_function_of(frame) |
| 568 | parent_func = self.file_module_function_of(frame.f_back) |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 569 | self._callers[(parent_func, this_func)] = 1 |
| 570 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 571 | def globaltrace_countfuncs(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 572 | """Handler for call events. |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 573 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 574 | Adds (filename, modulename, funcname) to the self._calledfuncs dict. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 575 | """ |
| 576 | if why == 'call': |
Skip Montanaro | 5bfd984 | 2004-04-10 16:29:58 +0000 | [diff] [blame] | 577 | this_func = self.file_module_function_of(frame) |
| 578 | self._calledfuncs[this_func] = 1 |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 579 | |
| 580 | def globaltrace_lt(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 581 | """Handler for call events. |
| 582 | |
| 583 | If the code block being entered is to be ignored, returns `None', |
| 584 | else returns self.localtrace. |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 585 | """ |
| 586 | if why == 'call': |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 587 | code = frame.f_code |
| 588 | filename = code.co_filename |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 589 | if filename: |
Jeremy Hylton | dfbfe73 | 2003-04-21 22:49:17 +0000 | [diff] [blame] | 590 | # XXX modname() doesn't work right for packages, so |
| 591 | # the ignore support won't work right for packages |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 592 | modulename = modname(filename) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 593 | if modulename is not None: |
| 594 | ignore_it = self.ignore.names(filename, modulename) |
| 595 | if not ignore_it: |
| 596 | if self.trace: |
| 597 | print (" --- modulename: %s, funcname: %s" |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 598 | % (modulename, code.co_name)) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 599 | return self.localtrace |
| 600 | else: |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 601 | return None |
| 602 | |
| 603 | def localtrace_trace_and_count(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 604 | if why == "line": |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 605 | # record the file name and line number of every trace |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 606 | filename = frame.f_code.co_filename |
| 607 | lineno = frame.f_lineno |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 608 | key = filename, lineno |
| 609 | self.counts[key] = self.counts.get(key, 0) + 1 |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 610 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 611 | bname = os.path.basename(filename) |
| 612 | print "%s(%d): %s" % (bname, lineno, |
| 613 | linecache.getline(filename, lineno)), |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 614 | return self.localtrace |
| 615 | |
| 616 | def localtrace_trace(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 617 | if why == "line": |
| 618 | # record the file name and line number of every trace |
| 619 | filename = frame.f_code.co_filename |
| 620 | lineno = frame.f_lineno |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 621 | |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 622 | bname = os.path.basename(filename) |
| 623 | print "%s(%d): %s" % (bname, lineno, |
| 624 | linecache.getline(filename, lineno)), |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 625 | return self.localtrace |
| 626 | |
| 627 | def localtrace_count(self, frame, why, arg): |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 628 | if why == "line": |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 629 | filename = frame.f_code.co_filename |
| 630 | lineno = frame.f_lineno |
| 631 | key = filename, lineno |
| 632 | self.counts[key] = self.counts.get(key, 0) + 1 |
| 633 | return self.localtrace |
| 634 | |
| 635 | def results(self): |
| 636 | return CoverageResults(self.counts, infile=self.infile, |
| 637 | outfile=self.outfile, |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 638 | calledfuncs=self._calledfuncs, |
| 639 | callers=self._callers) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 640 | |
| 641 | def _err_exit(msg): |
| 642 | sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
| 643 | sys.exit(1) |
| 644 | |
| 645 | def main(argv=None): |
| 646 | import getopt |
| 647 | |
| 648 | if argv is None: |
| 649 | argv = sys.argv |
| 650 | try: |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 651 | opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lT", |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 652 | ["help", "version", "trace", "count", |
| 653 | "report", "no-report", "summary", |
| 654 | "file=", "missing", |
| 655 | "ignore-module=", "ignore-dir=", |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 656 | "coverdir=", "listfuncs", |
| 657 | "trackcalls"]) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 658 | |
| 659 | except getopt.error, msg: |
| 660 | sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
| 661 | sys.stderr.write("Try `%s --help' for more information\n" |
| 662 | % sys.argv[0]) |
| 663 | sys.exit(1) |
| 664 | |
| 665 | trace = 0 |
| 666 | count = 0 |
| 667 | report = 0 |
| 668 | no_report = 0 |
| 669 | counts_file = None |
| 670 | missing = 0 |
| 671 | ignore_modules = [] |
| 672 | ignore_dirs = [] |
| 673 | coverdir = None |
| 674 | summary = 0 |
| 675 | listfuncs = False |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 676 | countcallers = False |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 677 | |
| 678 | for opt, val in opts: |
| 679 | if opt == "--help": |
| 680 | usage(sys.stdout) |
| 681 | sys.exit(0) |
| 682 | |
| 683 | if opt == "--version": |
| 684 | sys.stdout.write("trace 2.0\n") |
| 685 | sys.exit(0) |
| 686 | |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 687 | if opt == "-T" or opt == "--trackcalls": |
| 688 | countcallers = True |
| 689 | continue |
| 690 | |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 691 | if opt == "-l" or opt == "--listfuncs": |
| 692 | listfuncs = True |
| 693 | continue |
| 694 | |
| 695 | if opt == "-t" or opt == "--trace": |
| 696 | trace = 1 |
| 697 | continue |
| 698 | |
| 699 | if opt == "-c" or opt == "--count": |
| 700 | count = 1 |
| 701 | continue |
| 702 | |
| 703 | if opt == "-r" or opt == "--report": |
| 704 | report = 1 |
| 705 | continue |
| 706 | |
| 707 | if opt == "-R" or opt == "--no-report": |
| 708 | no_report = 1 |
| 709 | continue |
| 710 | |
| 711 | if opt == "-f" or opt == "--file": |
| 712 | counts_file = val |
| 713 | continue |
| 714 | |
| 715 | if opt == "-m" or opt == "--missing": |
| 716 | missing = 1 |
| 717 | continue |
| 718 | |
| 719 | if opt == "-C" or opt == "--coverdir": |
| 720 | coverdir = val |
| 721 | continue |
| 722 | |
| 723 | if opt == "-s" or opt == "--summary": |
| 724 | summary = 1 |
| 725 | continue |
| 726 | |
| 727 | if opt == "--ignore-module": |
| 728 | ignore_modules.append(val) |
| 729 | continue |
| 730 | |
| 731 | if opt == "--ignore-dir": |
| 732 | for s in val.split(os.pathsep): |
| 733 | s = os.path.expandvars(s) |
| 734 | # should I also call expanduser? (after all, could use $HOME) |
| 735 | |
| 736 | s = s.replace("$prefix", |
| 737 | os.path.join(sys.prefix, "lib", |
| 738 | "python" + sys.version[:3])) |
| 739 | s = s.replace("$exec_prefix", |
| 740 | os.path.join(sys.exec_prefix, "lib", |
| 741 | "python" + sys.version[:3])) |
| 742 | s = os.path.normpath(s) |
| 743 | ignore_dirs.append(s) |
| 744 | continue |
| 745 | |
| 746 | assert 0, "Should never get here" |
| 747 | |
| 748 | if listfuncs and (count or trace): |
| 749 | _err_exit("cannot specify both --listfuncs and (--trace or --count)") |
| 750 | |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 751 | if not (count or trace or report or listfuncs or countcallers): |
| 752 | _err_exit("must specify one of --trace, --count, --report, " |
| 753 | "--listfuncs, or --trackcalls") |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 754 | |
| 755 | if report and no_report: |
| 756 | _err_exit("cannot specify both --report and --no-report") |
| 757 | |
| 758 | if report and not counts_file: |
| 759 | _err_exit("--report requires a --file") |
| 760 | |
| 761 | if no_report and len(prog_argv) == 0: |
| 762 | _err_exit("missing name of file to run") |
| 763 | |
| 764 | # everything is ready |
| 765 | if report: |
| 766 | results = CoverageResults(infile=counts_file, outfile=counts_file) |
| 767 | results.write_results(missing, summary=summary, coverdir=coverdir) |
| 768 | else: |
| 769 | sys.argv = prog_argv |
| 770 | progname = prog_argv[0] |
| 771 | sys.path[0] = os.path.split(progname)[0] |
| 772 | |
| 773 | t = Trace(count, trace, countfuncs=listfuncs, |
Skip Montanaro | cafc811 | 2004-04-07 15:46:05 +0000 | [diff] [blame] | 774 | countcallers=countcallers, ignoremods=ignore_modules, |
| 775 | ignoredirs=ignore_dirs, infile=counts_file, |
| 776 | outfile=counts_file) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 777 | try: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 778 | t.run('execfile(%r)' % (progname,)) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 779 | except IOError, err: |
Jeremy Hylton | 38732e1 | 2003-04-21 22:04:46 +0000 | [diff] [blame] | 780 | _err_exit("Cannot run file %r because: %s" % (sys.argv[0], err)) |
Jeremy Hylton | 4edaa0d | 2003-02-18 15:06:17 +0000 | [diff] [blame] | 781 | except SystemExit: |
| 782 | pass |
| 783 | |
| 784 | results = t.results() |
| 785 | |
| 786 | if not no_report: |
| 787 | results.write_results(missing, summary=summary, coverdir=coverdir) |
| 788 | |
| 789 | if __name__=='__main__': |
| 790 | main() |