Skip to content

Commit

Permalink
refactored runner.is_exclude use Lintable
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Sep 1, 2022
1 parent 37e04ea commit a9bf5ca
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
from dataclasses import dataclass
from fnmatch import fnmatch
from pathlib import Path
from typing import TYPE_CHECKING, Any, Generator

import ansiblelint.skip_utils
Expand Down Expand Up @@ -84,25 +83,24 @@ def _update_exclude_paths(self, exclude_paths: list[str]) -> None:
else:
self.exclude_paths = []

def is_excluded(self, file_path: str) -> bool:
def is_excluded(self, lintable: Lintable) -> bool:
"""Verify if a file path should be excluded."""
# Any will short-circuit as soon as something returns True, but will
# be poor performance for the case where the path under question is
# not excluded.

# Exclusions should be evaluated only using absolute paths in order
# to work correctly.
if not file_path:
if not lintable:
return False

abs_path = os.path.abspath(file_path)
_file_path = Path(file_path)
abs_path = str(lintable.abspath)

return any(
abs_path.startswith(path)
or _file_path.match(path)
or lintable.path.match(path)
or fnmatch(str(abs_path), path)
or fnmatch(str(_file_path), path)
or fnmatch(str(lintable), path)
for path in self.exclude_paths
)

Expand All @@ -113,7 +111,7 @@ def run(self) -> list[MatchError]: # noqa: C901

# remove exclusions
for lintable in self.lintables.copy():
if self.is_excluded(str(lintable.path.resolve())):
if self.is_excluded(lintable):
_logger.debug("Excluded %s", lintable)
self.lintables.remove(lintable)
try:
Expand Down Expand Up @@ -180,7 +178,9 @@ def worker(lintable: Lintable) -> list[MatchError]:

# remove any matches made inside excluded files
matches = list(
filter(lambda match: not self.is_excluded(match.filename), matches)
filter(
lambda match: not self.is_excluded(Lintable(match.filename)), matches
)
)

return sorted(set(matches))
Expand All @@ -191,7 +191,7 @@ def _emit_matches(self, files: list[Lintable]) -> Generator[MatchError, None, No
for lintable in self.lintables - visited:
try:
for child in ansiblelint.utils.find_children(lintable):
if self.is_excluded(str(child.path)):
if self.is_excluded(child):
continue
self.lintables.add(child)
files.append(child)
Expand Down

0 comments on commit a9bf5ca

Please sign in to comment.