diff options
author | Scott Moser <smoser@ubuntu.com> | 2014-09-30 16:24:54 -0400 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2014-09-30 16:24:54 -0400 |
commit | 9736b260434af860c6ec81776f4278640f1fa9be (patch) | |
tree | 8c88e66b7d84a2bdd2fcda97ef5e19fde5aed431 /cloudinit/util.py | |
parent | 9e645c06677ef146ab5dd36ce6e4be2329a1202b (diff) | |
download | vyos-cloud-init-9736b260434af860c6ec81776f4278640f1fa9be.tar.gz vyos-cloud-init-9736b260434af860c6ec81776f4278640f1fa9be.zip |
support human2bytes, separate handling out to method
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r-- | cloudinit/util.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py index 349d0155..f236d0bf 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1978,3 +1978,32 @@ def read_meminfo(meminfo="/proc/meminfo", raw=False): ret[kmap[key]] = int(value) * mpliers[unit] return ret + + +def human2bytes(size): + """Convert human string or integer to size in bytes + 10M => 10485760 + .5G => 536870912 + """ + size_in = size + if size.endswith("B"): + size = size[:-1] + + mpliers = {'B': 1, 'K': 2 ** 10, 'M': 2 ** 20, 'G': 2 ** 30, 'T': 2 ** 40} + + num = size + mplier = 'B' + for m in mpliers: + if size.endswith(m): + mplier = m + num = size[0:-len(m)] + + try: + num = float(num) + except ValueError: + raise ValueError("'%s' is not valid input." % size_in) + + if num < 0: + raise ValueError("'%s': cannot be negative" % size_in) + + return int(num * mpliers[mplier]) |