From b2b4cae8e3ef27b8545a8d98a35dc7b07b1b132f Mon Sep 17 00:00:00 2001 From: Leonardo Rochael Almeida Date: Wed, 20 Jul 2016 16:43:34 -0300 Subject: [PATCH] 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() --- src/flake8/checker.py | 15 ++++++++++----- src/flake8/utils.py | 26 ++++++++++++++------------ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index f7bd540..d5132ff 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -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) diff --git a/src/flake8/utils.py b/src/flake8/utils.py index 68ed530..52417fc 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -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