blob: f96e04f50372fa66c7a3f03b1f252d501046cf48 [file] [log] [blame]
Jeremy Hyltonda1ec462000-08-03 19:26:21 +00001#!/usr/bin/env python
2
Guido van Rossuma30eacf2001-11-28 19:41:45 +00003# portions copyright 2001, Autonomous Zones Industries, Inc., all rights...
4# err... reserved and offered to the public under the terms of the
5# Python 2.2 license.
6# Author: Zooko O'Whielacronx
7# http://zooko.com/
8# mailto:zooko@zooko.com
9#
Jeremy Hyltonda1ec462000-08-03 19:26:21 +000010# Copyright 2000, Mojam Media, Inc., all rights reserved.
11# Author: Skip Montanaro
12#
13# Copyright 1999, Bioreason, Inc., all rights reserved.
14# Author: Andrew Dalke
15#
16# Copyright 1995-1997, Automatrix, Inc., all rights reserved.
17# Author: Skip Montanaro
18#
19# Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved.
20#
21#
22# Permission to use, copy, modify, and distribute this Python software and
23# its associated documentation for any purpose without fee is hereby
24# granted, provided that the above copyright notice appears in all copies,
25# and that both that copyright notice and this permission notice appear in
26# supporting documentation, and that the name of neither Automatrix,
27# Bioreason or Mojam Media be used in advertising or publicity pertaining to
28# distribution of the software without specific, written prior permission.
29#
30#
Guido van Rossuma30eacf2001-11-28 19:41:45 +000031# Cleaned up the usage message --GvR 11/28/01
32#
33# Summary of even more recent changes, --Zooko 2001-10-14
34# Used new `inspect' module for better (?) determination of file<->module
35# mappings, line numbers, and source code.
36# Used new local trace function for faster (and better?) operation.
37# Removed "speed hack", which, as far as I can tell, meant that it would
38# ignore all files ??? (When I tried it, it would ignore only *most* of my
39# files. In any case with the speed hack removed in favor of actually
40# calling `Ignore.names()', it ignores only those files that I told it to
41# ignore, so I am happy.)
42# Rolled the `Coverage' class into `Trace', which now does either tracing or
43# counting or both according to constructor flags.
44# Moved the construction of the `Ignore' object inside the constructor of
45# `Trace', simplifying usage.
46# Changed function `create_results_log()' into method
47# `CoverageResults.write_results()'.
48# Add new mode "countfuncs" which is faster and which just reports which
49# functions were invoked.
50
51# Made `write_results' create `coverdir' if it doesn't already exist.
52# Moved the `run' funcs into `Trace' for simpler usage.
53# Use pickle instead of marshal for persistence.
54#
Jeremy Hyltonda1ec462000-08-03 19:26:21 +000055# Summary of recent changes:
56# Support for files with the same basename (submodules in packages)
57# Expanded the idea of how to ignore files or modules
58# Split tracing and counting into different classes
59# Extracted count information and reporting from the count class
60# Added some ability to detect which missing lines could be executed
61# Added pseudo-pragma to prohibit complaining about unexecuted lines
62# Rewrote the main program
63
64# Summary of older changes:
65# Added run-time display of statements being executed
66# Incorporated portability and performance fixes from Greg Stein
67# Incorporated main program from Michael Scharf
68
69"""
70program/module to trace Python program or function execution
71
72Sample use, command line:
73 trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs
74 trace.py -t --ignore-dir '$prefix' spam.py eggs
75
Guido van Rossuma30eacf2001-11-28 19:41:45 +000076Sample use, programmatically
77 # create a Trace object, telling it what to ignore, and whether to do tracing
78 # or line-counting or both.
79 trace = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, count=1)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +000080 # run the new command using the given trace
Guido van Rossuma30eacf2001-11-28 19:41:45 +000081 trace.run(coverage.globaltrace, 'main()')
Jeremy Hyltonda1ec462000-08-03 19:26:21 +000082 # make a report, telling it where you want output
Guido van Rossuma30eacf2001-11-28 19:41:45 +000083 trace.print_results(show_missing=1)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +000084"""
85
Guido van Rossuma30eacf2001-11-28 19:41:45 +000086import sys, os, string, tempfile, types, copy, operator, inspect, exceptions, marshal
87try:
88 import cPickle
89 pickle = cPickle
90except ImportError:
91 import pickle
92
93true = 1
94false = None
95
96# DEBUG_MODE=1 # make this true to get printouts which help you understand what's going on
Jeremy Hyltonda1ec462000-08-03 19:26:21 +000097
98def usage(outfile):
99 outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
100
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000101Meta-options:
102--help Display this help then exit.
103--version Output version information then exit.
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000104
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000105Otherwise, exactly one of the following three options must be given:
106-t, --trace Print each line to sys.stdout before it is executed.
107-c, --count Count the number of times each line is executed
108 and write the counts to <module>.cover for each
109 module executed, in the module's directory.
110 See also `--coverdir', `--file', `--no-report' below.
111-r, --report Generate a report from a counts file; do not execute
112 any code. `--file' must specify the results file to
113 read, which must have been created in a previous run
114 with `--count --file=FILE'.
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000115
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000116Modifiers:
117-f, --file=<file> File to accumulate counts over several runs.
118-R, --no-report Do not generate the coverage report files.
119 Useful if you want to accumulate over several runs.
120-C, --coverdir=<dir> Directory where the report files. The coverage
121 report for <package>.<module> is written to file
122 <dir>/<package>/<module>.cover.
123-m, --missing Annotate executable lines that were not executed
124 with '>>>>>> '.
125-s, --summary Write a brief summary on stdout for each file.
126 (Can only be used with --count or --report.)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000127
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000128Filters, may be repeated multiple times:
129--ignore-module=<mod> Ignore the given module and its submodules
130 (if it is a package).
131--ignore-dir=<dir> Ignore files in the given directory (multiple
132 directories can be joined by os.pathsep).
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000133""" % sys.argv[0])
134
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000135class Ignore:
136 def __init__(self, modules = None, dirs = None):
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000137 self._mods = modules or []
138 self._dirs = dirs or []
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000139
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000140 self._dirs = map(os.path.normpath, self._dirs)
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000141 self._ignore = { '<string>': 1 }
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000142
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000143 def names(self, filename, modulename):
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000144 if self._ignore.has_key(modulename):
145 return self._ignore[modulename]
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000146
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000147 # haven't seen this one before, so see if the module name is
148 # on the ignore list. Need to take some care since ignoring
149 # "cmp" musn't mean ignoring "cmpcache" but ignoring
150 # "Spam" must also mean ignoring "Spam.Eggs".
151 for mod in self._mods:
152 if mod == modulename: # Identical names, so ignore
153 self._ignore[modulename] = 1
154 return 1
155 # check if the module is a proper submodule of something on
156 # the ignore list
157 n = len(mod)
158 # (will not overflow since if the first n characters are the
159 # same and the name has not already occured, then the size
160 # of "name" is greater than that of "mod")
161 if mod == modulename[:n] and modulename[n] == '.':
162 self._ignore[modulename] = 1
163 return 1
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000164
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000165 # Now check that __file__ isn't in one of the directories
166 if filename is None:
167 # must be a built-in, so we must ignore
168 self._ignore[modulename] = 1
169 return 1
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000170
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000171 # Ignore a file when it contains one of the ignorable paths
172 for d in self._dirs:
173 # The '+ os.sep' is to ensure that d is a parent directory,
174 # as compared to cases like:
175 # d = "/usr/local"
176 # filename = "/usr/local.py"
177 # or
178 # d = "/usr/local.py"
179 # filename = "/usr/local.py"
180 if string.find(filename, d + os.sep) == 0:
181 self._ignore[modulename] = 1
182 return 1
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000183
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000184 # Tried the different ways, so we don't ignore this module
185 self._ignore[modulename] = 0
186 return 0
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000187
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000188class CoverageResults:
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000189 def __init__(self, counts=None, calledfuncs=None, infile=None, outfile=None):
190 self.counts = counts
191 if self.counts is None:
192 self.counts = {}
193 self.counter = self.counts.copy() # map (filename, lineno) to count
194 self.calledfuncs = calledfuncs
195 if self.calledfuncs is None:
196 self.calledfuncs = {}
197 self.calledfuncs = self.calledfuncs.copy()
198 self.infile = infile
199 self.outfile = outfile
200 if self.infile:
201 # try and merge existing counts file
202 try:
203 thingie = pickle.load(open(self.infile, 'r'))
204 if type(thingie) is types.DictType:
205 # backwards compatibility for old trace.py after Zooko touched it but before calledfuncs --Zooko 2001-10-24
206 self.update(self.__class__(thingie))
207 elif type(thingie) is types.TupleType and len(thingie) == 2:
208 (counts, calledfuncs,) = thingie
209 self.update(self.__class__(counts, calledfuncs))
210 except (IOError, EOFError,):
211 pass
212 except pickle.UnpicklingError:
213 # backwards compatibility for old trace.py before Zooko touched it --Zooko 2001-10-24
214 self.update(self.__class__(marshal.load(open(self.infile))))
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000215
216 def update(self, other):
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000217 """Merge in the data from another CoverageResults"""
218 counts = self.counts
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000219 calledfuncs = self.calledfuncs
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000220 other_counts = other.counts
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000221 other_calledfuncs = other.calledfuncs
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000222
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000223 for key in other_counts.keys():
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000224 if key != 'calledfuncs': # backwards compatibility for abortive attempt to stuff calledfuncs into self.counts, by Zooko --Zooko 2001-10-24
225 counts[key] = counts.get(key, 0) + other_counts[key]
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000226
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000227 for key in other_calledfuncs.keys():
228 calledfuncs[key] = 1
229
230 def write_results(self, show_missing = 1, summary = 0, coverdir = None):
231 """
232 @param coverdir
233 """
234 for (filename, modulename, funcname,) in self.calledfuncs.keys():
235 print "filename: %s, modulename: %s, funcname: %s" % (filename, modulename, funcname,)
236
237 import re
238 # turn the counts data ("(filename, lineno) = count") into something
239 # accessible on a per-file basis
240 per_file = {}
241 for thingie in self.counts.keys():
242 if thingie != "calledfuncs": # backwards compatibility for abortive attempt to stuff calledfuncs into self.counts, by Zooko --Zooko 2001-10-24
243 (filename, lineno,) = thingie
244 lines_hit = per_file[filename] = per_file.get(filename, {})
245 lines_hit[lineno] = self.counts[(filename, lineno)]
246
247 # there are many places where this is insufficient, like a blank
248 # line embedded in a multiline string.
249 blank = re.compile(r'^\s*(#.*)?$')
250
251 # accumulate summary info, if needed
252 sums = {}
253
254 # generate file paths for the coverage files we are going to write...
255 fnlist = []
256 tfdir = tempfile.gettempdir()
257 for key in per_file.keys():
258 filename = key
259
260 # skip some "files" we don't care about...
261 if filename == "<string>":
262 continue
263 # are these caused by code compiled using exec or something?
264 if filename.startswith(tfdir):
265 continue
266
267 modulename = inspect.getmodulename(filename)
268
269 if filename.endswith(".pyc") or filename.endswith(".pyo"):
270 filename = filename[:-1]
271
272 if coverdir:
273 thiscoverdir = coverdir
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000274 else:
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000275 thiscoverdir = os.path.dirname(os.path.abspath(filename))
276
277 # the code from here to "<<<" is the contents of the `fileutil.make_dirs()' function in the Mojo Nation project. --Zooko 2001-10-14
278 # http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/mojonation/evil/common/fileutil.py?rev=HEAD&content-type=text/vnd.viewcvs-markup
279 tx = None
280 try:
281 os.makedirs(thiscoverdir)
282 except OSError, x:
283 tx = x
284
285 if not os.path.isdir(thiscoverdir):
286 if tx:
287 raise tx
288 raise exceptions.IOError, "unknown error prevented creation of directory: %s" % thiscoverdir # careful not to construct an IOError with a 2-tuple, as that has a special meaning...
289 # <<<
290
291 # build list file name by appending a ".cover" to the module name
292 # and sticking it into the specified directory
293 if "." in modulename:
294 # A module in a package
295 finalname = modulename.split(".")[-1]
296 listfilename = os.path.join(thiscoverdir, finalname + ".cover")
297 else:
298 listfilename = os.path.join(thiscoverdir, modulename + ".cover")
299
300 # Get the original lines from the .py file
301 try:
302 lines = open(filename, 'r').readlines()
303 except IOError, err:
304 sys.stderr.write("trace: Could not open %s for reading because: %s - skipping\n" % (`filename`, err))
305 continue
306
307 try:
308 outfile = open(listfilename, 'w')
309 except IOError, err:
310 sys.stderr.write(
311 '%s: Could not open %s for writing because: %s" \
312 "- skipping\n' % ("trace", `listfilename`, err))
313 continue
314
315 # If desired, get a list of the line numbers which represent
316 # executable content (returned as a dict for better lookup speed)
317 if show_missing:
318 executable_linenos = find_executable_linenos(filename)
319 else:
320 executable_linenos = {}
321
322 n_lines = 0
323 n_hits = 0
324 lines_hit = per_file[key]
325 for i in range(len(lines)):
326 line = lines[i]
327
328 # do the blank/comment match to try to mark more lines
329 # (help the reader find stuff that hasn't been covered)
330 if lines_hit.has_key(i+1):
331 # count precedes the lines that we captured
332 outfile.write('%5d: ' % lines_hit[i+1])
333 n_hits = n_hits + 1
334 n_lines = n_lines + 1
335 elif blank.match(line):
336 # blank lines and comments are preceded by dots
337 outfile.write(' . ')
338 else:
339 # lines preceded by no marks weren't hit
340 # Highlight them if so indicated, unless the line contains
341 # '#pragma: NO COVER' (it is possible to embed this into
342 # the text as a non-comment; no easy fix)
343 if executable_linenos.has_key(i+1) and \
344 string.find(lines[i],
345 string.join(['#pragma', 'NO COVER'])) == -1:
346 outfile.write('>>>>>> ')
347 else:
348 outfile.write(' '*7)
349 n_lines = n_lines + 1
350 outfile.write(string.expandtabs(lines[i], 8))
351
352 outfile.close()
353
354 if summary and n_lines:
355 percent = int(100 * n_hits / n_lines)
356 sums[modulename] = n_lines, percent, modulename, filename
357
358 if summary and sums:
359 mods = sums.keys()
360 mods.sort()
361 print "lines cov% module (path)"
362 for m in mods:
363 n_lines, percent, modulename, filename = sums[m]
364 print "%5d %3d%% %s (%s)" % sums[m]
365
366 if self.outfile:
367 # try and store counts and module info into self.outfile
368 try:
369 pickle.dump((self.counts, self.calledfuncs,), open(self.outfile, 'w'), 1)
370 except IOError, err:
371 sys.stderr.write("cannot save counts files because %s" % err)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000372
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000373def _find_LINENO_from_code(code):
374 """return the numbers of the lines containing the source code that
375 was compiled into code"""
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000376 linenos = {}
377
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000378 line_increments = [ord(c) for c in code.co_lnotab[1::2]]
379 table_length = len(line_increments)
380
381 lineno = code.co_first_lineno
382
383 for li in line_increments:
384 linenos[lineno] = 1
385 lineno += li
386 linenos[lineno] = 1
387
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000388 return linenos
389
390def _find_LINENO(code):
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000391 """return all of the lineno information from a code object"""
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000392 import types
393
394 # get all of the lineno information from the code of this scope level
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000395 linenos = _find_LINENO_from_code(code)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000396
397 # and check the constants for references to other code objects
398 for c in code.co_consts:
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000399 if type(c) == types.CodeType:
400 # find another code object, so recurse into it
401 linenos.update(_find_LINENO(c))
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000402 return linenos
403
404def find_executable_linenos(filename):
405 """return a dict of the line numbers from executable statements in a file
406
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000407 """
408 import parser
409
Jeremy Hylton66a7e572001-05-08 04:20:52 +0000410 assert filename.endswith('.py')
411
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000412 prog = open(filename).read()
413 ast = parser.suite(prog)
414 code = parser.compileast(ast, filename)
415
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000416 return _find_LINENO(code)
417
418### XXX because os.path.commonprefix seems broken by my way of thinking...
419def commonprefix(dirs):
420 "Given a list of pathnames, returns the longest common leading component"
421 if not dirs: return ''
422 n = copy.copy(dirs)
423 for i in range(len(n)):
424 n[i] = n[i].split(os.sep)
425 prefix = n[0]
426 for item in n:
427 for i in range(len(prefix)):
428 if prefix[:i+1] <> item[:i+1]:
429 prefix = prefix[:i]
430 if i == 0: return ''
431 break
432 return os.sep.join(prefix)
Tim Peters70c43782001-01-17 08:48:39 +0000433
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000434class Trace:
435 def __init__(self, count=1, trace=1, countfuncs=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None):
436 """
437 @param count true iff it should count number of times each line is executed
438 @param trace true iff it should print out each line that is being counted
439 @param countfuncs true iff it should just output a list of (filename, modulename, funcname,) for functions that were called at least once; This overrides `count' and `trace'
440 @param ignoremods a list of the names of modules to ignore
441 @param ignoredirs a list of the names of directories to ignore all of the (recursive) contents of
442 @param infile file from which to read stored counts to be added into the results
443 @param outfile file in which to write the results
444 """
445 self.infile = infile
446 self.outfile = outfile
447 self.ignore = Ignore(ignoremods, ignoredirs)
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000448 self.counts = {} # keys are (filename, linenumber)
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000449 self.blabbed = {} # for debugging
450 self.pathtobasename = {} # for memoizing os.path.basename
451 self.donothing = 0
452 self.trace = trace
453 self._calledfuncs = {}
454 if countfuncs:
455 self.globaltrace = self.globaltrace_countfuncs
456 elif trace and count:
457 self.globaltrace = self.globaltrace_lt
458 self.localtrace = self.localtrace_trace_and_count
459 elif trace:
460 self.globaltrace = self.globaltrace_lt
461 self.localtrace = self.localtrace_trace
462 elif count:
463 self.globaltrace = self.globaltrace_lt
464 self.localtrace = self.localtrace_count
465 else:
466 # Ahem -- do nothing? Okay.
467 self.donothing = 1
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000468
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000469 def run(self, cmd):
470 import __main__
471 dict = __main__.__dict__
472 if not self.donothing:
473 sys.settrace(self.globaltrace)
474 try:
475 exec cmd in dict, dict
476 finally:
477 if not self.donothing:
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000478 sys.settrace(None)
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000479
480 def runctx(self, cmd, globals=None, locals=None):
481 if globals is None: globals = {}
482 if locals is None: locals = {}
483 if not self.donothing:
Skip Montanaro3a48ed92002-07-25 16:09:35 +0000484 sys.settrace(self.globaltrace)
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000485 try:
Skip Montanaro3a48ed92002-07-25 16:09:35 +0000486 exec cmd in globals, locals
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000487 finally:
488 if not self.donothing:
489 sys.settrace(None)
490
491 def runfunc(self, func, *args, **kw):
492 result = None
493 if not self.donothing:
494 sys.settrace(self.globaltrace)
495 try:
496 result = apply(func, args, kw)
497 finally:
498 if not self.donothing:
499 sys.settrace(None)
500 return result
501
502 def globaltrace_countfuncs(self, frame, why, arg):
503 """
504 Handles `call' events (why == 'call') and adds the (filename, modulename, funcname,) to the self._calledfuncs dict.
505 """
506 if why == 'call':
507 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 0)
508 if filename:
509 modulename = inspect.getmodulename(filename)
510 else:
511 modulename = None
512 self._calledfuncs[(filename, modulename, funcname,)] = 1
513
514 def globaltrace_lt(self, frame, why, arg):
515 """
516 Handles `call' events (why == 'call') and if the code block being entered is to be ignored then it returns `None', else it returns `self.localtrace'.
517 """
518 if why == 'call':
519 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 0)
520 # if DEBUG_MODE and not filename:
521 # print "%s.globaltrace(frame: %s, why: %s, arg: %s): filename: %s, lineno: %s, funcname: %s, context: %s, lineindex: %s\n" % (self, frame, why, arg, filename, lineno, funcname, context, lineindex,)
522 if filename:
523 modulename = inspect.getmodulename(filename)
Skip Montanaro3a48ed92002-07-25 16:09:35 +0000524 if modulename is not None:
525 ignore_it = self.ignore.names(filename, modulename)
526 # if DEBUG_MODE and not self.blabbed.has_key((filename, modulename,)):
527 # self.blabbed[(filename, modulename,)] = None
528 # print "%s.globaltrace(frame: %s, why: %s, arg: %s, filename: %s, modulename: %s, ignore_it: %s\n" % (self, frame, why, arg, filename, modulename, ignore_it,)
529 if not ignore_it:
530 if self.trace:
531 print " --- modulename: %s, funcname: %s" % (modulename, funcname,)
532 # if DEBUG_MODE:
533 # print "%s.globaltrace(frame: %s, why: %s, arg: %s, filename: %s, modulename: %s, ignore_it: %s -- about to localtrace\n" % (self, frame, why, arg, filename, modulename, ignore_it,)
534 return self.localtrace
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000535 else:
536 # XXX why no filename?
537 return None
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000538
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000539 def localtrace_trace_and_count(self, frame, why, arg):
540 if why == 'line':
541 # record the file name and line number of every trace
542 # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001-10-14
543 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame, 1)
544 key = (filename, lineno,)
545 self.counts[key] = self.counts.get(key, 0) + 1
546 # XXX not convinced that this memoizing is a performance win -- I don't know enough about Python guts to tell. --Zooko 2001-10-14
547 bname = self.pathtobasename.get(filename)
548 if bname is None:
549 # Using setdefault faster than two separate lines? --Zooko 2001-10-14
550 bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))
551 try:
552 print "%s(%d): %s" % (bname, lineno, context[lineindex],),
553 except IndexError:
554 # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2. Oh well.
555 pass
556 return self.localtrace
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000557
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000558 def localtrace_trace(self, frame, why, arg):
559 if why == 'line':
560 # XXX shouldn't do the count increment when arg is exception? But be careful to return self.localtrace when arg is exception! ? --Zooko 2001-10-14
561 # record the file name and line number of every trace
562 # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001-10-14
563 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame)
564 # if DEBUG_MODE:
565 # print "%s.localtrace_trace(frame: %s, why: %s, arg: %s); filename: %s, lineno: %s, funcname: %s, context: %s, lineindex: %s\n" % (self, frame, why, arg, filename, lineno, funcname, context, lineindex,)
566 # XXX not convinced that this memoizing is a performance win -- I don't know enough about Python guts to tell. --Zooko 2001-10-14
567 bname = self.pathtobasename.get(filename)
568 if bname is None:
569 # Using setdefault faster than two separate lines? --Zooko 2001-10-14
570 bname = self.pathtobasename.setdefault(filename, os.path.basename(filename))
Skip Montanaro3a48ed92002-07-25 16:09:35 +0000571 if context is not None:
572 try:
573 print "%s(%d): %s" % (bname, lineno, context[lineindex],),
574 except IndexError:
575 # Uh.. sometimes getframeinfo gives me a context of length 1 and a lineindex of -2. Oh well.
576 pass
577 else:
578 print "%s(???): ???" % bname
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000579 return self.localtrace
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000580
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000581 def localtrace_count(self, frame, why, arg):
582 if why == 'line':
583 # XXX shouldn't do the count increment when arg is exception? But be careful to return self.localtrace when arg is exception! ? --Zooko 2001-10-14
584 # record the file name and line number of every trace
585 # XXX I wish inspect offered me an optimized `getfilename(frame)' to use in place of the presumably heavier `getframeinfo()'. --Zooko 2001-10-14
586 (filename, lineno, funcname, context, lineindex,) = inspect.getframeinfo(frame)
587 key = (filename, lineno,)
588 self.counts[key] = self.counts.get(key, 0) + 1
589 return self.localtrace
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000590
591 def results(self):
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000592 return CoverageResults(self.counts, infile=self.infile, outfile=self.outfile, calledfuncs=self._calledfuncs)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000593
594def _err_exit(msg):
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000595 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000596 sys.exit(1)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000597
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000598def main(argv=None):
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000599 import getopt
600
601 if argv is None:
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000602 argv = sys.argv
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000603 try:
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000604 opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:l",
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000605 ["help", "version", "trace", "count",
606 "report", "no-report",
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000607 "file=", "missing",
Jeremy Hylton66a7e572001-05-08 04:20:52 +0000608 "ignore-module=", "ignore-dir=",
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000609 "coverdir=", "listfuncs",])
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000610
611 except getopt.error, msg:
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000612 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
613 sys.stderr.write("Try `%s --help' for more information\n" % sys.argv[0])
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000614 sys.exit(1)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000615
616 trace = 0
617 count = 0
618 report = 0
619 no_report = 0
620 counts_file = None
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000621 missing = 0
622 ignore_modules = []
623 ignore_dirs = []
Jeremy Hylton66a7e572001-05-08 04:20:52 +0000624 coverdir = None
625 summary = 0
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000626 listfuncs = false
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000627
628 for opt, val in opts:
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000629 if opt == "--help":
630 usage(sys.stdout)
631 sys.exit(0)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000632
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000633 if opt == "--version":
634 sys.stdout.write("trace 2.0\n")
635 sys.exit(0)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000636
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000637 if opt == "-l" or opt == "--listfuncs":
638 listfuncs = true
639 continue
640
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000641 if opt == "-t" or opt == "--trace":
642 trace = 1
643 continue
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000644
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000645 if opt == "-c" or opt == "--count":
646 count = 1
647 continue
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000648
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000649 if opt == "-r" or opt == "--report":
650 report = 1
651 continue
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000652
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000653 if opt == "-R" or opt == "--no-report":
654 no_report = 1
655 continue
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000656
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000657 if opt == "-f" or opt == "--file":
658 counts_file = val
659 continue
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000660
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000661 if opt == "-m" or opt == "--missing":
662 missing = 1
663 continue
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000664
Jeremy Hylton66a7e572001-05-08 04:20:52 +0000665 if opt == "-C" or opt == "--coverdir":
666 coverdir = val
667 continue
668
669 if opt == "-s" or opt == "--summary":
670 summary = 1
671 continue
672
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000673 if opt == "--ignore-module":
674 ignore_modules.append(val)
675 continue
676
677 if opt == "--ignore-dir":
678 for s in string.split(val, os.pathsep):
679 s = os.path.expandvars(s)
680 # should I also call expanduser? (after all, could use $HOME)
681
682 s = string.replace(s, "$prefix",
683 os.path.join(sys.prefix, "lib",
684 "python" + sys.version[:3]))
685 s = string.replace(s, "$exec_prefix",
686 os.path.join(sys.exec_prefix, "lib",
687 "python" + sys.version[:3]))
688 s = os.path.normpath(s)
689 ignore_dirs.append(s)
690 continue
691
692 assert 0, "Should never get here"
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000693
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000694 if listfuncs and (count or trace):
695 _err_exit("cannot specify both --listfuncs and (--trace or --count)")
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000696
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000697 if not count and not trace and not report and not listfuncs:
698 _err_exit("must specify one of --trace, --count, --report or --listfuncs")
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000699
700 if report and no_report:
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000701 _err_exit("cannot specify both --report and --no-report")
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000702
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000703 if report and not counts_file:
704 _err_exit("--report requires a --file")
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000705
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000706 if no_report and len(prog_argv) == 0:
707 _err_exit("missing name of file to run")
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000708
709 # everything is ready
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000710 if report:
711 results = CoverageResults(infile=counts_file, outfile=counts_file)
712 results.write_results(missing, summary=summary, coverdir=coverdir)
713 else:
714 sys.argv = prog_argv
715 progname = prog_argv[0]
716 if eval(sys.version[:3])>1.3:
717 sys.path[0] = os.path.split(progname)[0] # ???
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000718
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000719 t = Trace(count, trace, countfuncs=listfuncs, ignoremods=ignore_modules, ignoredirs=ignore_dirs, infile=counts_file, outfile=counts_file)
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000720 try:
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000721 t.run('execfile(' + `progname` + ')')
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000722 except IOError, err:
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000723 _err_exit("Cannot run file %s because: %s" % (`sys.argv[0]`, err))
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000724 except SystemExit:
725 pass
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000726
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000727 results = t.results()
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000728
Jeremy Hylton0b7b4b82000-09-18 01:46:01 +0000729 if not no_report:
Guido van Rossuma30eacf2001-11-28 19:41:45 +0000730 results.write_results(missing, summary=summary, coverdir=coverdir)
Jeremy Hyltonda1ec462000-08-03 19:26:21 +0000731
732if __name__=='__main__':
733 main()