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