2022-08-25 17:03:56 +00:00
|
|
|
"""Generic size counter using pathlib"""
|
2024-07-26 19:59:44 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
import pathlib
|
2024-10-23 11:38:01 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_storage_usage(path: str) -> int:
|
|
|
|
"""
|
|
|
|
Calculate the real storage usage of path and all subdirectories.
|
|
|
|
Calculate using pathlib.
|
|
|
|
Do not follow symlinks.
|
|
|
|
"""
|
|
|
|
storage_usage = 0
|
|
|
|
for iter_path in pathlib.Path(path).rglob("**/*"):
|
|
|
|
if iter_path.is_dir():
|
|
|
|
continue
|
2022-09-22 15:34:33 +00:00
|
|
|
try:
|
|
|
|
storage_usage += iter_path.stat().st_size
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
except Exception as error:
|
2024-10-23 11:38:01 +00:00
|
|
|
logging.error(error)
|
2022-08-25 17:03:56 +00:00
|
|
|
return storage_usage
|