Skip to content

Commit

Permalink
[vbox] Small improvements in vboxcommon
Browse files Browse the repository at this point in the history
Make `run_vboxmanage` consistent with other functions in boxcommon and
make black happy with:
```
black --line-length=120 virtualbox/vboxcommon.py
```
  • Loading branch information
Ana06 committed Jan 29, 2025
1 parent 1e82ac5 commit e0ce8a6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions virtualbox/vboxcommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<stderr_info>.*)", result.stderr, flags=re.M)
if match:
error += f": {match['stderr_info']}"
raise RuntimeError(error)

return result.stdout
Expand All @@ -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()
Expand Down Expand Up @@ -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}.")

0 comments on commit e0ce8a6

Please sign in to comment.