|
| 1 | +import ast |
| 2 | +import importlib.metadata |
| 3 | +import os |
| 4 | + |
| 5 | +# Set PROJECT_DIR to the script's directory |
| 6 | +PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 7 | +VENV_DIR = os.path.join(PROJECT_DIR, ".venv") |
| 8 | + |
| 9 | + |
| 10 | +def get_installed_packages() -> set: |
| 11 | + """Get installed packages inside .venv using importlib.metadata.""" |
| 12 | + installed = {pkg.metadata["Name"].lower() for pkg in importlib.metadata.distributions()} |
| 13 | + return installed |
| 14 | + |
| 15 | + |
| 16 | +def get_imported_modules(project_path) -> set: |
| 17 | + """Scan all Python files in the project for imported modules, ignoring .venv.""" |
| 18 | + imported_modules = set() |
| 19 | + |
| 20 | + for root, _, files in os.walk(project_path): |
| 21 | + # Skip the virtual environment directory |
| 22 | + if ".venv" in root: |
| 23 | + continue |
| 24 | + |
| 25 | + for file in files: |
| 26 | + if file.endswith(".py"): |
| 27 | + file_path = os.path.join(root, file) |
| 28 | + print(f"Scanning {file_path}...") |
| 29 | + try: |
| 30 | + with open(file_path, "r", encoding="utf-8", errors="ignore") as f: |
| 31 | + tree = ast.parse(f.read(), filename=file_path) |
| 32 | + for node in ast.walk(tree): |
| 33 | + if isinstance(node, ast.Import): |
| 34 | + for alias in node.names: |
| 35 | + imported_modules.add(alias.name.split(".")[0]) |
| 36 | + elif isinstance(node, ast.ImportFrom): |
| 37 | + if node.module: |
| 38 | + imported_modules.add(node.module.split(".")[0]) |
| 39 | + except SyntaxError: |
| 40 | + print(f"Skipping {file_path} due to syntax error.") |
| 41 | + |
| 42 | + return imported_modules |
| 43 | + |
| 44 | + |
| 45 | +def compare_dependencies() -> None: |
| 46 | + """Compare installed vs imported dependencies.""" |
| 47 | + installed_packages = get_installed_packages() |
| 48 | + imported_modules = get_imported_modules(PROJECT_DIR) |
| 49 | + |
| 50 | + used_dependencies = installed_packages.intersection(imported_modules) |
| 51 | + unused_dependencies = installed_packages - imported_modules |
| 52 | + missing_dependencies = imported_modules - installed_packages |
| 53 | + |
| 54 | + print("\n📌 Dependency Comparison:\n") |
| 55 | + print("✅ Used Dependencies (Installed & Imported):") |
| 56 | + print(sorted(used_dependencies)) |
| 57 | + |
| 58 | + print("\n❌ Unused Dependencies (Installed but NOT used):") |
| 59 | + print(sorted(unused_dependencies)) |
| 60 | + |
| 61 | + print("\n⚠️ Missing Dependencies (Used but NOT installed):") |
| 62 | + print(sorted(missing_dependencies)) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + compare_dependencies() |
0 commit comments