-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_package_sizes.py
31 lines (27 loc) · 1.07 KB
/
get_package_sizes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import sys
from pip._internal.operations.freeze import freeze
## This code is used to get the size of the packages that are installed in the backend/venv as way to estimate the docker file size during deployment
def get_package_size(package_name):
try:
package_location = next(
p.split("==")[0] for p in freeze() if p.startswith(package_name)
)
package_location = os.path.join(sys.prefix, "Lib", "site-packages", package_name)
if os.path.isdir(package_location):
total_size = 0
for dirpath, dirnames, filenames in os.walk(package_location):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size
return 0
except StopIteration:
return 0
packages = sorted(
[(p.split("==")[0], get_package_size(p.split("==")[0])) for p in freeze()],
key=lambda x: x[1],
reverse=True,
)
for package_name, size in packages:
print(f"{package_name} {size / (1024 * 1024):.2f} MB")