Skip to content

Commit

Permalink
feat: support some allowed extensions to scan & print out the module+…
Browse files Browse the repository at this point in the history
…package names
  • Loading branch information
agusmakmun committed Apr 4, 2024
1 parent 6de9589 commit ab0f1bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# VSCode
.vscode/
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ So, this tool comes in handy for easily identifying which exact packages are act

[i] Please wait! It may take few minutes to complete...
[i] Scanning unused packages:
1. rcssmin
2. argon2-cffi
3. flower
4. django-model-utils
5. pinax-theme-bootstrap
6. phonenumbers
1. Module: rcssmin ---> Package: rcssmin
2. Module: model_utils ---> Package: django-model-utils
3. Module: pinax_theme_bootstrap ---> Package: pinax-theme-bootstrap
4. Module: phonenumbers ---> Package: phonenumbers
```

Cool right? 😎
Expand Down
18 changes: 15 additions & 3 deletions scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import multiprocessing
import os
import re
from typing import List, Optional
from typing import List, Optional, Tuple

import importlib_metadata

Expand All @@ -17,6 +17,15 @@
- If not, then print the "packages name".
"""

# allowed extensions to scan
ALLOWED_EXTENSIONS: Tuple[str] = (
".py",
".conf",
".cfg",
".yml",
".yaml",
)


def get_main_packages() -> dict:
"""
Expand All @@ -37,6 +46,7 @@ def get_main_packages() -> dict:
for module_name, package_names in packages.items():
if (
not any(exclude in module_name for exclude in excluded_packages)
and not module_name.startswith("_")
and package_names
and isinstance(package_names, list)
):
Expand Down Expand Up @@ -82,7 +92,7 @@ def search_string_in_python_files(directory: str, search_string: str) -> List[st
pool = multiprocessing.Pool()
for root, _, files in os.walk(directory):
for file_name in files:
if file_name.endswith(".py"):
if file_name.endswith(ALLOWED_EXTENSIONS):
file_path = os.path.join(root, file_name)
found_file = pool.apply_async(
search_string_in_file, (file_path, search_string)
Expand Down Expand Up @@ -134,7 +144,9 @@ def main(project_path: str, requirement_file: str):
results: list = search_string_in_python_files(project_path, module_name)
if not results and (module_name not in unused_packages):
unused_packages.append(package_name)
print(f" {number}. {package_name}")
print(
f" {number}. Module: {module_name} ---> Package: {package_name}"
)
number += 1

if len(unused_packages) < 1:
Expand Down

0 comments on commit ab0f1bf

Please sign in to comment.