summaryrefslogtreecommitdiff
path: root/buildstream/utils.py
diff options
context:
space:
mode:
authorJosh Smith <joshsmith@codethink.co.uk>2018-07-23 17:47:30 +0100
committerQinusty <jrsmith9822@gmail.com>2018-07-27 14:10:45 +0000
commitf5c8ff61a68b90d29c48b76d3e7fbab755f2ac2b (patch)
treeccaf02fc1370154c3dec52895349bfd6f36e8c74 /buildstream/utils.py
parent8a96679a7fae6ce7d844a596131a61c8a5ad780c (diff)
downloadbuildstream-f5c8ff61a68b90d29c48b76d3e7fbab755f2ac2b.tar.gz
_context.py: Cache size is now restricted to available disk spaceQinusty/491
This address issue #491. When attempting to run buildstream with a configuration specifying a cache quota larger than your available disk space, buildstream will alert the user and exit. Note: This takes into consideration your current cache usage and therefore restricts the overall size of your artifact cache folder.
Diffstat (limited to 'buildstream/utils.py')
-rw-r--r--buildstream/utils.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py
index bfb58c9ef..68f99b9a3 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -612,6 +612,27 @@ def _parse_size(size, volume):
return int(num) * 1024**units.index(unit)
+# _pretty_size()
+#
+# Converts a number of bytes into a string representation in KB, MB, GB, TB
+# represented as K, M, G, T etc.
+#
+# Args:
+# size (int): The size to convert in bytes.
+# dec_places (int): The number of decimal places to output to.
+#
+# Returns:
+# (str): The string representation of the number of bytes in the largest
+def _pretty_size(size, dec_places=0):
+ psize = size
+ unit = 'B'
+ for unit in ('B', 'K', 'M', 'G', 'T'):
+ if psize < 1024:
+ break
+ else:
+ psize /= 1024
+ return "{size:g}{unit}".format(size=round(psize, dec_places), unit=unit)
+
# A sentinel to be used as a default argument for functions that need
# to distinguish between a kwarg set to None and an unset kwarg.
_sentinel = object()