Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/trace.py @ 17581:c1937faabb23 2.1
Added support for optional explanation parameter to the \versionadded
macro.
Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most
of the work in a helper function, with the do_cmd_*() wrappers just supplying
a portion of the replacement text.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Wed, 18 Apr 2001 17:29:14 +0000 |
| parents | 14a27acc755a |
| children | f3b8785bd022 |
| rev | line source |
|---|---|
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
2 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
3 # Copyright 2000, Mojam Media, Inc., all rights reserved. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
4 # Author: Skip Montanaro |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
5 # |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
6 # Copyright 1999, Bioreason, Inc., all rights reserved. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
7 # Author: Andrew Dalke |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
8 # |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
9 # Copyright 1995-1997, Automatrix, Inc., all rights reserved. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
10 # Author: Skip Montanaro |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
11 # |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
12 # Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
13 # |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
14 # |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
15 # Permission to use, copy, modify, and distribute this Python software and |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
16 # its associated documentation for any purpose without fee is hereby |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
17 # granted, provided that the above copyright notice appears in all copies, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
18 # and that both that copyright notice and this permission notice appear in |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
19 # supporting documentation, and that the name of neither Automatrix, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
20 # Bioreason or Mojam Media be used in advertising or publicity pertaining to |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
21 # distribution of the software without specific, written prior permission. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
22 # |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
23 # |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
24 # Summary of recent changes: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
25 # Support for files with the same basename (submodules in packages) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
26 # Expanded the idea of how to ignore files or modules |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
27 # Split tracing and counting into different classes |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
28 # Extracted count information and reporting from the count class |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
29 # Added some ability to detect which missing lines could be executed |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
30 # Added pseudo-pragma to prohibit complaining about unexecuted lines |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
31 # Rewrote the main program |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
32 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
33 # Summary of older changes: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
34 # Added run-time display of statements being executed |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
35 # Incorporated portability and performance fixes from Greg Stein |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
36 # Incorporated main program from Michael Scharf |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
37 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
38 """ |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
39 program/module to trace Python program or function execution |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
40 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
41 Sample use, command line: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
42 trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
43 trace.py -t --ignore-dir '$prefix' spam.py eggs |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
44 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
45 Sample use, programmatically (still more complicated than it should be) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
46 # create an Ignore option, telling it what you want to ignore |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
47 ignore = trace.Ignore(dirs = [sys.prefix, sys.exec_prefix]) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
48 # create a Coverage object, telling it what to ignore |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
49 coverage = trace.Coverage(ignore) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
50 # run the new command using the given trace |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
51 trace.run(coverage.trace, 'main()') |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
52 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
53 # make a report, telling it where you want output |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
54 t = trace.create_results_log(coverage.results(), |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
55 '/usr/local/Automatrix/concerts/coverage') |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
56 show_missing = 1) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
57 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
58 The Trace class can be instantited instead of the Coverage class if |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
59 runtime display of executable lines is desired instead of statement |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
60 converage measurement. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
61 """ |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
62 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
63 import sys, os, string, marshal, tempfile, copy, operator |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
64 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
65 def usage(outfile): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
66 outfile.write("""Usage: %s [OPTIONS] <file> [ARGS] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
67 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
68 Execution: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
69 --help Display this help then exit. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
70 --version Output version information then exit. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
71 -t,--trace Print the line to be executed to sys.stdout. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
72 -c,--count Count the number of times a line is executed. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
73 Results are written in the results file, if given. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
74 -r,--report Generate a report from a results file; do not |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
75 execute any code. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
76 (One of `-t', `-c' or `-r' must be specified) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
77 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
78 I/O: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
79 -f,--file= File name for accumulating results over several runs. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
80 (No file name means do not archive results) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
81 -d,--logdir= Directory to use when writing annotated log files. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
82 Log files are the module __name__ with `.` replaced |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
83 by os.sep and with '.pyl' added. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
84 -m,--missing Annotate all executable lines which were not executed |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
85 with a '>>>>>> '. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
86 -R,--no-report Do not generate the annotated reports. Useful if |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
87 you want to accumulate several over tests. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
88 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
89 Selection: Do not trace or log lines from ... |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
90 --ignore-module=[string] modules with the given __name__, and submodules |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
91 of that module |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
92 --ignore-dir=[string] files in the stated directory (multiple |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
93 directories can be joined by os.pathsep) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
94 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
95 The selection options can be listed multiple times to ignore different |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
96 modules. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
97 """ % sys.argv[0]) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
98 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
99 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
100 class Ignore: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
101 def __init__(self, modules = None, dirs = None): |
| 14657 | 102 self._mods = modules or [] |
| 103 self._dirs = dirs or [] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
104 |
| 14657 | 105 self._ignore = { '<string>': 1 } |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
106 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
107 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
108 def names(self, filename, modulename): |
| 14657 | 109 if self._ignore.has_key(modulename): |
| 110 return self._ignore[modulename] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
111 |
| 14657 | 112 # haven't seen this one before, so see if the module name is |
| 113 # on the ignore list. Need to take some care since ignoring | |
| 114 # "cmp" musn't mean ignoring "cmpcache" but ignoring | |
| 115 # "Spam" must also mean ignoring "Spam.Eggs". | |
| 116 for mod in self._mods: | |
| 117 if mod == modulename: # Identical names, so ignore | |
| 118 self._ignore[modulename] = 1 | |
| 119 return 1 | |
| 120 # check if the module is a proper submodule of something on | |
| 121 # the ignore list | |
| 122 n = len(mod) | |
| 123 # (will not overflow since if the first n characters are the | |
| 124 # same and the name has not already occured, then the size | |
| 125 # of "name" is greater than that of "mod") | |
| 126 if mod == modulename[:n] and modulename[n] == '.': | |
| 127 self._ignore[modulename] = 1 | |
| 128 return 1 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
129 |
| 14657 | 130 # Now check that __file__ isn't in one of the directories |
| 131 if filename is None: | |
| 132 # must be a built-in, so we must ignore | |
| 133 self._ignore[modulename] = 1 | |
| 134 return 1 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
135 |
| 14657 | 136 # Ignore a file when it contains one of the ignorable paths |
| 137 for d in self._dirs: | |
| 138 # The '+ os.sep' is to ensure that d is a parent directory, | |
| 139 # as compared to cases like: | |
| 140 # d = "/usr/local" | |
| 141 # filename = "/usr/local.py" | |
| 142 # or | |
| 143 # d = "/usr/local.py" | |
| 144 # filename = "/usr/local.py" | |
| 145 if string.find(filename, d + os.sep) == 0: | |
| 146 self._ignore[modulename] = 1 | |
| 147 return 1 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
148 |
| 14657 | 149 # Tried the different ways, so we don't ignore this module |
| 150 self._ignore[modulename] = 0 | |
| 151 return 0 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
152 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
153 def run(trace, cmd): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
154 import __main__ |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
155 dict = __main__.__dict__ |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
156 sys.settrace(trace) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
157 try: |
| 14657 | 158 exec cmd in dict, dict |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
159 finally: |
| 14657 | 160 sys.settrace(None) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
161 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
162 def runctx(trace, cmd, globals=None, locals=None): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
163 if globals is None: globals = {} |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
164 if locals is None: locals = {} |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
165 sys.settrace(trace) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
166 try: |
| 14657 | 167 exec cmd in dict, dict |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
168 finally: |
| 14657 | 169 sys.settrace(None) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
170 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
171 def runfunc(trace, func, *args, **kw): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
172 result = None |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
173 sys.settrace(trace) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
174 try: |
| 14657 | 175 result = apply(func, args, kw) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
176 finally: |
| 14657 | 177 sys.settrace(None) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
178 return result |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
179 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
180 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
181 class CoverageResults: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
182 def __init__(self, counts = {}, modules = {}): |
| 14657 | 183 self.counts = counts.copy() # map (filename, lineno) to count |
| 184 self.modules = modules.copy() # map filenames to modules | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
185 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
186 def update(self, other): |
| 14657 | 187 """Merge in the data from another CoverageResults""" |
| 188 counts = self.counts | |
| 189 other_counts = other.counts | |
| 190 modules = self.modules | |
| 191 other_modules = other.modules | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
192 |
| 14657 | 193 for key in other_counts.keys(): |
| 194 counts[key] = counts.get(key, 0) + other_counts[key] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
195 |
| 14657 | 196 for key in other_modules.keys(): |
| 197 if modules.has_key(key): | |
| 198 # make sure they point to the same file | |
| 199 assert modules[key] == other_modules[key], \ | |
| 200 "Strange! filename %s has two different module names" % \ | |
| 201 (key, modules[key], other_module[key]) | |
| 202 else: | |
| 203 modules[key] = other_modules[key] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
204 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
205 # Given a code string, return the SET_LINENO information |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
206 def _find_LINENO_from_string(co_code): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
207 """return all of the SET_LINENO information from a code string""" |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
208 import dis |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
209 linenos = {} |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
210 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
211 # This code was filched from the `dis' module then modified |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
212 n = len(co_code) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
213 i = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
214 prev_op = None |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
215 prev_lineno = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
216 while i < n: |
| 14657 | 217 c = co_code[i] |
| 218 op = ord(c) | |
| 219 if op == dis.SET_LINENO: | |
| 220 if prev_op == op: | |
| 221 # two SET_LINENO in a row, so the previous didn't | |
| 222 # indicate anything. This occurs with triple | |
| 223 # quoted strings (?). Remove the old one. | |
| 224 del linenos[prev_lineno] | |
| 225 prev_lineno = ord(co_code[i+1]) + ord(co_code[i+2])*256 | |
| 226 linenos[prev_lineno] = 1 | |
| 227 if op >= dis.HAVE_ARGUMENT: | |
| 228 i = i + 3 | |
| 229 else: | |
| 230 i = i + 1 | |
| 231 prev_op = op | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
232 return linenos |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
233 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
234 def _find_LINENO(code): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
235 """return all of the SET_LINENO information from a code object""" |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
236 import types |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
237 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
238 # get all of the lineno information from the code of this scope level |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
239 linenos = _find_LINENO_from_string(code.co_code) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
240 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
241 # and check the constants for references to other code objects |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
242 for c in code.co_consts: |
| 14657 | 243 if type(c) == types.CodeType: |
| 244 # find another code object, so recurse into it | |
| 245 linenos.update(_find_LINENO(c)) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
246 return linenos |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
247 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
248 def find_executable_linenos(filename): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
249 """return a dict of the line numbers from executable statements in a file |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
250 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
251 Works by finding all of the code-like objects in the module then searching |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
252 the byte code for 'SET_LINENO' terms (so this won't work one -O files). |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
253 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
254 """ |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
255 import parser |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
256 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
257 prog = open(filename).read() |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
258 ast = parser.suite(prog) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
259 code = parser.compileast(ast, filename) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
260 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
261 # The only way I know to find line numbers is to look for the |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
262 # SET_LINENO instructions. Isn't there some way to get it from |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
263 # the AST? |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
264 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
265 return _find_LINENO(code) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
266 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
267 ### XXX because os.path.commonprefix seems broken by my way of thinking... |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
268 def commonprefix(dirs): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
269 "Given a list of pathnames, returns the longest common leading component" |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
270 if not dirs: return '' |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
271 n = copy.copy(dirs) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
272 for i in range(len(n)): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
273 n[i] = n[i].split(os.sep) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
274 prefix = n[0] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
275 for item in n: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
276 for i in range(len(prefix)): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
277 if prefix[:i+1] <> item[:i+1]: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
278 prefix = prefix[:i] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
279 if i == 0: return '' |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
280 break |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
281 return os.sep.join(prefix) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
282 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
283 def create_results_log(results, dirname = ".", show_missing = 1, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
284 save_counts = 0): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
285 import re |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
286 # turn the counts data ("(filename, lineno) = count") into something |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
287 # accessible on a per-file basis |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
288 per_file = {} |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
289 for filename, lineno in results.counts.keys(): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
290 lines_hit = per_file[filename] = per_file.get(filename, {}) |
| 14657 | 291 lines_hit[lineno] = results.counts[(filename, lineno)] |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
292 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
293 # try and merge existing counts and modules file from dirname |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
294 try: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
295 counts = marshal.load(open(os.path.join(dirname, "counts"))) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
296 modules = marshal.load(open(os.path.join(dirname, "modules"))) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
297 results.update(results.__class__(counts, modules)) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
298 except IOError: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
299 pass |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
300 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
301 # there are many places where this is insufficient, like a blank |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
302 # line embedded in a multiline string. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
303 blank = re.compile(r'^\s*(#.*)?$') |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
304 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
305 # generate file paths for the coverage files we are going to write... |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
306 fnlist = [] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
307 tfdir = tempfile.gettempdir() |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
308 for key in per_file.keys(): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
309 filename = key |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
310 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
311 # skip some "files" we don't care about... |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
312 if filename == "<string>": |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
313 continue |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
314 # are these caused by code compiled using exec or something? |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
315 if filename.startswith(tfdir): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
316 continue |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
317 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
318 # XXX this is almost certainly not portable!!! |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
319 fndir = os.path.dirname(filename) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
320 if filename[:1] == os.sep: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
321 coverpath = os.path.join(dirname, "."+fndir) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
322 else: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
323 coverpath = os.path.join(dirname, fndir) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
324 |
| 14657 | 325 if filename.endswith(".pyc") or filename.endswith(".pyo"): |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
326 filename = filename[:-1] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
327 |
| 14657 | 328 # Get the original lines from the .py file |
| 329 try: | |
| 330 lines = open(filename, 'r').readlines() | |
| 331 except IOError, err: | |
| 332 sys.stderr.write("%s: Could not open %s for reading " \ | |
| 333 "because: %s - skipping\n" % \ | |
| 334 ("trace", `filename`, err.strerror)) | |
| 335 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
336 |
| 14657 | 337 modulename = os.path.split(results.modules[key])[1] |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
338 |
| 14657 | 339 # build list file name by appending a ".cover" to the module name |
| 340 # and sticking it into the specified directory | |
| 341 listfilename = os.path.join(coverpath, modulename + ".cover") | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
342 #sys.stderr.write("modulename: %(modulename)s\n" |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
343 # "filename: %(filename)s\n" |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
344 # "coverpath: %(coverpath)s\n" |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
345 # "listfilename: %(listfilename)s\n" |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
346 # "dirname: %(dirname)s\n" |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
347 # % locals()) |
| 14657 | 348 try: |
| 349 outfile = open(listfilename, 'w') | |
| 350 except IOError, err: | |
| 351 sys.stderr.write( | |
| 352 '%s: Could not open %s for writing because: %s" \ | |
| 353 "- skipping\n' % ("trace", `listfilename`, err.strerror)) | |
| 354 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
355 |
| 14657 | 356 # If desired, get a list of the line numbers which represent |
| 357 # executable content (returned as a dict for better lookup speed) | |
| 358 if show_missing: | |
| 359 executable_linenos = find_executable_linenos(filename) | |
| 360 else: | |
| 361 executable_linenos = {} | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
362 |
| 14657 | 363 lines_hit = per_file[key] |
| 364 for i in range(len(lines)): | |
| 365 line = lines[i] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
366 |
| 14657 | 367 # do the blank/comment match to try to mark more lines |
| 368 # (help the reader find stuff that hasn't been covered) | |
| 369 if lines_hit.has_key(i+1): | |
| 370 # count precedes the lines that we captured | |
| 371 outfile.write('%5d: ' % lines_hit[i+1]) | |
| 372 elif blank.match(line): | |
| 373 # blank lines and comments are preceded by dots | |
| 374 outfile.write(' . ') | |
| 375 else: | |
| 376 # lines preceded by no marks weren't hit | |
| 377 # Highlight them if so indicated, unless the line contains | |
| 378 # '#pragma: NO COVER' (it is possible to embed this into | |
| 379 # the text as a non-comment; no easy fix) | |
| 380 if executable_linenos.has_key(i+1) and \ | |
| 381 string.find(lines[i], | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
382 string.join(['#pragma', 'NO COVER'])) == -1: |
| 14657 | 383 outfile.write('>>>>>> ') |
| 384 else: | |
| 385 outfile.write(' '*7) | |
| 386 outfile.write(string.expandtabs(lines[i], 8)) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
387 |
| 14657 | 388 outfile.close() |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
389 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
390 if save_counts: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
391 # try and store counts and module info into dirname |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
392 try: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
393 marshal.dump(results.counts, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
394 open(os.path.join(dirname, "counts"), "w")) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
395 marshal.dump(results.modules, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
396 open(os.path.join(dirname, "modules"), "w")) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
397 except IOError, err: |
| 14657 | 398 sys.stderr.write("cannot save counts/modules " \ |
| 399 "files because %s" % err.strerror) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
400 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
401 # There is a lot of code shared between these two classes even though |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
402 # it is straightforward to make a super class to share code. However, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
403 # for performance reasons (remember, this is called at every step) I |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
404 # wanted to keep everything to a single function call. Also, by |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
405 # staying within a single scope, I don't have to temporarily nullify |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
406 # sys.settrace, which would slow things down even more. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
407 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
408 class Coverage: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
409 def __init__(self, ignore = Ignore()): |
| 14657 | 410 self.ignore = ignore |
| 411 self.ignore_names = ignore._ignore # access ignore's cache (speed hack) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
412 |
| 14657 | 413 self.counts = {} # keys are (filename, linenumber) |
| 414 self.modules = {} # maps filename -> module name | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
415 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
416 def trace(self, frame, why, arg): |
| 14657 | 417 if why == 'line': |
| 418 # something is fishy about getting the file name | |
| 419 filename = frame.f_globals.get("__file__", None) | |
| 420 if filename is None: | |
| 421 filename = frame.f_code.co_filename | |
| 422 modulename = frame.f_globals["__name__"] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
423 |
| 14657 | 424 # We do this next block to keep from having to make methods |
| 425 # calls, which also requires resetting the trace | |
| 426 ignore_it = self.ignore_names.get(modulename, -1) | |
| 427 if ignore_it == -1: # unknown filename | |
| 428 sys.settrace(None) | |
| 429 ignore_it = self.ignore.names(filename, modulename) | |
| 430 sys.settrace(self.trace) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
431 |
| 14657 | 432 # record the module name for every file |
| 433 self.modules[filename] = modulename | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
434 |
| 14657 | 435 if not ignore_it: |
| 436 lineno = frame.f_lineno | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
437 |
| 14657 | 438 # record the file name and line number of every trace |
| 439 key = (filename, lineno) | |
| 440 self.counts[key] = self.counts.get(key, 0) + 1 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
441 |
| 14657 | 442 return self.trace |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
443 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
444 def results(self): |
| 14657 | 445 return CoverageResults(self.counts, self.modules) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
446 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
447 class Trace: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
448 def __init__(self, ignore = Ignore()): |
| 14657 | 449 self.ignore = ignore |
| 450 self.ignore_names = ignore._ignore # access ignore's cache (speed hack) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
451 |
| 14657 | 452 self.files = {'<string>': None} # stores lines from the .py file, or None |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
453 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
454 def trace(self, frame, why, arg): |
| 14657 | 455 if why == 'line': |
| 456 filename = frame.f_code.co_filename | |
| 457 modulename = frame.f_globals["__name__"] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
458 |
| 14657 | 459 # We do this next block to keep from having to make methods |
| 460 # calls, which also requires resetting the trace | |
| 461 ignore_it = self.ignore_names.get(modulename, -1) | |
| 462 if ignore_it == -1: # unknown filename | |
| 463 sys.settrace(None) | |
| 464 ignore_it = self.ignore.names(filename, modulename) | |
| 465 sys.settrace(self.trace) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
466 |
| 14657 | 467 if not ignore_it: |
| 468 lineno = frame.f_lineno | |
| 469 files = self.files | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
470 |
| 14657 | 471 if filename != '<string>' and not files.has_key(filename): |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
472 files[filename] = map(string.rstrip, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
473 open(filename).readlines()) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
474 |
| 14657 | 475 # If you want to see filenames (the original behaviour), try: |
| 476 # modulename = filename | |
| 477 # or, prettier but confusing when several files have the same name | |
| 478 # modulename = os.path.basename(filename) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
479 |
| 14657 | 480 if files[filename] != None: |
| 481 print '%s(%d): %s' % (os.path.basename(filename), lineno, | |
| 482 files[filename][lineno-1]) | |
| 483 else: | |
| 484 print '%s(%d): ??' % (modulename, lineno) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
485 |
| 14657 | 486 return self.trace |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
487 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
488 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
489 def _err_exit(msg): |
| 14657 | 490 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
| 491 sys.exit(1) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
492 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
493 def main(argv = None): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
494 import getopt |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
495 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
496 if argv is None: |
| 14657 | 497 argv = sys.argv |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
498 try: |
| 14657 | 499 opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:m", |
| 500 ["help", "version", "trace", "count", | |
| 501 "report", "no-report", | |
| 502 "file=", "logdir=", "missing", | |
| 503 "ignore-module=", "ignore-dir="]) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
504 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
505 except getopt.error, msg: |
| 14657 | 506 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
| 507 sys.stderr.write("Try `%s --help' for more information\n" % sys.argv[0]) | |
| 508 sys.exit(1) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
509 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
510 trace = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
511 count = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
512 report = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
513 no_report = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
514 counts_file = None |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
515 logdir = "." |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
516 missing = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
517 ignore_modules = [] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
518 ignore_dirs = [] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
519 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
520 for opt, val in opts: |
| 14657 | 521 if opt == "--help": |
| 522 usage(sys.stdout) | |
| 523 sys.exit(0) | |
| 524 | |
| 525 if opt == "--version": | |
| 526 sys.stdout.write("trace 2.0\n") | |
| 527 sys.exit(0) | |
| 528 | |
| 529 if opt == "-t" or opt == "--trace": | |
| 530 trace = 1 | |
| 531 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
532 |
| 14657 | 533 if opt == "-c" or opt == "--count": |
| 534 count = 1 | |
| 535 continue | |
| 536 | |
| 537 if opt == "-r" or opt == "--report": | |
| 538 report = 1 | |
| 539 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
540 |
| 14657 | 541 if opt == "-R" or opt == "--no-report": |
| 542 no_report = 1 | |
| 543 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
544 |
| 14657 | 545 if opt == "-f" or opt == "--file": |
| 546 counts_file = val | |
| 547 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
548 |
| 14657 | 549 if opt == "-d" or opt == "--logdir": |
| 550 logdir = val | |
| 551 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
552 |
| 14657 | 553 if opt == "-m" or opt == "--missing": |
| 554 missing = 1 | |
| 555 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
556 |
| 14657 | 557 if opt == "--ignore-module": |
| 558 ignore_modules.append(val) | |
| 559 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
560 |
| 14657 | 561 if opt == "--ignore-dir": |
| 562 for s in string.split(val, os.pathsep): | |
| 563 s = os.path.expandvars(s) | |
| 564 # should I also call expanduser? (after all, could use $HOME) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
565 |
| 14657 | 566 s = string.replace(s, "$prefix", |
| 567 os.path.join(sys.prefix, "lib", | |
| 568 "python" + sys.version[:3])) | |
| 569 s = string.replace(s, "$exec_prefix", | |
| 570 os.path.join(sys.exec_prefix, "lib", | |
| 571 "python" + sys.version[:3])) | |
| 572 s = os.path.normpath(s) | |
| 573 ignore_dirs.append(s) | |
| 574 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
575 |
| 14657 | 576 assert 0, "Should never get here" |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
577 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
578 if len(prog_argv) == 0: |
| 14657 | 579 _err_exit("missing name of file to run") |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
580 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
581 if count + trace + report > 1: |
| 14657 | 582 _err_exit("can only specify one of --trace, --count or --report") |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
583 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
584 if count + trace + report == 0: |
| 14657 | 585 _err_exit("must specify one of --trace, --count or --report") |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
586 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
587 if report and counts_file is None: |
| 14657 | 588 _err_exit("--report requires a --file") |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
589 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
590 if report and no_report: |
| 14657 | 591 _err_exit("cannot specify both --report and --no-report") |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
592 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
593 if logdir is not None: |
| 14657 | 594 # warn if the directory doesn't exist, but keep on going |
| 595 # (is this the correct behaviour?) | |
| 596 if not os.path.isdir(logdir): | |
| 597 sys.stderr.write( | |
| 598 "trace: WARNING, --logdir directory %s is not available\n" % | |
| 599 `logdir`) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
600 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
601 sys.argv = prog_argv |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
602 progname = prog_argv[0] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
603 if eval(sys.version[:3])>1.3: |
| 14657 | 604 sys.path[0] = os.path.split(progname)[0] # ??? |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
605 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
606 # everything is ready |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
607 ignore = Ignore(ignore_modules, ignore_dirs) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
608 if trace: |
| 14657 | 609 t = Trace(ignore) |
| 610 try: | |
| 611 run(t.trace, 'execfile(' + `progname` + ')') | |
| 612 except IOError, err: | |
| 613 _err_exit("Cannot run file %s because: %s" % \ | |
| 614 (`sys.argv[0]`, err.strerror)) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
615 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
616 elif count: |
| 14657 | 617 t = Coverage(ignore) |
| 618 try: | |
| 619 run(t.trace, 'execfile(' + `progname` + ')') | |
| 620 except IOError, err: | |
| 621 _err_exit("Cannot run file %s because: %s" % \ | |
| 622 (`sys.argv[0]`, err.strerror)) | |
| 623 except SystemExit: | |
| 624 pass | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
625 |
| 14657 | 626 results = t.results() |
| 627 # Add another lookup from the program's file name to its import name | |
| 628 # This give the right results, but I'm not sure why ... | |
| 629 results.modules[progname] = os.path.splitext(progname)[0] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
630 |
| 14657 | 631 if counts_file: |
| 632 # add in archived data, if available | |
| 633 try: | |
| 634 old_counts, old_modules = marshal.load(open(counts_file, 'rb')) | |
| 635 except IOError: | |
| 636 pass | |
| 637 else: | |
| 638 results.update(CoverageResults(old_counts, old_modules)) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
639 |
| 14657 | 640 if not no_report: |
| 641 create_results_log(results, logdir, missing) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
642 |
| 14657 | 643 if counts_file: |
| 644 try: | |
| 645 marshal.dump( (results.counts, results.modules), | |
| 646 open(counts_file, 'wb')) | |
| 647 except IOError, err: | |
| 648 _err_exit("Cannot save counts file %s because: %s" % \ | |
| 649 (`counts_file`, err.strerror)) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
650 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
651 elif report: |
| 14657 | 652 old_counts, old_modules = marshal.load(open(counts_file, 'rb')) |
| 653 results = CoverageResults(old_counts, old_modules) | |
| 654 create_results_log(results, logdir, missing) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
655 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
656 else: |
| 14657 | 657 assert 0, "Should never get here" |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
658 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
659 if __name__=='__main__': |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
660 main() |
