diff options
Diffstat (limited to 'tools/hacking.py')
-rwxr-xr-x | tools/hacking.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tools/hacking.py b/tools/hacking.py index ea490f748..edb06525e 100755 --- a/tools/hacking.py +++ b/tools/hacking.py @@ -21,6 +21,7 @@ built on top of pep8.py """ +import fnmatch import inspect import logging import os @@ -49,6 +50,52 @@ DOCSTRING_TRIPLE = ['"""', "'''"] VERBOSE_MISSING_IMPORT = False +# Monkey patch broken excluded filter in pep8 +def filename_match(filename, patterns, default=True): + """ + Check if patterns contains a pattern that matches filename. + If patterns is unspecified, this always returns True. + """ + if not patterns: + return default + return any(fnmatch.fnmatch(filename, pattern) for pattern in patterns) + + +def excluded(filename): + """ + Check if options.exclude contains a pattern that matches filename. + """ + basename = os.path.basename(filename) + return any((filename_match(filename, pep8.options.exclude, + default=False), + filename_match(basename, pep8.options.exclude, + default=False))) + + +def input_dir(dirname, runner=None): + """ + Check all Python source files in this directory and all subdirectories. + """ + dirname = dirname.rstrip('/') + if excluded(dirname): + return + if runner is None: + runner = pep8.input_file + for root, dirs, files in os.walk(dirname): + if pep8.options.verbose: + print('directory ' + root) + pep8.options.counters['directories'] += 1 + dirs.sort() + for subdir in dirs[:]: + if excluded(os.path.join(root, subdir)): + dirs.remove(subdir) + files.sort() + for filename in files: + if pep8.filename_match(filename) and not excluded(filename): + pep8.options.counters['files'] += 1 + runner(os.path.join(root, filename)) + + def is_import_exception(mod): return (mod in IMPORT_EXCEPTIONS or any(mod.startswith(m + '.') for m in IMPORT_EXCEPTIONS)) @@ -417,6 +464,8 @@ if __name__ == "__main__": add_nova() pep8.current_file = current_file pep8.readlines = readlines + pep8.excluded = excluded + pep8.input_dir = input_dir try: pep8._main() finally: |