Skip to content

Commit e3c46c7

Browse files
committed
CM-53667: improve ignore logging
1 parent ee30ee8 commit e3c46c7

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

cycode/cli/files_collector/walk_ignore.py

Lines changed: 16 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('File Ignore')
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,15 @@ 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+
47+
for dirpath, dirnames, filenames, ignored_dirnames, ignored_filenames in ignore_filter_manager.walk_with_ignored():
48+
rel_dirpath = '' if dirpath == path else os.path.relpath(dirpath, path)
49+
display_dir = rel_dirpath or '.'
50+
for kind, names in (
51+
('directory', ignored_dirnames),
52+
('file', ignored_filenames),
53+
):
54+
for name in names:
55+
logger.debug('Skipping matched %s %s/%s', kind, display_dir, name)
56+
57+
yield dirpath, dirnames, filenames

cycode/cli/utils/ignore_utils.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,19 +388,30 @@ 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 = [d for d in original_dirnames if not self.is_ignored(os.path.join(rel_dirpath, d))]
403+
396404
# decrease recursion depth of os.walk() by ignoring subdirectories because of topdown=True
397405
# 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))]
406+
dirnames[:] = included_dirnames
407+
408+
ignored_dirnames = [d for d in original_dirnames if d not in included_dirnames]
399409

400-
# remove ignored files
401-
filenames = [f for f in filenames if not self.is_ignored(os.path.join(rel_dirpath, f))]
410+
original_filenames = list(filenames)
411+
included_filenames = [f for f in original_filenames if not self.is_ignored(os.path.join(rel_dirpath, f))]
412+
ignored_filenames = [f for f in original_filenames if f not in included_filenames]
402413

403-
yield dirpath, dirnames, filenames
414+
yield dirpath, dirnames, included_filenames, ignored_dirnames, ignored_filenames
404415

405416
@classmethod
406417
def build(

0 commit comments

Comments
 (0)