vbox.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/python2
  2. import subprocess,re
  3. # kill action: 0 -> power off, 1 -> save state
  4. killAction = 1
  5. # Only change anything below this comment if you know, what you're doing!!
  6. print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<openbox_pipe_menu>")
  7. killActions = ['poweroff', 'savestate']
  8. notRunningVms = []
  9. runningVms = []
  10. vmNamePattern = re.compile('"(.+)" .*')
  11. for vm in subprocess.check_output(['VBoxManage', 'list', 'vms']).decode("utf-8").split("\n"):
  12. match = vmNamePattern.match(vm)
  13. if match:
  14. notRunningVms.append(match.group(1))
  15. for vm in subprocess.check_output(['VBoxManage', 'list', 'runningvms']).decode("utf-8").split("\n"):
  16. match = vmNamePattern.match(vm)
  17. if match:
  18. runningVms.append(match.group(1))
  19. notRunningVms.remove(match.group(1))
  20. if len(notRunningVms):
  21. print("<separator label=\"Start VM\"/>")
  22. for vm in notRunningVms:
  23. print("<item label=\"" + vm + "\">")
  24. print("<action name=\"Execute\">")
  25. print("<execute>VBoxManage startvm \"" + vm + "\"</execute>")
  26. print("</action>\n</item>")
  27. if len(runningVms):
  28. print("<separator label=\"Stop VM\"/>")
  29. for vm in runningVms:
  30. print("<item label=\"" + vm + "\">")
  31. print("<action name=\"Execute\">")
  32. print("<execute>VBoxManage controlvm \"" + vm + "\" " + killActions[killAction] + "</execute>")
  33. print("</action>\n</item>")
  34. print("</openbox_pipe_menu>")