annotate Tools/scripts/trace.py @ 14150:302dfd53b3c8 legacy-trunk

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