Skip to content

Commit

Permalink
add pure Python fallback for getDirSizeRecursively() (#4753)
Browse files Browse the repository at this point in the history
* add pure Python fallback for getDirSizeRecursively()

* Fix spelling

---------

Co-authored-by: Adam Novak <[email protected]>
  • Loading branch information
mr-c and adamnovak authored Jan 16, 2024
1 parent c0feabd commit 3aecdb3
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/toil/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,10 +1626,7 @@ def getDirSizeRecursively(dirPath: str) -> int:
internally, and a (possibly 0) lower bound on the size of the directory
will be returned.
The environment variable 'BLOCKSIZE'='512' is set instead of the much cleaner
--block-size=1 because Apple can't handle it.
:param str dirPath: A valid path to a directory or file.
:param dirPath: A valid path to a directory or file.
:return: Total size, in bytes, of the file or directory at dirPath.
"""

Expand All @@ -1639,12 +1636,22 @@ def getDirSizeRecursively(dirPath: str) -> int:
# allocated with the environment variable: BLOCKSIZE='512' set, and we
# multiply this by 512 to return the filesize in bytes.

dirPath = os.path.abspath(dirPath)
try:
return int(subprocess.check_output(['du', '-s', dirPath],
env=dict(os.environ, BLOCKSIZE='512')).decode('utf-8').split()[0]) * 512
except subprocess.CalledProcessError:
# Something was inaccessible or went away
return 0
# The environment variable 'BLOCKSIZE'='512' is set instead of the much cleaner
# --block-size=1 because Apple can't handle it.
except (OSError, subprocess.CalledProcessError):
# Fallback to pure Python implementation, useful for when kernel limits
# to argument list size are hit, etc..
total_size: int = 0
if os.path.isfile(dirPath):
return os.lstat(dirPath).st_blocks * 512
for dir_path, dir_names, filenames in os.walk(dirPath):
for name in filenames:
total_size += os.lstat(os.path.join(dir_path, name)).st_blocks * 512
return total_size


def getFileSystemSize(dirPath: str) -> Tuple[int, int]:
Expand Down

0 comments on commit 3aecdb3

Please sign in to comment.