summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2019-01-20 12:38:22 -0500
committerTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2019-01-24 13:02:08 -0500
commit24ca2f469230a2b9a1ce6a487f1341b4428d4477 (patch)
treee85b36b628f52252c31bb8176993f465c6bce365
parent41f03296791db6fc3cded6310dbaebe855765084 (diff)
downloadbuildstream-24ca2f469230a2b9a1ce6a487f1341b4428d4477.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.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py
index e3ff88034..c8d79c95a 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -633,6 +633,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
@@ -667,8 +688,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)