-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dce427
commit 10b9e38
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |