Skip to content

Commit 17ff725

Browse files
committed
Gather stats for exclusion pattern match counts
* bdx/binary.py (BinaryDirectory): Add `_exclusion_stats' attribute. (BinaryDirectory.__post_init__): Prepopulate it. (BinaryDirectory.exclusion_stats): New property. (BinaryDirectory._find_files): Update it. * bdx/cli.py (index): Print exclusion pattern stats. * bdx/index.py (IndexingStats): Add `exclusion_stats' attribute. (index_binary_directory): Update it.
1 parent a7ef925 commit 17ff725

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

bdx/binary.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,17 @@ class BinaryDirectory:
506506
use_compilation_database: bool = False
507507

508508
_file_list: list[Path] = field(repr=False, default_factory=list)
509+
_exclusion_stats: dict[Exclusion, int] = field(
510+
repr=False, default_factory=dict
511+
)
509512

510513
class CompilationDatabaseNotFoundError(FileNotFoundError):
511514
"""Could not find the compilation database."""
512515

513516
def __post_init__(self):
517+
for ex in self.exclusions:
518+
self._exclusion_stats[ex] = 0
519+
514520
self._file_list.extend(self._find_files())
515521

516522
@cached_property
@@ -521,6 +527,11 @@ def compilation_database(self):
521527
info("Found compilation database: {}", compdb)
522528
return compdb
523529

530+
@property
531+
def exclusion_stats(self) -> dict[Exclusion, int]:
532+
"""Mapping of ``Exclusion`` to match count."""
533+
return dict(self._exclusion_stats)
534+
524535
def changed_files(self) -> Iterator[Path]:
525536
"""Yield files that were changed/created since last run."""
526537
files = self._file_list
@@ -575,6 +586,7 @@ def _find_files(self) -> Iterator[Path]:
575586
file,
576587
exclusion.pattern,
577588
)
589+
self._exclusion_stats[exclusion] += 1
578590
continue
579591

580592
if is_readable_elf_file(file):

bdx/cli.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,17 @@ def index(
383383
exit(1)
384384

385385
log(
386-
f"Files indexed: {stats.num_files_indexed} "
387-
f"(out of {stats.num_files_changed} changed files)"
386+
"Files indexed: {} (out of {} changed files)",
387+
stats.num_files_indexed,
388+
stats.num_files_changed,
388389
)
389-
log(f"Files removed from index: {stats.num_files_deleted}")
390-
log(f"Symbols indexed: {stats.num_symbols_indexed}")
390+
log("Files removed from index: {}", stats.num_files_deleted)
391+
log("Symbols indexed: {}", stats.num_symbols_indexed)
392+
393+
if stats.exclusion_stats:
394+
log("Exclusion pattern stats:")
395+
for ex, count in stats.exclusion_stats.items():
396+
log(" {!r}: {}", ex.pattern, count)
391397

392398

393399
class SearchOutputFormatParamType(click.Choice):

bdx/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,10 @@ class IndexingStats:
885885
num_files_deleted: int = 0
886886
num_symbols_indexed: int = 0
887887

888+
exclusion_stats: dict[Exclusion, int] = field(
889+
repr=False, default_factory=dict
890+
)
891+
888892

889893
@contextmanager
890894
def sigint_catcher() -> Iterator[Callable[[], bool]]:
@@ -1190,6 +1194,7 @@ def index_binary_directory(
11901194

11911195
stats.num_files_changed = len(changed_files)
11921196
stats.num_files_deleted = len(deleted_files)
1197+
stats.exclusion_stats.update(bdir.exclusion_stats)
11931198

11941199
def log_unindex_file(path, is_deleted):
11951200
if dry_run and path in existing_files:

0 commit comments

Comments
 (0)