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