From e0ce8a6bfecd2878f39d0ec30f9b54167f66c894 Mon Sep 17 00:00:00 2001 From: Ana Maria Martinez Gomez Date: Mon, 27 Jan 2025 17:34:58 +0100 Subject: [PATCH] [vbox] Small improvements in vboxcommon Make `run_vboxmanage` consistent with other functions in boxcommon and make black happy with: ``` black --line-length=120 virtualbox/vboxcommon.py ``` --- virtualbox/vboxcommon.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/virtualbox/vboxcommon.py b/virtualbox/vboxcommon.py index 2ad8b0b..7491758 100644 --- a/virtualbox/vboxcommon.py +++ b/virtualbox/vboxcommon.py @@ -23,12 +23,14 @@ def format_arg(arg): return f"'{arg}'" return arg + def cmd_to_str(cmd): """Convert a list of string arguments to a string.""" return " ".join(format_arg(arg) for arg in cmd) + def run_vboxmanage(cmd): - """Runs a VBoxManage command and returns the output. + """Run a VBoxManage command and return the output. Args: cmd: list of string arguments to pass to VBoxManage @@ -40,9 +42,9 @@ def run_vboxmanage(cmd): # Use only the first "VBoxManage: error:" line to prevent using the long # VBoxManage help message or noisy information like the details and context. error = f"Command '{cmd_to_str(cmd)}' failed" - stderr_info = re.search("^VBoxManage: error: (.*)", result.stderr, flags=re.M) - if stderr_info: - error += f": {stderr_info.group(1)}" + match = re.search("^VBoxManage: error: (?P.*)", result.stderr, flags=re.M) + if match: + error += f": {match['stderr_info']}" raise RuntimeError(error) return result.stdout @@ -58,6 +60,7 @@ def get_hostonlyif_name(): if match: return match["hostonlyif_name"] + def ensure_hostonlyif_exists(): """Get the name of the host-only interface. Create the interface if it doesn't exist.""" hostonlyif_name = get_hostonlyif_name() @@ -134,4 +137,3 @@ def ensure_vm_shutdown(vm_uuid): if not wait_until_vm_state(vm_uuid, "poweroff"): raise RuntimeError(f"Unable to shutdown VM {vm_uuid}.") -