Allow stdin and directly named files to be excluded from check

For the sake of IDEs, check filename for exclusion even if the file is directly
named in the command line.

Also, if the filename is "-" (stdin) check the provided display name for
exclusion.

Also, avoid calling path checking functions on the "-" filename:

 * fnmatch.fnmatch()
 * os.path.isdir()
 * os.path.exists()
This commit is contained in:
Leonardo Rochael Almeida 2016-07-20 16:43:34 -03:00
parent d69cb0564a
commit b2b4cae8e3
2 changed files with 24 additions and 17 deletions

View file

@ -237,6 +237,12 @@ class Manager(object):
exclude = self.options.exclude
if not exclude:
return False
if path == '-':
# stdin, use display name to check exclusion, if present
path = self.options.stdin_display_name
if path is None:
LOG.debug("unnamed stdin has not been excluded")
return False
basename = os.path.basename(path)
if utils.fnmatch(basename, exclude):
LOG.debug('"%s" has been excluded', basename)
@ -263,12 +269,11 @@ class Manager(object):
# best solution right now.
def should_create_file_checker(filename):
"""Determine if we should create a file checker."""
matches_filename_patterns = utils.fnmatch(
filename, filename_patterns
return (
filename == '-' or # stdin
utils.fnmatch(filename, filename_patterns) and
os.path.exists(filename)
)
is_stdin = filename == '-'
file_exists = os.path.exists(filename)
return (file_exists and matches_filename_patterns) or is_stdin
self.checkers = [
FileChecker(filename, self.checks, self.style_guide)

View file

@ -208,24 +208,26 @@ def filenames_from(arg, predicate=None):
"""
if predicate is None:
predicate = _default_predicate
if os.path.isdir(arg):
for root, sub_directories, files in os.walk(arg):
if predicate(root):
sub_directories[:] = []
continue
if predicate(arg):
return
if arg == "-":
# stdin, don't call isdir()
yield arg
elif os.path.isdir(arg):
for root, sub_directories, files in os.walk(arg):
# NOTE(sigmavirus24): os.walk() will skip a directory if you
# remove it from the list of sub-directories.
for directory in sub_directories:
joined = os.path.join(root, directory)
if predicate(directory) or predicate(joined):
sub_directories.remove(directory)
sub_directories[:] = [
directory for directory in sub_directories
if not predicate(os.path.join(root, directory))
]
for filename in files:
joined = os.path.join(root, filename)
if predicate(joined) or predicate(filename):
continue
yield joined
if not predicate(joined):
yield joined
else:
yield arg