Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 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 | # |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 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 | # |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 30 | """program/module to trace Python program or function execution |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 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 |
| 35 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 36 | Sample use, programmatically |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 37 | # create a Trace object, telling it what to ignore, and whether to |
| 38 | # do tracing or line-counting or both. |
| 39 | trace = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, |
| 40 | count=1) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 41 | # run the new command using the given trace |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 42 | trace.run(coverage.globaltrace, 'main()') |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 43 | # make a report, telling it where you want output |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 44 | trace.print_results(show_missing=1) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 45 | """ |
| 46 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 47 | import sys, os, tempfile, types, copy, operator, inspect, exceptions, marshal |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 48 | try: |
| 49 | import cPickle |
| 50 | pickle = cPickle |
| 51 | except ImportError: |
| 52 | import pickle |
| 53 | |
| 54 | true = 1 |
| 55 | false = None |
| 56 | |
| 57 | # DEBUG_MODE=1 # make this true to get printouts which help you understand what's going on |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 58 | |
| 59 | def usage(outfile): |
| 60 | outfile.write("""Usage: %s [OPTIONS] <file> [ARGS] |
| 61 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 62 | Meta-options: |
| 63 | --help Display this help then exit. |
| 64 | --version Output version information then exit. |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 66 | Otherwise, exactly one of the following three options must be given: |
| 67 | -t, --trace Print each line to sys.stdout before it is executed. |
| 68 | -c, --count Count the number of times each line is executed |
| 69 | and write the counts to <module>.cover for each |
| 70 | module executed, in the module's directory. |
| 71 | See also `--coverdir', `--file', `--no-report' below. |
| 72 | -r, --report Generate a report from a counts file; do not execute |
| 73 | any code. `--file' must specify the results file to |
| 74 | read, which must have been created in a previous run |
| 75 | with `--count --file=FILE'. |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 77 | Modifiers: |
| 78 | -f, --file=<file> File to accumulate counts over several runs. |
| 79 | -R, --no-report Do not generate the coverage report files. |
| 80 | Useful if you want to accumulate over several runs. |
| 81 | -C, --coverdir=<dir> Directory where the report files. The coverage |
| 82 | report for <package>.<module> is written to file |
| 83 | <dir>/<package>/<module>.cover. |
| 84 | -m, --missing Annotate executable lines that were not executed |
| 85 | with '>>>>>> '. |
| 86 | -s, --summary Write a brief summary on stdout for each file. |
| 87 | (Can only be used with --count or --report.) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 89 | Filters, may be repeated multiple times: |
| 90 | --ignore-module=<mod> Ignore the given module and its submodules |
| 91 | (if it is a package). |
| 92 | --ignore-dir=<dir> Ignore files in the given directory (multiple |
| 93 | directories can be joined by os.pathsep). |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 94 | """ % sys.argv[0]) |
| 95 | |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 96 | class Ignore: |
| 97 | def __init__(self, modules = None, dirs = None): |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 98 | self._mods = modules or [] |
| 99 | self._dirs = dirs or [] |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 101 | self._dirs = map(os.path.normpath, self._dirs) |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 102 | self._ignore = { '<string>': 1 } |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 103 | |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 104 | def names(self, filename, modulename): |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 105 | if self._ignore.has_key(modulename): |
| 106 | return self._ignore[modulename] |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 107 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 108 | # haven't seen this one before, so see if the module name is |
| 109 | # on the ignore list. Need to take some care since ignoring |
| 110 | # "cmp" musn't mean ignoring "cmpcache" but ignoring |
| 111 | # "Spam" must also mean ignoring "Spam.Eggs". |
| 112 | for mod in self._mods: |
| 113 | if mod == modulename: # Identical names, so ignore |
| 114 | self._ignore[modulename] = 1 |
| 115 | return 1 |
| 116 | # check if the module is a proper submodule of something on |
| 117 | # the ignore list |
| 118 | n = len(mod) |
| 119 | # (will not overflow since if the first n characters are the |
| 120 | # same and the name has not already occured, then the size |
| 121 | # of "name" is greater than that of "mod") |
| 122 | if mod == modulename[:n] and modulename[n] == '.': |
| 123 | self._ignore[modulename] = 1 |
| 124 | return 1 |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 125 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 126 | # Now check that __file__ isn't in one of the directories |
| 127 | if filename is None: |
| 128 | # must be a built-in, so we must ignore |
| 129 | self._ignore[modulename] = 1 |
| 130 | return 1 |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 131 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 132 | # Ignore a file when it contains one of the ignorable paths |
| 133 | for d in self._dirs: |
| 134 | # The '+ os.sep' is to ensure that d is a parent directory, |
| 135 | # as compared to cases like: |
| 136 | # d = "/usr/local" |
| 137 | # filename = "/usr/local.py" |
| 138 | # or |
| 139 | # d = "/usr/local.py" |
| 140 | # filename = "/usr/local.py" |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 141 | if filename.startswith(d + os.sep): |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 142 | self._ignore[modulename] = 1 |
| 143 | return 1 |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 144 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 145 | # Tried the different ways, so we don't ignore this module |
| 146 | self._ignore[modulename] = 0 |
| 147 | return 0 |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 148 | |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 149 | class CoverageResults: |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 150 | def __init__(self, counts=None, calledfuncs=None, infile=None, outfile=None): |
| 151 | self.counts = counts |
| 152 | if self.counts is None: |
| 153 | self.counts = {} |
| 154 | self.counter = self.counts.copy() # map (filename, lineno) to count |
| 155 | self.calledfuncs = calledfuncs |
| 156 | if self.calledfuncs is None: |
| 157 | self.calledfuncs = {} |
| 158 | self.calledfuncs = self.calledfuncs.copy() |
| 159 | self.infile = infile |
| 160 | self.outfile = outfile |
| 161 | if self.infile: |
| 162 | # try and merge existing counts file |
| 163 | try: |
| 164 | thingie = pickle.load(open(self.infile, 'r')) |
| 165 | if type(thingie) is types.DictType: |
| 166 | # backwards compatibility for old trace.py after Zooko touched it but before calledfuncs --Zooko 2001-10-24 |
| 167 | self.update(self.__class__(thingie)) |
| 168 | elif type(thingie) is types.TupleType and len(thingie) == 2: |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 169 | counts, calledfuncs = thingie |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 170 | self.update(self.__class__(counts, calledfuncs)) |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 171 | except (IOError, EOFError): |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 172 | pass |
| 173 | except pickle.UnpicklingError: |
| 174 | # backwards compatibility for old trace.py before Zooko touched it --Zooko 2001-10-24 |
| 175 | self.update(self.__class__(marshal.load(open(self.infile)))) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 176 | |
| 177 | def update(self, other): |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 178 | """Merge in the data from another CoverageResults""" |
| 179 | counts = self.counts |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 180 | calledfuncs = self.calledfuncs |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 181 | other_counts = other.counts |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 182 | other_calledfuncs = other.calledfuncs |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 183 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 184 | for key in other_counts.keys(): |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 185 | if key != 'calledfuncs': # backwards compatibility for abortive attempt to stuff calledfuncs into self.counts, by Zooko --Zooko 2001-10-24 |
| 186 | counts[key] = counts.get(key, 0) + other_counts[key] |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 188 | for key in other_calledfuncs.keys(): |
| 189 | calledfuncs[key] = 1 |
| 190 | |
| 191 | def write_results(self, show_missing = 1, summary = 0, coverdir = None): |
| 192 | """ |
| 193 | @param coverdir |
| 194 | """ |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 195 | for filename, modulename, funcname in self.calledfuncs.keys(): |
| 196 | print ("filename: %s, modulename: %s, funcname: %s" |
| 197 | % (filename, modulename, funcname)) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 198 | |
| 199 | import re |
| 200 | # turn the counts data ("(filename, lineno) = count") into something |
| 201 | # accessible on a per-file basis |
| 202 | per_file = {} |
| 203 | for thingie in self.counts.keys(): |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 204 | if thingie != "calledfuncs": |
| 205 | # backwards compatibility for abortive attempt to |
| 206 | # stuff calledfuncs into self.counts, by Zooko --Zooko |
| 207 | # 2001-10-24 |
| 208 | filename, lineno = thingie |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 209 | lines_hit = per_file[filename] = per_file.get(filename, {}) |
| 210 | lines_hit[lineno] = self.counts[(filename, lineno)] |
| 211 | |
| 212 | # there are many places where this is insufficient, like a blank |
| 213 | # line embedded in a multiline string. |
| 214 | blank = re.compile(r'^\s*(#.*)?$') |
| 215 | |
| 216 | # accumulate summary info, if needed |
| 217 | sums = {} |
| 218 | |
| 219 | # generate file paths for the coverage files we are going to write... |
| 220 | fnlist = [] |
| 221 | tfdir = tempfile.gettempdir() |
| 222 | for key in per_file.keys(): |
| 223 | filename = key |
| 224 | |
| 225 | # skip some "files" we don't care about... |
| 226 | if filename == "<string>": |
| 227 | continue |
| 228 | # are these caused by code compiled using exec or something? |
| 229 | if filename.startswith(tfdir): |
| 230 | continue |
| 231 | |
| 232 | modulename = inspect.getmodulename(filename) |
| 233 | |
| 234 | if filename.endswith(".pyc") or filename.endswith(".pyo"): |
| 235 | filename = filename[:-1] |
| 236 | |
| 237 | if coverdir: |
| 238 | thiscoverdir = coverdir |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 239 | else: |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 240 | thiscoverdir = os.path.dirname(os.path.abspath(filename)) |
| 241 | |
| 242 | # the code from here to "<<<" is the contents of the `fileutil.make_dirs()' function in the Mojo Nation project. --Zooko 2001-10-14 |
| 243 | # http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/mojonation/evil/common/fileutil.py?rev=HEAD&content-type=text/vnd.viewcvs-markup |
| 244 | tx = None |
| 245 | try: |
| 246 | os.makedirs(thiscoverdir) |
| 247 | except OSError, x: |
| 248 | tx = x |
| 249 | |
| 250 | if not os.path.isdir(thiscoverdir): |
| 251 | if tx: |
| 252 | raise tx |
| 253 | raise exceptions.IOError, "unknown error prevented creation of directory: %s" % thiscoverdir # careful not to construct an IOError with a 2-tuple, as that has a special meaning... |
| 254 | # <<< |
| 255 | |
| 256 | # build list file name by appending a ".cover" to the module name |
| 257 | # and sticking it into the specified directory |
| 258 | if "." in modulename: |
| 259 | # A module in a package |
| 260 | finalname = modulename.split(".")[-1] |
| 261 | listfilename = os.path.join(thiscoverdir, finalname + ".cover") |
| 262 | else: |
| 263 | listfilename = os.path.join(thiscoverdir, modulename + ".cover") |
| 264 | |
| 265 | # Get the original lines from the .py file |
| 266 | try: |
| 267 | lines = open(filename, 'r').readlines() |
| 268 | except IOError, err: |
| 269 | sys.stderr.write("trace: Could not open %s for reading because: %s - skipping\n" % (`filename`, err)) |
| 270 | continue |
| 271 | |
| 272 | try: |
| 273 | outfile = open(listfilename, 'w') |
| 274 | except IOError, err: |
| 275 | sys.stderr.write( |
| 276 | '%s: Could not open %s for writing because: %s" \ |
| 277 | "- skipping\n' % ("trace", `listfilename`, err)) |
| 278 | continue |
| 279 | |
| 280 | # If desired, get a list of the line numbers which represent |
| 281 | # executable content (returned as a dict for better lookup speed) |
| 282 | if show_missing: |
| 283 | executable_linenos = find_executable_linenos(filename) |
| 284 | else: |
| 285 | executable_linenos = {} |
| 286 | |
| 287 | n_lines = 0 |
| 288 | n_hits = 0 |
| 289 | lines_hit = per_file[key] |
| 290 | for i in range(len(lines)): |
| 291 | line = lines[i] |
| 292 | |
| 293 | # do the blank/comment match to try to mark more lines |
| 294 | # (help the reader find stuff that hasn't been covered) |
| 295 | if lines_hit.has_key(i+1): |
| 296 | # count precedes the lines that we captured |
| 297 | outfile.write('%5d: ' % lines_hit[i+1]) |
| 298 | n_hits = n_hits + 1 |
| 299 | n_lines = n_lines + 1 |
| 300 | elif blank.match(line): |
| 301 | # blank lines and comments are preceded by dots |
| 302 | outfile.write(' . ') |
| 303 | else: |
| 304 | # lines preceded by no marks weren't hit |
| 305 | # Highlight them if so indicated, unless the line contains |
| 306 | # '#pragma: NO COVER' (it is possible to embed this into |
| 307 | # the text as a non-comment; no easy fix) |
| 308 | if executable_linenos.has_key(i+1) and \ |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 309 | lines[i].find(' '.join(['#pragma', 'NO COVER'])) == -1: |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 310 | outfile.write('>>>>>> ') |
| 311 | else: |
| 312 | outfile.write(' '*7) |
| 313 | n_lines = n_lines + 1 |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 314 | outfile.write(lines[i].expandtabs(8)) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 315 | |
| 316 | outfile.close() |
| 317 | |
| 318 | if summary and n_lines: |
| 319 | percent = int(100 * n_hits / n_lines) |
| 320 | sums[modulename] = n_lines, percent, modulename, filename |
| 321 | |
| 322 | if summary and sums: |
| 323 | mods = sums.keys() |
| 324 | mods.sort() |
| 325 | print "lines cov% module (path)" |
| 326 | for m in mods: |
| 327 | n_lines, percent, modulename, filename = sums[m] |
| 328 | print "%5d %3d%% %s (%s)" % sums[m] |
| 329 | |
| 330 | if self.outfile: |
| 331 | # try and store counts and module info into self.outfile |
| 332 | try: |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 333 | pickle.dump((self.counts, self.calledfuncs), |
| 334 | open(self.outfile, 'w'), 1) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 335 | except IOError, err: |
| 336 | sys.stderr.write("cannot save counts files because %s" % err) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 337 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 338 | def _find_LINENO_from_code(code): |
| 339 | """return the numbers of the lines containing the source code that |
| 340 | was compiled into code""" |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 341 | linenos = {} |
| 342 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 343 | line_increments = [ord(c) for c in code.co_lnotab[1::2]] |
| 344 | table_length = len(line_increments) |
| 345 | |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 346 | lineno = code.co_firstlineno |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 347 | |
| 348 | for li in line_increments: |
| 349 | linenos[lineno] = 1 |
| 350 | lineno += li |
| 351 | linenos[lineno] = 1 |
| 352 | |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 353 | return linenos |
| 354 | |
| 355 | def _find_LINENO(code): |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 356 | """return all of the lineno information from a code object""" |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 357 | import types |
| 358 | |
| 359 | # get all of the lineno information from the code of this scope level |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 360 | linenos = _find_LINENO_from_code(code) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 361 | |
| 362 | # and check the constants for references to other code objects |
| 363 | for c in code.co_consts: |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 364 | if type(c) == types.CodeType: |
| 365 | # find another code object, so recurse into it |
| 366 | linenos.update(_find_LINENO(c)) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 367 | return linenos |
| 368 | |
| 369 | def find_executable_linenos(filename): |
| 370 | """return a dict of the line numbers from executable statements in a file |
| 371 | |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 372 | """ |
| 373 | import parser |
| 374 | |
Jeremy Hylton | 66a7e57 | 2001-05-08 04:20:52 +0000 | [diff] [blame] | 375 | assert filename.endswith('.py') |
| 376 | |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 377 | prog = open(filename).read() |
| 378 | ast = parser.suite(prog) |
| 379 | code = parser.compileast(ast, filename) |
| 380 | |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 381 | return _find_LINENO(code) |
| 382 | |
| 383 | ### XXX because os.path.commonprefix seems broken by my way of thinking... |
| 384 | def commonprefix(dirs): |
| 385 | "Given a list of pathnames, returns the longest common leading component" |
| 386 | if not dirs: return '' |
| 387 | n = copy.copy(dirs) |
| 388 | for i in range(len(n)): |
| 389 | n[i] = n[i].split(os.sep) |
| 390 | prefix = n[0] |
| 391 | for item in n: |
| 392 | for i in range(len(prefix)): |
| 393 | if prefix[:i+1] <> item[:i+1]: |
| 394 | prefix = prefix[:i] |
| 395 | if i == 0: return '' |
| 396 | break |
| 397 | return os.sep.join(prefix) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 399 | class Trace: |
| 400 | def __init__(self, count=1, trace=1, countfuncs=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None): |
| 401 | """ |
| 402 | @param count true iff it should count number of times each line is executed |
| 403 | @param trace true iff it should print out each line that is being counted |
| 404 | @param countfuncs true iff it should just output a list of (filename, modulename, funcname,) for functions that were called at least once; This overrides `count' and `trace' |
| 405 | @param ignoremods a list of the names of modules to ignore |
| 406 | @param ignoredirs a list of the names of directories to ignore all of the (recursive) contents of |
| 407 | @param infile file from which to read stored counts to be added into the results |
| 408 | @param outfile file in which to write the results |
| 409 | """ |
| 410 | self.infile = infile |
| 411 | self.outfile = outfile |
| 412 | self.ignore = Ignore(ignoremods, ignoredirs) |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 413 | self.counts = {} # keys are (filename, linenumber) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 414 | self.blabbed = {} # for debugging |
| 415 | self.pathtobasename = {} # for memoizing os.path.basename |
| 416 | self.donothing = 0 |
| 417 | self.trace = trace |
| 418 | self._calledfuncs = {} |
| 419 | if countfuncs: |
| 420 | self.globaltrace = self.globaltrace_countfuncs |
| 421 | elif trace and count: |
| 422 | self.globaltrace = self.globaltrace_lt |
| 423 | self.localtrace = self.localtrace_trace_and_count |
| 424 | elif trace: |
| 425 | self.globaltrace = self.globaltrace_lt |
| 426 | self.localtrace = self.localtrace_trace |
| 427 | elif count: |
| 428 | self.globaltrace = self.globaltrace_lt |
| 429 | self.localtrace = self.localtrace_count |
| 430 | else: |
| 431 | # Ahem -- do nothing? Okay. |
| 432 | self.donothing = 1 |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 434 | def run(self, cmd): |
| 435 | import __main__ |
| 436 | dict = __main__.__dict__ |
| 437 | if not self.donothing: |
| 438 | sys.settrace(self.globaltrace) |
| 439 | try: |
| 440 | exec cmd in dict, dict |
| 441 | finally: |
| 442 | if not self.donothing: |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 443 | sys.settrace(None) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 444 | |
| 445 | def runctx(self, cmd, globals=None, locals=None): |
| 446 | if globals is None: globals = {} |
| 447 | if locals is None: locals = {} |
| 448 | if not self.donothing: |
Skip Montanaro | 3a48ed9 | 2002-07-25 16:09:35 +0000 | [diff] [blame] | 449 | sys.settrace(self.globaltrace) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 450 | try: |
Skip Montanaro | 3a48ed9 | 2002-07-25 16:09:35 +0000 | [diff] [blame] | 451 | exec cmd in globals, locals |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 452 | finally: |
| 453 | if not self.donothing: |
| 454 | sys.settrace(None) |
| 455 | |
| 456 | def runfunc(self, func, *args, **kw): |
| 457 | result = None |
| 458 | if not self.donothing: |
| 459 | sys.settrace(self.globaltrace) |
| 460 | try: |
| 461 | result = apply(func, args, kw) |
| 462 | finally: |
| 463 | if not self.donothing: |
| 464 | sys.settrace(None) |
| 465 | return result |
| 466 | |
| 467 | def globaltrace_countfuncs(self, frame, why, arg): |
| 468 | """ |
| 469 | Handles `call' events (why == 'call') and adds the (filename, modulename, funcname,) to the self._calledfuncs dict. |
| 470 | """ |
| 471 | if why == 'call': |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 472 | filename, lineno, funcname, context, lineindex = \ |
| 473 | inspect.getframeinfo(frame, 0) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 474 | if filename: |
| 475 | modulename = inspect.getmodulename(filename) |
| 476 | else: |
| 477 | modulename = None |
| 478 | self._calledfuncs[(filename, modulename, funcname,)] = 1 |
| 479 | |
| 480 | def globaltrace_lt(self, frame, why, arg): |
| 481 | """ |
| 482 | Handles `call' events (why == 'call') and if the code block being entered is to be ignored then it returns `None', else it returns `self.localtrace'. |
| 483 | """ |
| 484 | if why == 'call': |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 485 | filename, lineno, funcname, context, lineindex = \ |
| 486 | inspect.getframeinfo(frame, 0) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 487 | if filename: |
| 488 | modulename = inspect.getmodulename(filename) |
Skip Montanaro | 3a48ed9 | 2002-07-25 16:09:35 +0000 | [diff] [blame] | 489 | if modulename is not None: |
| 490 | ignore_it = self.ignore.names(filename, modulename) |
Skip Montanaro | 3a48ed9 | 2002-07-25 16:09:35 +0000 | [diff] [blame] | 491 | if not ignore_it: |
| 492 | if self.trace: |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 493 | print (" --- modulename: %s, funcname: %s" |
| 494 | % (modulename, funcname)) |
Skip Montanaro | 3a48ed9 | 2002-07-25 16:09:35 +0000 | [diff] [blame] | 495 | return self.localtrace |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 496 | else: |
| 497 | # XXX why no filename? |
| 498 | return None |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 499 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 500 | def localtrace_trace_and_count(self, frame, why, arg): |
| 501 | if why == 'line': |
| 502 | # record the file name and line number of every trace |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 503 | # XXX I wish inspect offered me an optimized |
| 504 | # `getfilename(frame)' to use in place of the presumably |
| 505 | # heavier `getframeinfo()'. --Zooko 2001-10-14 |
| 506 | |
| 507 | filename, lineno, funcname, context, lineindex = \ |
| 508 | inspect.getframeinfo(frame, 1) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 509 | key = (filename, lineno,) |
| 510 | self.counts[key] = self.counts.get(key, 0) + 1 |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 511 | |
| 512 | # XXX not convinced that this memoizing is a performance |
| 513 | # win -- I don't know enough about Python guts to tell. |
| 514 | # --Zooko 2001-10-14 |
| 515 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 516 | bname = self.pathtobasename.get(filename) |
| 517 | if bname is None: |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 518 | |
| 519 | # Using setdefault faster than two separate lines? |
| 520 | # --Zooko 2001-10-14 |
| 521 | bname = self.pathtobasename.setdefault(filename, |
| 522 | os.path.basename(filename)) |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 523 | try: |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 524 | print "%s(%d): %s" % (bname, lineno, context[lineindex]), |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 525 | except IndexError: |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 526 | # Uh.. sometimes getframeinfo gives me a context of |
| 527 | # length 1 and a lineindex of -2. Oh well. |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 528 | pass |
| 529 | return self.localtrace |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 530 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 531 | def localtrace_trace(self, frame, why, arg): |
| 532 | if why == 'line': |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 533 | # XXX shouldn't do the count increment when arg is |
| 534 | # exception? But be careful to return self.localtrace |
| 535 | # when arg is exception! ? --Zooko 2001-10-14 |
| 536 | |
| 537 | # record the file name and line number of every trace XXX |
| 538 | # I wish inspect offered me an optimized |
| 539 | # `getfilename(frame)' to use in place of the presumably |
| 540 | # heavier `getframeinfo()'. --Zooko 2001-10-14 |
| 541 | filename, lineno, funcname, context, lineindex = \ |
| 542 | inspect.getframeinfo(frame) |
| 543 | |
| 544 | # XXX not convinced that this memoizing is a performance |
| 545 | # win -- I don't know enough about Python guts to tell. |
| 546 | # --Zooko 2001-10-14 |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 547 | bname = self.pathtobasename.get(filename) |
| 548 | if bname is None: |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 549 | # Using setdefault faster than two separate lines? |
| 550 | # --Zooko 2001-10-14 |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 551 | bname = self.pathtobasename.setdefault(filename, os.path.basename(filename)) |
Skip Montanaro | 3a48ed9 | 2002-07-25 16:09:35 +0000 | [diff] [blame] | 552 | if context is not None: |
| 553 | try: |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 554 | print "%s(%d): %s" % (bname, lineno, context[lineindex]), |
Skip Montanaro | 3a48ed9 | 2002-07-25 16:09:35 +0000 | [diff] [blame] | 555 | except IndexError: |
| 556 | # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2. Oh well. |
| 557 | pass |
| 558 | else: |
| 559 | print "%s(???): ???" % bname |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 560 | return self.localtrace |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 561 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 562 | def localtrace_count(self, frame, why, arg): |
| 563 | if why == 'line': |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 564 | filename = frame.f_code.co_filename |
| 565 | lineno = frame.f_lineno |
| 566 | key = filename, lineno |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 567 | self.counts[key] = self.counts.get(key, 0) + 1 |
| 568 | return self.localtrace |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 569 | |
| 570 | def results(self): |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 571 | return CoverageResults(self.counts, infile=self.infile, |
| 572 | outfile=self.outfile, |
| 573 | calledfuncs=self._calledfuncs) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 574 | |
| 575 | def _err_exit(msg): |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 576 | sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 577 | sys.exit(1) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 578 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 579 | def main(argv=None): |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 580 | import getopt |
| 581 | |
| 582 | if argv is None: |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 583 | argv = sys.argv |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 584 | try: |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 585 | opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:l", |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 586 | ["help", "version", "trace", "count", |
| 587 | "report", "no-report", |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 588 | "file=", "missing", |
Jeremy Hylton | 66a7e57 | 2001-05-08 04:20:52 +0000 | [diff] [blame] | 589 | "ignore-module=", "ignore-dir=", |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 590 | "coverdir=", "listfuncs",]) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 591 | |
| 592 | except getopt.error, msg: |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 593 | sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 594 | sys.stderr.write("Try `%s --help' for more information\n" |
| 595 | % sys.argv[0]) |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 596 | sys.exit(1) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 597 | |
| 598 | trace = 0 |
| 599 | count = 0 |
| 600 | report = 0 |
| 601 | no_report = 0 |
| 602 | counts_file = None |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 603 | missing = 0 |
| 604 | ignore_modules = [] |
| 605 | ignore_dirs = [] |
Jeremy Hylton | 66a7e57 | 2001-05-08 04:20:52 +0000 | [diff] [blame] | 606 | coverdir = None |
| 607 | summary = 0 |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 608 | listfuncs = false |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 609 | |
| 610 | for opt, val in opts: |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 611 | if opt == "--help": |
| 612 | usage(sys.stdout) |
| 613 | sys.exit(0) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 614 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 615 | if opt == "--version": |
| 616 | sys.stdout.write("trace 2.0\n") |
| 617 | sys.exit(0) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 619 | if opt == "-l" or opt == "--listfuncs": |
| 620 | listfuncs = true |
| 621 | continue |
| 622 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 623 | if opt == "-t" or opt == "--trace": |
| 624 | trace = 1 |
| 625 | continue |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 626 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 627 | if opt == "-c" or opt == "--count": |
| 628 | count = 1 |
| 629 | continue |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 630 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 631 | if opt == "-r" or opt == "--report": |
| 632 | report = 1 |
| 633 | continue |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 634 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 635 | if opt == "-R" or opt == "--no-report": |
| 636 | no_report = 1 |
| 637 | continue |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 638 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 639 | if opt == "-f" or opt == "--file": |
| 640 | counts_file = val |
| 641 | continue |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 642 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 643 | if opt == "-m" or opt == "--missing": |
| 644 | missing = 1 |
| 645 | continue |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 646 | |
Jeremy Hylton | 66a7e57 | 2001-05-08 04:20:52 +0000 | [diff] [blame] | 647 | if opt == "-C" or opt == "--coverdir": |
| 648 | coverdir = val |
| 649 | continue |
| 650 | |
| 651 | if opt == "-s" or opt == "--summary": |
| 652 | summary = 1 |
| 653 | continue |
| 654 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 655 | if opt == "--ignore-module": |
| 656 | ignore_modules.append(val) |
| 657 | continue |
| 658 | |
| 659 | if opt == "--ignore-dir": |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 660 | for s in val.split(os.pathsep): |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 661 | s = os.path.expandvars(s) |
| 662 | # should I also call expanduser? (after all, could use $HOME) |
| 663 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 664 | s = s.replace("$prefix", |
| 665 | os.path.join(sys.prefix, "lib", |
| 666 | "python" + sys.version[:3])) |
| 667 | s = s.replace("$exec_prefix", |
| 668 | os.path.join(sys.exec_prefix, "lib", |
| 669 | "python" + sys.version[:3])) |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 670 | s = os.path.normpath(s) |
| 671 | ignore_dirs.append(s) |
| 672 | continue |
| 673 | |
| 674 | assert 0, "Should never get here" |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 675 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 676 | if listfuncs and (count or trace): |
| 677 | _err_exit("cannot specify both --listfuncs and (--trace or --count)") |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 678 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 679 | if not count and not trace and not report and not listfuncs: |
| 680 | _err_exit("must specify one of --trace, --count, --report or --listfuncs") |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 681 | |
| 682 | if report and no_report: |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 683 | _err_exit("cannot specify both --report and --no-report") |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 685 | if report and not counts_file: |
| 686 | _err_exit("--report requires a --file") |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 687 | |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 688 | if no_report and len(prog_argv) == 0: |
| 689 | _err_exit("missing name of file to run") |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 690 | |
| 691 | # everything is ready |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 692 | if report: |
| 693 | results = CoverageResults(infile=counts_file, outfile=counts_file) |
| 694 | results.write_results(missing, summary=summary, coverdir=coverdir) |
| 695 | else: |
| 696 | sys.argv = prog_argv |
| 697 | progname = prog_argv[0] |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 698 | sys.path[0] = os.path.split(progname)[0] |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 699 | |
Jeremy Hylton | 89f1d6c | 2002-12-11 21:28:32 +0000 | [diff] [blame^] | 700 | t = Trace(count, trace, countfuncs=listfuncs, |
| 701 | ignoremods=ignore_modules, ignoredirs=ignore_dirs, |
| 702 | infile=counts_file, outfile=counts_file) |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 703 | try: |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 704 | t.run('execfile(' + `progname` + ')') |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 705 | except IOError, err: |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 706 | _err_exit("Cannot run file %s because: %s" % (`sys.argv[0]`, err)) |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 707 | except SystemExit: |
| 708 | pass |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 709 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 710 | results = t.results() |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 711 | |
Jeremy Hylton | 0b7b4b8 | 2000-09-18 01:46:01 +0000 | [diff] [blame] | 712 | if not no_report: |
Guido van Rossum | a30eacf | 2001-11-28 19:41:45 +0000 | [diff] [blame] | 713 | results.write_results(missing, summary=summary, coverdir=coverdir) |
Jeremy Hylton | da1ec46 | 2000-08-03 19:26:21 +0000 | [diff] [blame] | 714 | |
| 715 | if __name__=='__main__': |
| 716 | main() |