diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2019-01-20 12:38:22 -0500 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2019-04-14 16:25:19 +0900 |
commit | 6f9c882d56d2b0b062829c4b0af05549bc669f22 (patch) | |
tree | f71ec94f9dc44ec63e7ccca6b2e818c87d2a75c5 | |
parent | 3cbc04979227314d838684967c4110d9dddef266 (diff) | |
download | buildstream-6f9c882d56d2b0b062829c4b0af05549bc669f22.tar.gz |
utils.py: Add _get_volume_size()
We can streamline this call to os.statvfs() in a few places.
-rw-r--r-- | buildstream/utils.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py index e3e9dc8c0..d02777897 100644 --- a/buildstream/utils.py +++ b/buildstream/utils.py @@ -583,6 +583,27 @@ def _get_dir_size(path): return get_size(path) +# _get_volume_size(): +# +# Gets the overall usage and total size of a mounted filesystem in bytes. +# +# Args: +# path (str): The path to check +# +# Returns: +# (int): The total number of bytes on the volume +# (int): The number of available bytes on the volume +# +def _get_volume_size(path): + try: + stat_ = os.statvfs(path) + except OSError as e: + raise UtilError("Failed to retrieve stats on volume for path '{}': {}" + .format(path, e)) from e + + return stat_.f_bsize * stat_.f_blocks, stat_.f_bsize * stat_.f_bavail + + # _parse_size(): # # Convert a string representing data size to a number of @@ -617,8 +638,7 @@ def _parse_size(size, volume): if num > 100: raise UtilError("{}% is not a valid percentage value.".format(num)) - stat_ = os.statvfs(volume) - disk_size = stat_.f_blocks * stat_.f_bsize + disk_size, _ = _get_volume_size(volume) return disk_size * (num / 100) |