Skip to content

Commit d68ef8f

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

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python Debugger: Current File with Arguments",
9+
"type": "debugpy",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal",
13+
"args": "${command:pickArgs}"
14+
}
15+
]
16+
}

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: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
BinaryIO,
4646
Optional,
4747
Union,
48+
List,
49+
Dict,
4850
)
4951

5052

@@ -388,19 +390,30 @@ def is_ignored(self, path: str) -> Optional[bool]:
388390
return matches[-1].is_exclude
389391
return None
390392

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."""
393+
def walk_with_ignored(
394+
self, **kwargs
395+
) -> Generator[tuple[str, list[str], list[str], list[str], list[str]], None, None]:
396+
"""Wrap os.walk() and also return lists of ignored directories and files.
397+
398+
Yields tuples: (dirpath, included_dirnames, included_filenames, ignored_dirnames, ignored_filenames)
399+
"""
393400
for dirpath, dirnames, filenames in os.walk(self.path, topdown=True, **kwargs):
394401
rel_dirpath = '' if dirpath == self.path else os.path.relpath(dirpath, self.path)
395402

403+
original_dirnames = list(dirnames)
404+
included_dirnames = [d for d in original_dirnames if not self.is_ignored(os.path.join(rel_dirpath, d))]
405+
396406
# decrease recursion depth of os.walk() by ignoring subdirectories because of topdown=True
397407
# 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))]
408+
dirnames[:] = included_dirnames
409+
410+
ignored_dirnames = [d for d in original_dirnames if d not in included_dirnames]
399411

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

403-
yield dirpath, dirnames, filenames
416+
yield dirpath, dirnames, included_filenames, ignored_dirnames, ignored_filenames
404417

405418
@classmethod
406419
def build(

0 commit comments

Comments
 (0)