diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2010-05-29 23:51:53 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2010-05-29 23:51:53 -0400 |
commit | ec6b5187461bfe701e0741c35375a0e341c07e78 (patch) | |
tree | ba7d374837ce77cc9c6cd2102e355a9afb0654a2 /coverage/control.py | |
parent | 366d15ff4949f6f13ccae5e800248a6b624a9ee2 (diff) | |
parent | b5990d4ceb60c3028575a11f8d7fd4080411ed67 (diff) | |
download | python-coveragepy-git-ec6b5187461bfe701e0741c35375a0e341c07e78.tar.gz |
Merge latest code from main coverage.py repo
Diffstat (limited to 'coverage/control.py')
-rw-r--r-- | coverage/control.py | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/coverage/control.py b/coverage/control.py index d07abaf3..7936c0d0 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -1,6 +1,6 @@ """Core control stuff for Coverage.""" -import atexit, os, random, socket, sys +import atexit, fnmatch, os, random, socket, sys from coverage.annotate import AnnotateReporter from coverage.backward import string_class @@ -32,7 +32,7 @@ class coverage(object): def __init__(self, data_file=None, data_suffix=None, cover_pylib=None, auto_data=False, timid=None, branch=None, config_file=True, - omit_prefixes=None, include_prefixes=None): + omit=None, include=None): """ `data_file` is the base name of the data file to use, defaulting to ".coverage". `data_suffix` is appended (with a dot) to `data_file` to @@ -59,9 +59,8 @@ class coverage(object): standard file is read (".coveragerc"). If it is False, then no file is read. - `omit_prefixes` and `include_prefixes` are lists of filename prefixes. - Files that match `include_prefixes` will be measured, files that match - `omit_prefixes` will not. + `include` and `omit` are lists of filename patterns. Files that match + `include` will be measured, files that match `omit` will not. """ from coverage import __version__ @@ -86,8 +85,7 @@ class coverage(object): self.config.from_args( data_file=data_file, cover_pylib=cover_pylib, timid=timid, branch=branch, parallel=bool_or_none(data_suffix), - omit_prefixes=omit_prefixes, - include_prefixes=include_prefixes + omit=omit, include=include ) self.auto_data = auto_data @@ -98,8 +96,8 @@ class coverage(object): self.file_locator = FileLocator() - self.omit_prefixes = self._abs_files(self.config.omit_prefixes) - self.include_prefixes = self._abs_files(self.config.include_prefixes) + self.omit = self._abs_files(self.config.omit) + self.include = self._abs_files(self.config.include) self.collector = Collector( self._should_trace, timid=self.config.timid, @@ -127,17 +125,26 @@ class coverage(object): collector="coverage v%s" % __version__ ) - # The prefix for files considered "installed with the interpreter". + # The dirs for files considered "installed with the interpreter". if not self.config.cover_pylib: # Look at where the "os" module is located. That's the indication # for "installed with the interpreter". - os_file = self.file_locator.canonical_filename(os.__file__) - self.pylib_prefix = os.path.split(os_file)[0] + os_dir = self.canonical_dir(os.__file__) + self.pylib_dirs = [os_dir] + + # In a virtualenv, there're actually two lib directories. Find the + # other one. This is kind of ad-hoc, but it works. + random_dir = self.canonical_dir(random.__file__) + if random_dir != os_dir: + self.pylib_dirs.append(random_dir) # To avoid tracing the coverage code itself, we skip anything located # where we are. - here = self.file_locator.canonical_filename(__file__) - self.cover_prefix = os.path.split(here)[0] + self.cover_dir = self.canonical_dir(__file__) + + def canonical_dir(self, f): + """Return the canonical directory of the file `f`.""" + return os.path.split(self.file_locator.canonical_filename(f))[0] def _should_trace(self, filename, frame): """Decide whether to trace execution in `filename` @@ -170,27 +177,28 @@ class coverage(object): filename = dunder_file canonical = self.file_locator.canonical_filename(filename) + canon_dir = os.path.split(canonical)[0] # If we aren't supposed to trace installed code, then check if this is # near the Python standard library and skip it if so. if not self.config.cover_pylib: - if canonical.startswith(self.pylib_prefix): + if canon_dir in self.pylib_dirs: return False # We exclude the coverage code itself, since a little of it will be # measured otherwise. - if canonical.startswith(self.cover_prefix): + if canon_dir == self.cover_dir: return False - # Check the file against the include and omit prefixes. - if self.include_prefixes: - for prefix in self.include_prefixes: - if canonical.startswith(prefix): + # Check the file against the include and omit patterns. + if self.include: + for pattern in self.include: + if fnmatch.fnmatch(canonical, pattern): break else: return False - for prefix in self.omit_prefixes: - if canonical.startswith(prefix): + for pattern in self.omit: + if fnmatch.fnmatch(canonical, pattern): return False return canonical @@ -349,33 +357,31 @@ class coverage(object): def report(self, morfs=None, show_missing=True, ignore_errors=None, file=None, # pylint: disable-msg=W0622 - omit_prefixes=None, include_prefixes=None + omit=None, include=None ): """Write a summary report to `file`. Each module in `morfs` is listed, with counts of statements, executed statements, missing statements, and a list of lines missed. - `include_prefixes` is a list of filename prefixes. Modules that match - those prefixes will be included in the report. Modules that match - `omit_prefixes` will not be included in the report. + `include` is a list of filename patterns. Modules whose filenames + match those patterns will be included in the report. Modules matching + `omit` will not be included in the report. """ self.config.from_args( - ignore_errors=ignore_errors, - omit_prefixes=omit_prefixes, - include_prefixes=include_prefixes + ignore_errors=ignore_errors, omit=omit, include=include ) reporter = SummaryReporter( self, show_missing, self.config.ignore_errors ) reporter.report( - morfs, outfile=file, omit_prefixes=self.config.omit_prefixes, - include_prefixes=self.config.include_prefixes + morfs, outfile=file, omit=self.config.omit, + include=self.config.include ) def annotate(self, morfs=None, directory=None, ignore_errors=None, - omit_prefixes=None, include_prefixes=None): + omit=None, include=None): """Annotate a list of modules. Each module in `morfs` is annotated. The source is written to a new @@ -387,39 +393,35 @@ class coverage(object): """ self.config.from_args( - ignore_errors=ignore_errors, - omit_prefixes=omit_prefixes, - include_prefixes=include_prefixes + ignore_errors=ignore_errors, omit=omit, include=include ) reporter = AnnotateReporter(self, self.config.ignore_errors) reporter.report( morfs, directory=directory, - omit_prefixes=self.config.omit_prefixes, - include_prefixes=self.config.include_prefixes + omit=self.config.omit, + include=self.config.include ) def html_report(self, morfs=None, directory=None, ignore_errors=None, - omit_prefixes=None, include_prefixes=None): + omit=None, include=None): """Generate an HTML report. See `coverage.report()` for other arguments. """ self.config.from_args( - ignore_errors=ignore_errors, - omit_prefixes=omit_prefixes, - include_prefixes=include_prefixes, + ignore_errors=ignore_errors, omit=omit, include=include, html_dir=directory, ) reporter = HtmlReporter(self, self.config.ignore_errors) reporter.report( morfs, directory=self.config.html_dir, - omit_prefixes=self.config.omit_prefixes, - include_prefixes=self.config.include_prefixes + omit=self.config.omit, + include=self.config.include ) def xml_report(self, morfs=None, outfile=None, ignore_errors=None, - omit_prefixes=None, include_prefixes=None): + omit=None, include=None): """Generate an XML report of coverage results. The report is compatible with Cobertura reports. @@ -431,9 +433,7 @@ class coverage(object): """ self.config.from_args( - ignore_errors=ignore_errors, - omit_prefixes=omit_prefixes, - include_prefixes=include_prefixes, + ignore_errors=ignore_errors, omit=omit, include=include, xml_output=outfile, ) file_to_close = None @@ -446,8 +446,8 @@ class coverage(object): try: reporter = XmlReporter(self, self.config.ignore_errors) reporter.report( - morfs, omit_prefixes=self.config.omit_prefixes, - include_prefixes=self.config.include_prefixes, outfile=outfile + morfs, omit=self.config.omit, include=self.config.include, + outfile=outfile ) finally: if file_to_close: @@ -462,8 +462,8 @@ class coverage(object): info = [ ('version', covmod.__version__), ('coverage', covmod.__file__), - ('cover_prefix', self.cover_prefix), - ('pylib_prefix', self.pylib_prefix), + ('cover_dir', self.cover_dir), + ('pylib_dirs', self.pylib_dirs), ('tracer', self.collector.tracer_name()), ('data_path', self.data.filename), ('python', sys.version.replace('\n', '')), |