diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-09-17 14:53:12 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-10-31 20:09:31 -0700 |
commit | b04213f5dc75b507ee5bc7fe16af7f7b8a3b0c3a (patch) | |
tree | 4932ab8555ffea8989af4f1f90d7ef5be7ee8c63 /nova/utils.py | |
parent | 33825bea5bccab26542e0db649932ba8da3de27f (diff) | |
download | nova-b04213f5dc75b507ee5bc7fe16af7f7b8a3b0c3a.tar.gz nova-b04213f5dc75b507ee5bc7fe16af7f7b8a3b0c3a.tar.xz nova-b04213f5dc75b507ee5bc7fe16af7f7b8a3b0c3a.zip |
Move to a more canonicalized output from qemu-img info.
Move to a form that is all lower cased, dashes->underscores, underscores
instead of spaces which allows for better integration with python. Also
make the parser more robust to failures when encountering new fields such
as snapshot lists. Provide a new qemu img info object that can be used
to do the parsing and access the underlying attributes.
Change-Id: Ie098dbd9f06dd4ef966768e2caa128f1d09b019c
Diffstat (limited to 'nova/utils.py')
-rw-r--r-- | nova/utils.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py index 01df19b27..fe81ffd93 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -63,6 +63,16 @@ FLAGS.register_opt( cfg.BoolOpt('disable_process_locking', default=False, help='Whether to disable inter-process locks')) +# Used for looking up extensions of text +# to their 'multiplied' byte amount +BYTE_MULTIPLIERS = { + '': 1, + 't': 1024 ** 4, + 'g': 1024 ** 3, + 'm': 1024 ** 2, + 'k': 1024, +} + def vpn_ping(address, port, timeout=0.05, session_id=None): """Sends a vpn negotiation packet and returns the server session. @@ -574,6 +584,34 @@ def utf8(value): return value +def to_bytes(text, default=0): + """Try to turn a string into a number of bytes. Looks at the last + characters of the text to determine what conversion is needed to + turn the input text into a byte number. + + Supports: B/b, K/k, M/m, G/g, T/t (or the same with b/B on the end) + + """ + # Take off everything not number 'like' (which should leave + # only the byte 'identifier' left) + mult_key_org = text.lstrip('-1234567890') + mult_key = mult_key_org.lower() + mult_key_len = len(mult_key) + if mult_key.endswith("b"): + mult_key = mult_key[0:-1] + try: + multiplier = BYTE_MULTIPLIERS[mult_key] + if mult_key_len: + # Empty cases shouldn't cause text[0:-0] + text = text[0:-mult_key_len] + return int(text) * multiplier + except KeyError: + msg = _('Unknown byte multiplier: %s') % mult_key_org + raise TypeError(msg) + except ValueError: + return default + + def delete_if_exists(pathname): """delete a file, but ignore file not found error""" |