Skip to content

Commit 809b7f1

Browse files
authored
fix: show the name of the file that's being analyzed (#333)
1 parent 35133fd commit 809b7f1

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/twyn/main.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ def _analyze_packages_from_source(
174174
threshold_class=SimilarityThreshold,
175175
)
176176
results: list[TyposquatCheckResultFromSource] = []
177+
177178
for parser in parsers:
178179
analyzed_dependencies = _analyze_dependencies(
179-
top_package_reference, trusted_packages, parser.parse(), allowlist, show_progress_bar
180+
top_package_reference, trusted_packages, parser.parse(), allowlist, show_progress_bar, parser.file_path
180181
)
181182

182183
if analyzed_dependencies:
@@ -194,6 +195,7 @@ def _analyze_dependencies(
194195
packages: set[str],
195196
allowlist: set[str],
196197
show_progress_bar: bool,
198+
dependency_file: Optional[str] = None,
197199
) -> list[TyposquatCheckResultEntry]:
198200
"""Analyze the set of given dependencies against the trusted packages' golden set.
199201
@@ -203,8 +205,7 @@ def _analyze_dependencies(
203205
normalized_dependencies = top_package_reference.normalize_packages(packages)
204206

205207
errors = []
206-
207-
for dependency in _get_dependencies_list(normalized_dependencies, show_progress_bar):
208+
for dependency in _get_dependencies_list(normalized_dependencies, show_progress_bar, dependency_file):
208209
if dependency in normalized_allowlist_packages:
209210
logger.info("Dependency %s is in the allowlist", dependency)
210211
continue
@@ -216,20 +217,26 @@ def _analyze_dependencies(
216217
return errors
217218

218219

219-
def _get_dependencies_list(normalized_dependencies: set[str], show_progress_bar: bool) -> Iterable[str]:
220+
def _get_dependencies_list(
221+
normalized_dependencies: set[str], show_progress_bar: bool, dependency_file: Optional[str] = None
222+
) -> Iterable[str]:
220223
"""Determine if the progress bar will be showed or not. It returns an iterable of all the dependencies to analyze."""
221224
try:
222225
from rich.progress import track # noqa: PLC0415
223226

227+
if dependency_file:
228+
from click import echo, style # noqa: PLC0415
229+
230+
echo(style(f"Reading file {dependency_file}", fg="green"), color=True)
224231
return (
225232
track(normalized_dependencies, description="Processing...")
226233
if show_progress_bar
227234
else normalized_dependencies
228235
)
229-
except ImportError as e:
236+
except ModuleNotFoundError as e:
230237
if show_progress_bar:
231238
raise InvalidArgumentsError(
232-
"Cannot show progress bar because `rich` dependency is not installed. "
239+
"Cannot show progress bar because `rich` and `click` dependencies are not installed. "
233240
"It is only meant to be shown when running `twyn` as a cli tool. "
234241
"If this is you case, install all the dependencies with `pip install twyn[cli]`. "
235242
) from e

0 commit comments

Comments
 (0)