Skip to content

Commit f1b86bb

Browse files
committed
Add --dry-run option to index command
* bdx/cli.py (index): Add --dry-run option and pass it's value to `index_binary_directory'. * bdx/index.py (SymbolIndex.open_shard): Add `readonly' argument that defaults to False. (_WorkerPool.__init__): Add `dry_run' argument. (_WorkerPool._worker): Use it. (index_binary_directory): Add `dry_run' argument.
1 parent df292e5 commit f1b86bb

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

bdx/cli.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ def cli():
327327
" have been modified/removed."
328328
),
329329
)
330+
@click.option(
331+
"-n",
332+
"--dry-run",
333+
is_flag=True,
334+
help="Don't index, instead print out what would be done.",
335+
)
330336
def index(
331337
directory,
332338
index_path,
@@ -335,9 +341,10 @@ def index(
335341
reindex,
336342
exclude,
337343
delete,
344+
dry_run,
338345
):
339346
"""Index the specified directory."""
340-
if delete:
347+
if delete and not dry_run:
341348
delete_index(index_path)
342349

343350
exclusions = [Exclusion(ex) for ex in exclude]
@@ -352,6 +359,7 @@ def index(
352359
exclusions=exclusions,
353360
use_compilation_database=use_compilation_database,
354361
reindex=reindex,
362+
dry_run=dry_run,
355363
)
356364
except BinaryDirectory.CompilationDatabaseNotFoundError as e:
357365
error(str(e))

bdx/index.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,13 @@ def open(directory: Path | str, readonly: bool = False) -> "SymbolIndex":
593593
return index
594594

595595
@staticmethod
596-
def open_shard(directory: Path | str) -> "SymbolIndex":
596+
def open_shard(
597+
directory: Path | str, readonly: bool = False
598+
) -> "SymbolIndex":
597599
"""Open a writable shard for index in given directory."""
598600
for path in SymbolIndex.generate_shard_paths(Path(directory) / "db"):
599601
try:
600-
return SymbolIndex(path, readonly=False, is_shard=True)
602+
return SymbolIndex(path, readonly=readonly, is_shard=True)
601603
except Exception:
602604
pass
603605

@@ -928,11 +930,13 @@ def __init__(
928930
should_quit: Callable[[], bool],
929931
index_path: Path,
930932
use_compilation_database: bool,
933+
dry_run: bool,
931934
):
932935
self.options = options
933936
self.should_quit = should_quit
934937
self.index_path = index_path
935938
self.use_compilation_database = use_compilation_database
939+
self.dry_run = dry_run
936940

937941
self._job_queue: Queue[Path]
938942
self._result_queue: Queue[int]
@@ -1025,9 +1029,16 @@ def _worker(self):
10251029
except QueueEmpty:
10261030
continue
10271031

1028-
result = _index_single_file(
1029-
index, path, self.options, self.use_compilation_database
1030-
)
1032+
if self.dry_run:
1033+
print(f"index {path}")
1034+
result = 1
1035+
else:
1036+
result = _index_single_file(
1037+
index,
1038+
path,
1039+
self.options,
1040+
self.use_compilation_database,
1041+
)
10311042

10321043
self._result_queue.put(result)
10331044

@@ -1039,6 +1050,7 @@ def index_binary_directory(
10391050
use_compilation_database: bool = False,
10401051
reindex: bool = False,
10411052
exclusions: Optional[Collection[Exclusion]] = None,
1053+
dry_run: bool = False,
10421054
) -> IndexingStats:
10431055
"""Index the given directory."""
10441056
stats = IndexingStats()
@@ -1052,8 +1064,15 @@ def index_binary_directory(
10521064

10531065
bindir_path = Path(directory)
10541066

1055-
with SymbolIndex.open(index_path, readonly=False) as index:
1056-
if index.binary_dir() is None:
1067+
with SymbolIndex.open(index_path, readonly=dry_run) as index:
1068+
1069+
def delete_file(path):
1070+
if dry_run:
1071+
print(f"delete {path}")
1072+
else:
1073+
index.delete_file(path)
1074+
1075+
if index.binary_dir() is None and not dry_run:
10571076
index.set_binary_dir(bindir_path)
10581077

10591078
saved_exclusions = list(index.exclusions())
@@ -1089,19 +1108,20 @@ def index_binary_directory(
10891108
desc="Removing outdated files",
10901109
leave=False,
10911110
):
1092-
index.delete_file(file)
1111+
delete_file(file)
10931112
debug("File modified: {}", file)
10941113
for file in make_progress_bar(
10951114
deleted_files,
10961115
desc="Removing deleted files",
10971116
leave=False,
10981117
):
1099-
index.delete_file(file)
1118+
delete_file(file)
11001119
debug("File deleted: {}", file)
11011120

11021121
if options.persist_exclusions:
11031122
saved_exclusions.extend(original_exclusions)
1104-
index.set_exclusions(saved_exclusions)
1123+
if not dry_run:
1124+
index.set_exclusions(saved_exclusions)
11051125

11061126
with (
11071127
sigint_catcher() as interrupted,
@@ -1110,6 +1130,7 @@ def index_binary_directory(
11101130
interrupted,
11111131
index_path,
11121132
use_compilation_database=use_compilation_database,
1133+
dry_run=dry_run,
11131134
) as pool,
11141135
):
11151136
perfile_iterator = pool.index_files(changed_files)

0 commit comments

Comments
 (0)