Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/trace.py @ 20737:00a581bdedeb legacy-trunk
When overriding __str__ or __repr__, set the tp_print slot to NULL.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 29 Oct 2001 14:33:44 +0000 |
| parents | 5a9880156d71 |
| children | 6392f03222e0 |
| 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) |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
77 -s,--summary Generate a brief summary for each file. (Can only |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
78 be used with -c or -r.) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
79 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
80 I/O: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
81 -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
|
82 (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
|
83 -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
|
84 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
|
85 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
|
86 -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
|
87 with a '>>>>>> '. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
88 -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
|
89 you want to accumulate several over tests. |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
90 -C,--coverdir= Generate .cover files in this directory |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
91 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
92 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
|
93 --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
|
94 of that module |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
95 --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
|
96 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
|
97 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
98 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
|
99 modules. |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
100 """ % sys.argv[0]) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
101 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
102 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
103 class Ignore: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
104 def __init__(self, modules = None, dirs = None): |
| 14657 | 105 self._mods = modules or [] |
| 106 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
|
107 |
| 14657 | 108 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
|
109 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
110 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
111 def names(self, filename, modulename): |
| 14657 | 112 if self._ignore.has_key(modulename): |
| 113 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
|
114 |
| 14657 | 115 # haven't seen this one before, so see if the module name is |
| 116 # on the ignore list. Need to take some care since ignoring | |
| 117 # "cmp" musn't mean ignoring "cmpcache" but ignoring | |
| 118 # "Spam" must also mean ignoring "Spam.Eggs". | |
| 119 for mod in self._mods: | |
| 120 if mod == modulename: # Identical names, so ignore | |
| 121 self._ignore[modulename] = 1 | |
| 122 return 1 | |
| 123 # check if the module is a proper submodule of something on | |
| 124 # the ignore list | |
| 125 n = len(mod) | |
| 126 # (will not overflow since if the first n characters are the | |
| 127 # same and the name has not already occured, then the size | |
| 128 # of "name" is greater than that of "mod") | |
| 129 if mod == modulename[:n] and modulename[n] == '.': | |
| 130 self._ignore[modulename] = 1 | |
| 131 return 1 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
132 |
| 14657 | 133 # Now check that __file__ isn't in one of the directories |
| 134 if filename is None: | |
| 135 # must be a built-in, so we must ignore | |
| 136 self._ignore[modulename] = 1 | |
| 137 return 1 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
138 |
| 14657 | 139 # Ignore a file when it contains one of the ignorable paths |
| 140 for d in self._dirs: | |
| 141 # The '+ os.sep' is to ensure that d is a parent directory, | |
| 142 # as compared to cases like: | |
| 143 # d = "/usr/local" | |
| 144 # filename = "/usr/local.py" | |
| 145 # or | |
| 146 # d = "/usr/local.py" | |
| 147 # filename = "/usr/local.py" | |
| 148 if string.find(filename, d + os.sep) == 0: | |
| 149 self._ignore[modulename] = 1 | |
| 150 return 1 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
151 |
| 14657 | 152 # Tried the different ways, so we don't ignore this module |
| 153 self._ignore[modulename] = 0 | |
| 154 return 0 | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
155 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
156 def run(trace, cmd): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
157 import __main__ |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
158 dict = __main__.__dict__ |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
159 sys.settrace(trace) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
160 try: |
| 14657 | 161 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
|
162 finally: |
| 14657 | 163 sys.settrace(None) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
164 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
165 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
|
166 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
|
167 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
|
168 sys.settrace(trace) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
169 try: |
| 14657 | 170 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
|
171 finally: |
| 14657 | 172 sys.settrace(None) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
173 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
174 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
|
175 result = None |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
176 sys.settrace(trace) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
177 try: |
| 14657 | 178 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
|
179 finally: |
| 14657 | 180 sys.settrace(None) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
181 return result |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
182 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
183 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
184 class CoverageResults: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
185 def __init__(self, counts = {}, modules = {}): |
| 14657 | 186 self.counts = counts.copy() # map (filename, lineno) to count |
| 187 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
|
188 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
189 def update(self, other): |
| 14657 | 190 """Merge in the data from another CoverageResults""" |
| 191 counts = self.counts | |
| 192 other_counts = other.counts | |
| 193 modules = self.modules | |
| 194 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
|
195 |
| 14657 | 196 for key in other_counts.keys(): |
| 197 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
|
198 |
| 14657 | 199 for key in other_modules.keys(): |
| 200 if modules.has_key(key): | |
| 201 # make sure they point to the same file | |
| 202 assert modules[key] == other_modules[key], \ | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
203 "Strange! filename %s has two different module " \ |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
204 "names: %s and %s" % \ |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
205 (key, modules[key], other_modules[key]) |
| 14657 | 206 else: |
| 207 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
|
208 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
209 # 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
|
210 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
|
211 """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
|
212 import dis |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
213 linenos = {} |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
214 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
215 # 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
|
216 n = len(co_code) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
217 i = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
218 prev_op = None |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
219 prev_lineno = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
220 while i < n: |
| 14657 | 221 c = co_code[i] |
| 222 op = ord(c) | |
| 223 if op == dis.SET_LINENO: | |
| 224 if prev_op == op: | |
| 225 # two SET_LINENO in a row, so the previous didn't | |
| 226 # indicate anything. This occurs with triple | |
| 227 # quoted strings (?). Remove the old one. | |
| 228 del linenos[prev_lineno] | |
| 229 prev_lineno = ord(co_code[i+1]) + ord(co_code[i+2])*256 | |
| 230 linenos[prev_lineno] = 1 | |
| 231 if op >= dis.HAVE_ARGUMENT: | |
| 232 i = i + 3 | |
| 233 else: | |
| 234 i = i + 1 | |
| 235 prev_op = op | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
236 return linenos |
|
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 def _find_LINENO(code): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
239 """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
|
240 import types |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
241 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
242 # 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
|
243 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
|
244 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
245 # 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
|
246 for c in code.co_consts: |
| 14657 | 247 if type(c) == types.CodeType: |
| 248 # find another code object, so recurse into it | |
| 249 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
|
250 return linenos |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
251 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
252 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
|
253 """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
|
254 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
255 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
|
256 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
|
257 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
258 """ |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
259 import parser |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
260 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
261 assert filename.endswith('.py') |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
262 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
263 prog = open(filename).read() |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
264 ast = parser.suite(prog) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
265 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
|
266 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
267 # 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
|
268 # 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
|
269 # the AST? |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
270 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
271 return _find_LINENO(code) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
272 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
273 ### 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
|
274 def commonprefix(dirs): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
275 "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
|
276 if not dirs: return '' |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
277 n = copy.copy(dirs) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
278 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
|
279 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
|
280 prefix = n[0] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
281 for item in n: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
282 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
|
283 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
|
284 prefix = prefix[:i] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
285 if i == 0: return '' |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
286 break |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
287 return os.sep.join(prefix) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
288 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
289 def create_results_log(results, dirname = ".", show_missing = 1, |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
290 save_counts = 0, summary = 0, coverdir = None): |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
291 import re |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
292 # 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
|
293 # 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
|
294 per_file = {} |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
295 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
|
296 lines_hit = per_file[filename] = per_file.get(filename, {}) |
| 14657 | 297 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
|
298 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
299 # 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
|
300 try: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
301 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
|
302 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
|
303 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
|
304 except IOError: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
305 pass |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
306 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
307 # 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
|
308 # 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
|
309 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
|
310 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
311 # accumulate summary info, if needed |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
312 sums = {} |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
313 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
314 # 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
|
315 fnlist = [] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
316 tfdir = tempfile.gettempdir() |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
317 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
|
318 filename = key |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
319 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
320 # 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
|
321 if filename == "<string>": |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
322 continue |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
323 # 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
|
324 if filename.startswith(tfdir): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
325 continue |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
326 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
327 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
|
328 |
| 14657 | 329 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
|
330 filename = filename[:-1] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
331 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
332 if coverdir: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
333 listfilename = os.path.join(coverdir, modulename + ".cover") |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
334 else: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
335 # XXX this is almost certainly not portable!!! |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
336 fndir = os.path.dirname(filename) |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
337 if os.path.isabs(filename): |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
338 coverpath = fndir |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
339 else: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
340 coverpath = os.path.join(dirname, fndir) |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
341 |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
342 # build list file name by appending a ".cover" to the module name |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
343 # and sticking it into the specified directory |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
344 if "." in modulename: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
345 # A module in a package |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
346 finalname = modulename.split(".")[-1] |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
347 listfilename = os.path.join(coverpath, finalname + ".cover") |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
348 else: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
349 listfilename = os.path.join(coverpath, modulename + ".cover") |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
350 |
| 14657 | 351 # Get the original lines from the .py file |
| 352 try: | |
| 353 lines = open(filename, 'r').readlines() | |
| 354 except IOError, err: | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
355 print >> sys.stderr, "trace: Could not open %s for reading " \ |
|
20322
5a9880156d71
Replace all instances of err.strerror with err.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17703
diff
changeset
|
356 "because: %s - skipping" % (`filename`, err) |
| 14657 | 357 continue |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
358 |
| 14657 | 359 try: |
| 360 outfile = open(listfilename, 'w') | |
| 361 except IOError, err: | |
| 362 sys.stderr.write( | |
| 363 '%s: Could not open %s for writing because: %s" \ | |
|
20322
5a9880156d71
Replace all instances of err.strerror with err.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17703
diff
changeset
|
364 "- skipping\n' % ("trace", `listfilename`, err)) |
| 14657 | 365 continue |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
366 |
| 14657 | 367 # If desired, get a list of the line numbers which represent |
| 368 # executable content (returned as a dict for better lookup speed) | |
| 369 if show_missing: | |
| 370 executable_linenos = find_executable_linenos(filename) | |
| 371 else: | |
| 372 executable_linenos = {} | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
373 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
374 n_lines = 0 |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
375 n_hits = 0 |
| 14657 | 376 lines_hit = per_file[key] |
| 377 for i in range(len(lines)): | |
| 378 line = lines[i] | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
379 |
| 14657 | 380 # do the blank/comment match to try to mark more lines |
| 381 # (help the reader find stuff that hasn't been covered) | |
| 382 if lines_hit.has_key(i+1): | |
| 383 # count precedes the lines that we captured | |
| 384 outfile.write('%5d: ' % lines_hit[i+1]) | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
385 n_hits = n_hits + 1 |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
386 n_lines = n_lines + 1 |
| 14657 | 387 elif blank.match(line): |
| 388 # blank lines and comments are preceded by dots | |
| 389 outfile.write(' . ') | |
| 390 else: | |
| 391 # lines preceded by no marks weren't hit | |
| 392 # Highlight them if so indicated, unless the line contains | |
| 393 # '#pragma: NO COVER' (it is possible to embed this into | |
| 394 # the text as a non-comment; no easy fix) | |
| 395 if executable_linenos.has_key(i+1) and \ | |
| 396 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
|
397 string.join(['#pragma', 'NO COVER'])) == -1: |
| 14657 | 398 outfile.write('>>>>>> ') |
| 399 else: | |
| 400 outfile.write(' '*7) | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
401 n_lines = n_lines + 1 |
| 14657 | 402 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
|
403 |
| 14657 | 404 outfile.close() |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
405 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
406 if summary and n_lines: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
407 percent = int(100 * n_hits / n_lines) |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
408 sums[modulename] = n_lines, percent, modulename, filename |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
409 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
410 if save_counts: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
411 # 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
|
412 try: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
413 marshal.dump(results.counts, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
414 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
|
415 marshal.dump(results.modules, |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
416 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
|
417 except IOError, err: |
| 14657 | 418 sys.stderr.write("cannot save counts/modules " \ |
|
20322
5a9880156d71
Replace all instances of err.strerror with err.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17703
diff
changeset
|
419 "files because %s" % err) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
420 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
421 if summary and sums: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
422 mods = sums.keys() |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
423 mods.sort() |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
424 print "lines cov% module (path)" |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
425 for m in mods: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
426 n_lines, percent, modulename, filename = sums[m] |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
427 print "%5d %3d%% %s (%s)" % sums[m] |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
428 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
429 # 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
|
430 # 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
|
431 # 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
|
432 # 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
|
433 # 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
|
434 # 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
|
435 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
436 class Coverage: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
437 def __init__(self, ignore = Ignore()): |
| 14657 | 438 self.ignore = ignore |
| 439 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
|
440 |
| 14657 | 441 self.counts = {} # keys are (filename, linenumber) |
| 442 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
|
443 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
444 def trace(self, frame, why, arg): |
| 14657 | 445 if why == 'line': |
| 446 # something is fishy about getting the file name | |
| 447 filename = frame.f_globals.get("__file__", None) | |
| 448 if filename is None: | |
| 449 filename = frame.f_code.co_filename | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
450 try: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
451 modulename = frame.f_globals["__name__"] |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
452 except KeyError: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
453 # PyRun_String() for example |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
454 # XXX what to do? |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
455 modulename = None |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
456 |
| 14657 | 457 # We do this next block to keep from having to make methods |
| 458 # calls, which also requires resetting the trace | |
| 459 ignore_it = self.ignore_names.get(modulename, -1) | |
| 460 if ignore_it == -1: # unknown filename | |
| 461 sys.settrace(None) | |
| 462 ignore_it = self.ignore.names(filename, modulename) | |
| 463 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
|
464 |
| 14657 | 465 # record the module name for every file |
| 466 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
|
467 |
| 14657 | 468 if not ignore_it: |
| 469 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
|
470 |
| 14657 | 471 # record the file name and line number of every trace |
| 472 key = (filename, lineno) | |
| 473 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
|
474 |
| 14657 | 475 return self.trace |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
476 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
477 def results(self): |
| 14657 | 478 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
|
479 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
480 class Trace: |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
481 def __init__(self, ignore = Ignore()): |
| 14657 | 482 self.ignore = ignore |
| 483 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
|
484 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
485 self.files = {'<string>': None} # stores lines from the .py file, |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
486 # or None |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
487 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
488 def trace(self, frame, why, arg): |
| 14657 | 489 if why == 'line': |
| 490 filename = frame.f_code.co_filename | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
491 try: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
492 modulename = frame.f_globals["__name__"] |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
493 except KeyError: |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
494 # PyRun_String() for example |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
495 # XXX what to do? |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
496 modulename = None |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
497 |
| 14657 | 498 # We do this next block to keep from having to make methods |
| 499 # calls, which also requires resetting the trace | |
| 500 ignore_it = self.ignore_names.get(modulename, -1) | |
| 501 if ignore_it == -1: # unknown filename | |
| 502 sys.settrace(None) | |
| 503 ignore_it = self.ignore.names(filename, modulename) | |
| 504 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
|
505 |
| 14657 | 506 if not ignore_it: |
| 507 lineno = frame.f_lineno | |
| 508 files = self.files | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
509 |
| 14657 | 510 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
|
511 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
|
512 open(filename).readlines()) |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
513 |
| 14657 | 514 # If you want to see filenames (the original behaviour), try: |
| 515 # modulename = filename | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
516 # or, prettier but confusing when several files have the |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
517 # same name |
| 14657 | 518 # 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
|
519 |
| 14657 | 520 if files[filename] != None: |
| 521 print '%s(%d): %s' % (os.path.basename(filename), lineno, | |
| 522 files[filename][lineno-1]) | |
| 523 else: | |
| 524 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
|
525 |
| 14657 | 526 return self.trace |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14657
diff
changeset
|
527 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
528 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
529 def _err_exit(msg): |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
530 print >> sys.stderr, "%s: %s" % (sys.argv[0], msg) |
| 14657 | 531 sys.exit(1) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
532 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
533 def main(argv = None): |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
534 import getopt |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
535 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
536 if argv is None: |
| 14657 | 537 argv = sys.argv |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
538 try: |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
539 opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:", |
| 14657 | 540 ["help", "version", "trace", "count", |
| 541 "report", "no-report", | |
| 542 "file=", "logdir=", "missing", | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
543 "ignore-module=", "ignore-dir=", |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
544 "coverdir="]) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
545 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
546 except getopt.error, msg: |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
547 print >> sys.stderr, "%s: %s" % (sys.argv[0], msg) |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
548 print >> sys.stderr, "Try `%s --help' for more information" \ |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
549 % sys.argv[0] |
| 14657 | 550 sys.exit(1) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
551 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
552 trace = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
553 count = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
554 report = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
555 no_report = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
556 counts_file = None |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
557 logdir = "." |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
558 missing = 0 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
559 ignore_modules = [] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
560 ignore_dirs = [] |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
561 coverdir = None |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
562 summary = 0 |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
563 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
564 for opt, val in opts: |
| 14657 | 565 if opt == "--help": |
| 566 usage(sys.stdout) | |
| 567 sys.exit(0) | |
| 568 | |
| 569 if opt == "--version": | |
| 570 sys.stdout.write("trace 2.0\n") | |
| 571 sys.exit(0) | |
| 572 | |
| 573 if opt == "-t" or opt == "--trace": | |
| 574 trace = 1 | |
| 575 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
576 |
| 14657 | 577 if opt == "-c" or opt == "--count": |
| 578 count = 1 | |
| 579 continue | |
| 580 | |
| 581 if opt == "-r" or opt == "--report": | |
| 582 report = 1 | |
| 583 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
584 |
| 14657 | 585 if opt == "-R" or opt == "--no-report": |
| 586 no_report = 1 | |
| 587 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
588 |
| 14657 | 589 if opt == "-f" or opt == "--file": |
| 590 counts_file = val | |
| 591 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
592 |
| 14657 | 593 if opt == "-d" or opt == "--logdir": |
| 594 logdir = val | |
| 595 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
596 |
| 14657 | 597 if opt == "-m" or opt == "--missing": |
| 598 missing = 1 | |
| 599 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
600 |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
601 if opt == "-C" or opt == "--coverdir": |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
602 coverdir = val |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
603 continue |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
604 |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
605 if opt == "-s" or opt == "--summary": |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
606 summary = 1 |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
607 continue |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
608 |
| 14657 | 609 if opt == "--ignore-module": |
| 610 ignore_modules.append(val) | |
| 611 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
612 |
| 14657 | 613 if opt == "--ignore-dir": |
| 614 for s in string.split(val, os.pathsep): | |
| 615 s = os.path.expandvars(s) | |
| 616 # 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
|
617 |
| 14657 | 618 s = string.replace(s, "$prefix", |
| 619 os.path.join(sys.prefix, "lib", | |
| 620 "python" + sys.version[:3])) | |
| 621 s = string.replace(s, "$exec_prefix", | |
| 622 os.path.join(sys.exec_prefix, "lib", | |
| 623 "python" + sys.version[:3])) | |
| 624 s = os.path.normpath(s) | |
| 625 ignore_dirs.append(s) | |
| 626 continue | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
627 |
| 14657 | 628 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
|
629 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
630 if len(prog_argv) == 0: |
| 14657 | 631 _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
|
632 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
633 if count + trace + report > 1: |
| 14657 | 634 _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
|
635 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
636 if count + trace + report == 0: |
| 14657 | 637 _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
|
638 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
639 if report and counts_file is None: |
| 14657 | 640 _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
|
641 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
642 if report and no_report: |
| 14657 | 643 _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
|
644 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
645 if logdir is not None: |
| 14657 | 646 # warn if the directory doesn't exist, but keep on going |
| 647 # (is this the correct behaviour?) | |
| 648 if not os.path.isdir(logdir): | |
| 649 sys.stderr.write( | |
| 650 "trace: WARNING, --logdir directory %s is not available\n" % | |
| 651 `logdir`) | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
652 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
653 sys.argv = prog_argv |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
654 progname = prog_argv[0] |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
655 if eval(sys.version[:3])>1.3: |
| 14657 | 656 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
|
657 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
658 # everything is ready |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
659 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
|
660 if trace: |
| 14657 | 661 t = Trace(ignore) |
| 662 try: | |
| 663 run(t.trace, 'execfile(' + `progname` + ')') | |
| 664 except IOError, err: | |
| 665 _err_exit("Cannot run file %s because: %s" % \ | |
|
20322
5a9880156d71
Replace all instances of err.strerror with err.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17703
diff
changeset
|
666 (`sys.argv[0]`, err)) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
667 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
668 elif count: |
| 14657 | 669 t = Coverage(ignore) |
| 670 try: | |
| 671 run(t.trace, 'execfile(' + `progname` + ')') | |
| 672 except IOError, err: | |
| 673 _err_exit("Cannot run file %s because: %s" % \ | |
|
20322
5a9880156d71
Replace all instances of err.strerror with err.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17703
diff
changeset
|
674 (`sys.argv[0]`, err)) |
| 14657 | 675 except SystemExit: |
| 676 pass | |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
677 |
| 14657 | 678 results = t.results() |
| 679 # Add another lookup from the program's file name to its import name | |
| 680 # This give the right results, but I'm not sure why ... | |
| 681 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
|
682 |
| 14657 | 683 if counts_file: |
| 684 # add in archived data, if available | |
| 685 try: | |
| 686 old_counts, old_modules = marshal.load(open(counts_file, 'rb')) | |
| 687 except IOError: | |
| 688 pass | |
| 689 else: | |
| 690 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
|
691 |
| 14657 | 692 if not no_report: |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
693 create_results_log(results, logdir, missing, |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
694 summary=summary, coverdir=coverdir) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
695 |
| 14657 | 696 if counts_file: |
| 697 try: | |
| 698 marshal.dump( (results.counts, results.modules), | |
| 699 open(counts_file, 'wb')) | |
| 700 except IOError, err: | |
| 701 _err_exit("Cannot save counts file %s because: %s" % \ | |
|
20322
5a9880156d71
Replace all instances of err.strerror with err.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17703
diff
changeset
|
702 (`counts_file`, err)) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
703 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
704 elif report: |
| 14657 | 705 old_counts, old_modules = marshal.load(open(counts_file, 'rb')) |
| 706 results = CoverageResults(old_counts, old_modules) | |
|
17703
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
707 create_results_log(results, logdir, missing, |
|
f3b8785bd022
Fix several bugs and add two features.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15930
diff
changeset
|
708 summary=summary, coverdir=coverdir) |
|
13838
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
709 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
710 else: |
| 14657 | 711 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
|
712 |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
713 if __name__=='__main__': |
|
91d95a7b7858
Python code coverage tool by Skip Montanaro and Andrew Dalke
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
714 main() |
