code: def attach_virtualbox(
      vm_uuid, vid_hex=None, pid_hex=None, serial=None
  ):
      usb_devices = parse_virtualbox_devices()
      for dev in usb_devices:
          m = VIRTUALBOX_VID_PID_RE.match(dev["VendorId"])
          if not m:
              _LOG.warning("Malformed VendorId: %s", dev["VendorId"])
              continue
  
          dev_vid_hex = m.group(1).lower()
  
          m = VIRTUALBOX_VID_PID_RE.match(dev["ProductId"])
          if not m:
              _LOG.warning("Malformed ProductId: %s", dev["ProductId"])
              continue
  
          dev_pid_hex = m.group(1).lower()
  
          if (
              vid_hex == dev_vid_hex
              and pid_hex == dev_pid_hex
              and (serial is None or serial == dev["SerialNumber"])
          ):
              attached_devices = parse_virtualbox_attached_usb_devices(
                  vm_uuid
              )
              for vid, pid in parse_virtualbox_attached_usb_devices(
                  vm_uuid
              ):
                  if vid_hex == vid and pid_hex == pid:
                      print(
                          f"USB dev {vid_hex}:{pid_hex} already attached. Skipping attach."
                      )
                      return
  
              rule_args = [
                  "VBoxManage",
                  "usbfilter",
                  "add",
                  "0",
                  "--action",
                  "hold",
                  "--name",
                  "test device",
                  "--target",
                  vm_uuid,
                  "--vendorid",
                  vid_hex,
                  "--productid",
                  pid_hex,
              ]
              if serial is not None:
                  rule_args.extend(["--serialnumber", serial])
              subprocess.check_call(rule_args)
              subprocess.check_call(
                  [
                      "VBoxManage",
                      "controlvm",
                      vm_uuid,
                      "usbattach",
                      dev["UUID"],
                  ]
              )
              return
  
      raise Exception(
          f"Device with vid={vid_hex}, pid={pid_hex}, serial={serial!r} not found:\n{usb_devices!r}"
      )
  
imports: import argparse
  import copy
  import json
  import logging
  import pathlib
  import os
  import re
  import shlex
  import shutil
  import subprocess
  import sys
  import pathlib
label_desc: Use image node_id 3 for calling a global function with example usage: attach_virtualbox(vm_uuid, vid_hex, pid_hex, serial) without return types