1
0
mirror of https://github.com/gryf/boxpy.git synced 2025-12-19 21:47:59 +01:00

Added ability to specify sizes in GB.

This commit is contained in:
2021-04-12 17:34:02 +02:00
parent ca713375ea
commit 11f1ee5dda

27
box.py
View File

@@ -9,7 +9,7 @@ import sys
import tempfile import tempfile
import time import time
import uuid import uuid
import xml import xml.dom.minidom
CACHE_DIR = os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache')) 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): class BoxError(Exception):
pass pass
@@ -167,6 +190,7 @@ class VBoxManage:
def create(self, cpus, memory): def create(self, cpus, memory):
self.uuid = None self.uuid = None
memory = convert_to_mega(memory)
try: try:
out = subprocess.check_output(['vboxmanage', 'createvm', '--name', out = subprocess.check_output(['vboxmanage', 'createvm', '--name',
@@ -213,6 +237,7 @@ class VBoxManage:
def move_and_resize_image(self, src, dst, size): def move_and_resize_image(self, src, dst, size):
fullpath = os.path.join(self.get_vm_base_path(), dst) fullpath = os.path.join(self.get_vm_base_path(), dst)
size = convert_to_mega(size)
if subprocess.call(['vboxmanage', 'modifymedium', 'disk', src, if subprocess.call(['vboxmanage', 'modifymedium', 'disk', src,
'--resize', str(size), '--move', fullpath]) != 0: '--resize', str(size), '--move', fullpath]) != 0: