Skip to content

Commit 58891d8

Browse files
authored
CM-53667: improve ignore logging (#349)
1 parent ee30ee8 commit 58891d8

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

cycode/cli/files_collector/walk_ignore.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
from collections.abc import Generator, Iterable
33

4-
from cycode.cli.logger import logger
4+
from cycode.cli.logger import get_logger
55
from cycode.cli.utils.ignore_utils import IgnoreFilterManager
66

7+
logger = get_logger('Ignores')
8+
79
_SUPPORTED_IGNORE_PATTERN_FILES = {
810
'.gitignore',
911
'.cycodeignore',
@@ -30,7 +32,7 @@ def _collect_top_level_ignore_files(path: str) -> list[str]:
3032
for ignore_file in _SUPPORTED_IGNORE_PATTERN_FILES:
3133
ignore_file_path = os.path.join(dir_path, ignore_file)
3234
if os.path.exists(ignore_file_path):
33-
logger.debug('Apply top level ignore file: %s', ignore_file_path)
35+
logger.debug('Reading top level ignore file: %s', ignore_file_path)
3436
ignore_files.append(ignore_file_path)
3537
return ignore_files
3638

@@ -41,4 +43,17 @@ def walk_ignore(path: str) -> Generator[tuple[str, list[str], list[str]], None,
4143
global_ignore_file_paths=_collect_top_level_ignore_files(path),
4244
global_patterns=_DEFAULT_GLOBAL_IGNORE_PATTERNS,
4345
)
44-
yield from ignore_filter_manager.walk()
46+
for dirpath, dirnames, filenames, ignored_dirnames, ignored_filenames in ignore_filter_manager.walk_with_ignored():
47+
rel_dirpath = '' if dirpath == path else os.path.relpath(dirpath, path)
48+
display_dir = rel_dirpath or '.'
49+
for is_dir, names in (
50+
(True, ignored_dirnames),
51+
(False, ignored_filenames),
52+
):
53+
for name in names:
54+
full_path = os.path.join(path, display_dir, name)
55+
if is_dir:
56+
full_path = os.path.join(full_path, '*')
57+
logger.debug('Ignoring match %s', full_path)
58+
59+
yield dirpath, dirnames, filenames

cycode/cli/utils/ignore_utils.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,19 +388,38 @@ def is_ignored(self, path: str) -> Optional[bool]:
388388
return matches[-1].is_exclude
389389
return None
390390

391-
def walk(self, **kwargs) -> Generator[tuple[str, list[str], list[str]], None, None]:
392-
"""Wrap os.walk() without ignored files and subdirectories and kwargs are passed to walk."""
391+
def walk_with_ignored(
392+
self, **kwargs
393+
) -> Generator[tuple[str, list[str], list[str], list[str], list[str]], None, None]:
394+
"""Wrap os.walk() and also return lists of ignored directories and files.
395+
396+
Yields tuples: (dirpath, included_dirnames, included_filenames, ignored_dirnames, ignored_filenames)
397+
"""
393398
for dirpath, dirnames, filenames in os.walk(self.path, topdown=True, **kwargs):
394399
rel_dirpath = '' if dirpath == self.path else os.path.relpath(dirpath, self.path)
395400

401+
original_dirnames = list(dirnames)
402+
included_dirnames = []
403+
ignored_dirnames = []
404+
for d in original_dirnames:
405+
if self.is_ignored(os.path.join(rel_dirpath, d)):
406+
ignored_dirnames.append(d)
407+
else:
408+
included_dirnames.append(d)
409+
396410
# decrease recursion depth of os.walk() by ignoring subdirectories because of topdown=True
397411
# slicing ([:]) is mandatory to change dict in-place!
398-
dirnames[:] = [d for d in dirnames if not self.is_ignored(os.path.join(rel_dirpath, d))]
412+
dirnames[:] = included_dirnames
399413

400-
# remove ignored files
401-
filenames = [f for f in filenames if not self.is_ignored(os.path.join(rel_dirpath, f))]
414+
included_filenames = []
415+
ignored_filenames = []
416+
for f in filenames:
417+
if self.is_ignored(os.path.join(rel_dirpath, f)):
418+
ignored_filenames.append(f)
419+
else:
420+
included_filenames.append(f)
402421

403-
yield dirpath, dirnames, filenames
422+
yield dirpath, dirnames, included_filenames, ignored_dirnames, ignored_filenames
404423

405424
@classmethod
406425
def build(

0 commit comments

Comments
 (0)