From 11f1ee5dda9b994e638f7af13259e71eb42fbaa5 Mon Sep 17 00:00:00 2001 From: gryf Date: Mon, 12 Apr 2021 17:34:02 +0200 Subject: [PATCH] Added ability to specify sizes in GB. --- box.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/box.py b/box.py index 212dda7..b51bae0 100755 --- a/box.py +++ b/box.py @@ -9,7 +9,7 @@ import sys import tempfile import time import uuid -import xml +import xml.dom.minidom CACHE_DIR = os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache')) @@ -37,6 +37,29 @@ power_state: ''') +def convert_to_mega(size): + """ + Vritualbox uses MB as a common denominator for amount of memory or disk + size. This function will return string of MB from string which have human + readable suffix, like M or G. Case insensitive. + """ + + if size.isnumeric(): + return str(size) + + if size.lower().endswith('m') and size[:-1].isnumeric(): + return str(size[:-1]) + + if size.lower().endswith('g') and size[:-1].isnumeric(): + return str(int(size[:-1]) * 1024) + + if size.lower().endswith('mb') and size[:-2].isnumeric(): + return str(size[:-2]) + + if size.lower().endswith('gb') and size[:-2].isnumeric(): + return str(int(size[:-2]) * 1024) + + class BoxError(Exception): pass @@ -167,6 +190,7 @@ class VBoxManage: def create(self, cpus, memory): self.uuid = None + memory = convert_to_mega(memory) try: out = subprocess.check_output(['vboxmanage', 'createvm', '--name', @@ -213,6 +237,7 @@ class VBoxManage: def move_and_resize_image(self, src, dst, size): fullpath = os.path.join(self.get_vm_base_path(), dst) + size = convert_to_mega(size) if subprocess.call(['vboxmanage', 'modifymedium', 'disk', src, '--resize', str(size), '--move', fullpath]) != 0: