diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2010-06-13 21:46:35 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2010-06-13 21:46:35 -0400 |
commit | 42afb74959d043efa3d30873703736d3552aebfa (patch) | |
tree | e4df7b64bfdbb463896a2bfcebb8c31a64a28c08 /coverage/files.py | |
parent | 5acd2aed8f2654a120bc8e74feafa523cbbc0c6f (diff) | |
download | python-coveragepy-42afb74959d043efa3d30873703736d3552aebfa.tar.gz |
The 'source' option is a list of directories or packages to limit coverage's attention.
Diffstat (limited to 'coverage/files.py')
-rw-r--r-- | coverage/files.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/coverage/files.py b/coverage/files.py index 5690679..d74b4d7 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -1,6 +1,6 @@ """File wrangling.""" -import os, sys +import fnmatch, os, sys class FileLocator(object): """Understand how filenames work.""" @@ -76,3 +76,37 @@ class FileLocator(object): data = data.decode('utf8') # TODO: How to do this properly? return data return None + + +class TreeMatcher(object): + """A matcher for files in a tree.""" + def __init__(self, directories): + self.dirs = directories[:] + + def add(self, directory): + """Add another directory to the list we match for.""" + self.dirs.append(directory) + + def match(self, fpath): + """Does `fpath` indicate a file in one of our trees?""" + for d in self.dirs: + if fpath.startswith(d): + if fpath == d: + # This is the same file! + return True + if fpath[len(d)] == os.sep: + # This is a file in the directory + return True + return False + +class FnmatchMatcher(object): + """A matcher for files by filename pattern.""" + def __init__(self, pats): + self.pats = pats[:] + + def match(self, fpath): + """Does `fpath` match one of our filename patterns?""" + for pat in self.pats: + if fnmatch.fnmatch(fpath, pat): + return True + return False |