def make_checkers(self, paths=None): # type: (List[str]) -> NoneType """Create checkers for each file.""" if paths is None: paths = self.arguments if not paths: paths = ['.'] filename_patterns = self.options.filename # NOTE(sigmavirus24): Yes this is a little unsightly, but it's our # 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 ) is_stdin = filename == '-' file_exists = os.path.exists(filename) return (file_exists and matches_filename_patterns) or is_stdin checks = self.checks.to_dictionary() self.checkers = [ FileChecker(filename, checks, self.options) for argument in paths for filename in utils.filenames_from(argument, self.is_path_excluded) if should_create_file_checker(filename) ] LOG.info('Checking %d files', len(self.checkers))
def make_checkers(self, paths=None): # type: (List[str]) -> None """ Reloaded checkers generator to provide one checker per file per rule. Original `make_checkers` provides checker per file with all rules mixed. It makes difficult to filter checks by codes after all. """ if paths is None: paths = self.arguments if not paths: paths = ['.'] self.checkers = [] for argument in paths: for filename in filenames_from(argument, self.is_path_excluded): for check_type, checks in self.checks.to_dictionary().items(): for check in checks: checker = self._make_checker( argument=argument, filename=filename, check_type=check_type, check=check, ) if checker is not None: self.checkers.append(checker)
def make_checkers(self, paths: List[str] = None) -> None: """ Reloaded checkers generator to provide one checker per file per rule. Original `make_checkers` provides checker per file with all rules mixed. It makes difficult to filter checks by codes after all. """ if paths is None: paths = self.arguments if not paths: paths = ['.'] prepare_cache() # `checkers` is list of checks to run (and then cache) # check is a combination of plugin and file. self.checkers = [] # `snapshots` is the list of checks that have cache and should not be run self.snapshots = [] for argument in paths: for filename in filenames_from(argument, self.is_path_excluded): # get checks list selected_checks: Dict[str, List[Dict[str, Any]]] selected_checks = dict( ast_plugins=[], logical_line_plugins=[], physical_line_plugins=[], ) has_checks = False for check_type, checks in self.checks.to_dictionary().items(): for check in checks: should_process = self._should_process( argument=argument, filename=filename, check_type=check_type, check=check, ) if not should_process: continue selected_checks[check_type].append(check) has_checks = True # Create checker with selected checks if not has_checks: continue checker = FlakeHellFileChecker( filename=filename, checks=selected_checks, options=self.options, ) # ignore files with top-level `# flake8: noqa` if not checker.should_process: continue checker.snapshot = Snapshot.create( checker=checker, options=self.options, ) if checker.snapshot.exists(): self.snapshots.append(checker) continue self.checkers.append(checker)
def test_filenames_from_a_directory_with_a_predicate(): """Verify that predicates filter filenames_from.""" filenames = list(utils.filenames_from( arg='src/flake8/', predicate=lambda filename: filename == 'flake8/__init__.py', )) assert len(filenames) > 2 assert 'flake8/__init__.py' not in filenames
def test_filenames_from_a_directory_with_a_predicate_from_the_current_dir(): """Verify that predicates filter filenames_from.""" filenames = set(utils.filenames_from( arg=_normpath('./a/b'), predicate=lambda path: path == 'c.py', )) # none should have matched the predicate so all returned expected = _normpaths(('./a/b/c.py', './a/b/d.py', './a/b/e/f.py')) assert filenames == expected
def test_filenames_from_a_directory_with_a_predicate(): """Verify that predicates filter filenames_from.""" filenames = set(utils.filenames_from( arg=_normpath('a/b/'), predicate=lambda path: path.endswith(_normpath('b/c.py')), )) # should not include c.py expected = _normpaths(('a/b/d.py', 'a/b/e/f.py')) assert filenames == expected
def make_checkers(self, paths=None): # type: (List[str]) -> NoneType """Create checkers for each file.""" if paths is None: paths = self.arguments if not paths: paths = ['.'] filename_patterns = self.options.filename running_from_vcs = self.options._running_from_vcs running_from_diff = self.options.diff # NOTE(sigmavirus24): Yes this is a little unsightly, but it's our # best solution right now. def should_create_file_checker(filename, argument): """Determine if we should create a file checker.""" matches_filename_patterns = utils.fnmatch( filename, filename_patterns ) is_stdin = filename == '-' # NOTE(sigmavirus24): If a user explicitly specifies something, # e.g, ``flake8 bin/script`` then we should run Flake8 against # that. Since should_create_file_checker looks to see if the # filename patterns match the filename, we want to skip that in # the event that the argument and the filename are identical. # If it was specified explicitly, the user intended for it to be # checked. explicitly_provided = (not running_from_vcs and not running_from_diff and (argument == filename)) return ((explicitly_provided or matches_filename_patterns) or is_stdin) checks = self.checks.to_dictionary() checkers = ( FileChecker(filename, checks, self.options) for argument in paths for filename in utils.filenames_from(argument, self.is_path_excluded) if should_create_file_checker(filename, argument) ) # errors may already have been captured during checker set up, # preserve these malfunctioning checkers in a separate list so reports # can be generated self.checkers = [] self.unprocessed_checkers = [] for checker in checkers: if checker.should_process: self.checkers.append(checker) else: self.unprocessed_checkers.append(checker) LOG.info('Checking %d files', len(self.checkers))
def make_checkers(self, paths=None): # type: (List[str]) -> NoneType """Create checkers for each file.""" if paths is None: paths = self.arguments if not paths: paths = ["."] filename_patterns = self.options.filename running_from_vcs = self.options._running_from_vcs running_from_diff = self.options.diff # NOTE(sigmavirus24): Yes this is a little unsightly, but it's our # best solution right now. def should_create_file_checker(filename, argument): """Determine if we should create a file checker.""" matches_filename_patterns = utils.fnmatch( filename, filename_patterns ) is_stdin = filename == "-" file_exists = os.path.exists(filename) # NOTE(sigmavirus24): If a user explicitly specifies something, # e.g, ``flake8 bin/script`` then we should run Flake8 against # that. Since should_create_file_checker looks to see if the # filename patterns match the filename, we want to skip that in # the event that the argument and the filename are identical. # If it was specified explicitly, the user intended for it to be # checked. explicitly_provided = ( not running_from_vcs and not running_from_diff and (argument == filename) ) return ( file_exists and (explicitly_provided or matches_filename_patterns) ) or is_stdin checks = self.checks.to_dictionary() checkers = ( FileChecker(filename, checks, self.options) for argument in paths for filename in utils.filenames_from( argument, self.is_path_excluded ) if should_create_file_checker(filename, argument) ) self.checkers = [ checker for checker in checkers if checker.should_process ] LOG.info("Checking %d files", len(self.checkers))
def test_filenames_from_exclude_doesnt_exclude_directory_names(tmpdir): """Verify that we don't greedily exclude subdirs.""" tmpdir.join('1').ensure_dir().join('dont_return_me.py').ensure() tmpdir.join('2').join('1').ensure_dir().join('return_me.py').ensure() exclude = [tmpdir.join('1').strpath] # This acts similar to src.flake8.checker.is_path_excluded def predicate(pth): return utils.fnmatch(os.path.abspath(pth), exclude) with tmpdir.as_cwd(): filenames = list(utils.filenames_from('.', predicate)) assert filenames == [os.path.join('.', '2', '1', 'return_me.py')]
def make_checkers(self, paths=None): # type: (List[str]) -> None """Create checkers for each file.""" if paths is None: paths = self.arguments if not paths: paths = ["."] filename_patterns = self.options.filename running_from_vcs = self.options._running_from_vcs running_from_diff = self.options.diff # NOTE(sigmavirus24): Yes this is a little unsightly, but it's our # best solution right now. def should_create_file_checker(filename, argument): """Determine if we should create a file checker.""" matches_filename_patterns = utils.fnmatch( filename, filename_patterns ) is_stdin = filename == "-" # NOTE(sigmavirus24): If a user explicitly specifies something, # e.g, ``flake8 bin/script`` then we should run Flake8 against # that. Since should_create_file_checker looks to see if the # filename patterns match the filename, we want to skip that in # the event that the argument and the filename are identical. # If it was specified explicitly, the user intended for it to be # checked. explicitly_provided = ( not running_from_vcs and not running_from_diff and (argument == filename) ) return ( explicitly_provided or matches_filename_patterns ) or is_stdin checks = self.checks.to_dictionary() checkers = ( FileChecker(filename, checks, self.options) for argument in paths for filename in utils.filenames_from( argument, self.is_path_excluded ) if should_create_file_checker(filename, argument) ) self.checkers = [ checker for checker in checkers if checker.should_process ] LOG.info("Checking %d files", len(self.checkers))
def make_checkers(self, paths: List[str] = None) -> None: """ Reloaded checkers generator to provide one checker per file per rule. Original `make_checkers` provides checker per file with all rules mixed. It makes difficult to filter checks by codes after all. """ if paths is None: paths = self.arguments if not paths: paths = ['.'] prepare_cache() # `checkers` is list of checks to run (and then cache) # check is a combination of plugin and file. self.checkers = [] # `snapshots` is the list of checks that have cache and should not be run self.snapshots = [] for argument in paths: for filename in filenames_from(argument, self.is_path_excluded): for check_type, checks in self.checks.to_dictionary().items(): for check in checks: checker = self._make_checker( argument=argument, filename=filename, check_type=check_type, check=check, ) if checker is None: continue checker.snapshot = Snapshot.create( checker=checker, options=self.options, ) if checker.snapshot.exists(): self.snapshots.append(checker) continue self.checkers.append(checker)
def test_filenames_from_a_directory(): """Verify that filenames_from walks a directory.""" filenames = list(utils.filenames_from('src/flake8/')) assert len(filenames) > 2 assert 'src/flake8/__init__.py' in filenames
def test_filenames_from_a_single_file_does_not_exist(): """Verify that a passed filename which does not exist is returned back.""" filenames = set(utils.filenames_from(_normpath('d/n/e.py'))) assert filenames == {_normpath('d/n/e.py')}
def test_filenames_from_a_single_file(): """Verify that we simply yield that filename.""" filenames = set(utils.filenames_from(_normpath('a/b/c.py'))) assert filenames == {_normpath('a/b/c.py')}
def test_filenames_from_a_directory(): """Verify that filenames_from walks a directory.""" filenames = set(utils.filenames_from(_normpath('a/b/'))) # should include all files expected = _normpaths(('a/b/c.py', 'a/b/d.py', 'a/b/e/f.py')) assert filenames == expected
def test_filenames_from_a_single_file(): """Verify that we simply yield that filename.""" filenames = list(utils.filenames_from('flake8/__init__.py')) assert len(filenames) == 1 assert ['flake8/__init__.py'] == filenames