Skip to content

Commit

Permalink
feat: list Python dependency size
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Dec 2, 2023
1 parent 8dce427 commit 10b9e38
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
39 changes: 39 additions & 0 deletions python_boilerplate/demo/python_dependency_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from importlib import metadata as importlib_metadata
from importlib.metadata import Distribution, PackagePath
from pathlib import Path

from loguru import logger


def calculate_distribution(dist: Distribution) -> int:
# noinspection PyUnresolvedReferences,PyProtectedMember
distribution_parent: Path = dist._path.parent # type: ignore
files_in_distribution: list[PackagePath] = (
dist.files if dist.files is not None else []
)
total_size = 0
logger.debug(f"{dist.metadata['name']} has {len(files_in_distribution)} files")
for file in files_in_distribution:
file_path = distribution_parent / str(file)
if file_path.is_file():
total_size += file_path.stat().st_size
return total_size


def list_python_dependencies() -> None:
distributions = list(importlib_metadata.distributions())
logger.info(f"Size of dists: {len(distributions)}")
total_size = 0
for dist in distributions:
size = calculate_distribution(dist)
total_size += size
if size / 1000 > 1.0:
logger.info(f"{dist.metadata['name']}: {size / 1000} KB")
else:
logger.info(f"{dist.metadata['name']}: {size} B")
logger.info("-" * 40)
logger.info(f"Total dependency size: {total_size / 1000} KB")


if __name__ == "__main__":
list_python_dependencies()
8 changes: 8 additions & 0 deletions tests/demo/test_python_dependency_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from python_boilerplate.demo.python_dependency_size import list_python_dependencies


def test_list_python_dependencies() -> None:
try:
list_python_dependencies()
except Exception as e:
assert False, f"Failed to test throttle_function(). {e}"

0 comments on commit 10b9e38

Please sign in to comment.