-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBoxMenu.py
executable file
·101 lines (87 loc) · 3.23 KB
/
VBoxMenu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
""" VirtualBox tray (Windows)/menubar (Mac) console
It's one of the features I missed from Microsoft's Virtual PC (not anymore)
(c) Daniel Serodio 2009
"""
#########################################################################
#VBOX_HOME = 'C:/Program Files/Sun/Virtualbox'
VBOX_HOME = '/Applications/VirtualBox.app/Contents/MacOS'
VBOX_PNG = "/Applications/VirtualBox.app/Contents/Resources/virtualbox.png"
#EOL = '\r\n'
EOL = '\n'
#########################################################################
import re
import subprocess
import sys
from subprocess import Popen
try:
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
except ImportError:
from PySide import QtGui, QtCore
from PySide.QtCore import *
from PySide.QtGui import *
regex = re.compile(r'^"(.+)" {(.+)}') # "VM name" {UUID}
def icon_activated(reason):
if reason == QtGui.QSystemTrayIcon.DoubleClick:
subprocess.call("%s/VirtualBox" % VBOX_HOME)
class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)
menu = QtGui.QMenu(parent)
self.vms = {}
for v in list_vms():
submenu = QtGui.QMenu(v, menu)
self.vms[v] = {}
self.vms[v]['menu'] = submenu
start = submenu.addAction("Start", lambda vm=v: self.start_vm(vm))
self.vms[v]['start'] = start
stop = submenu.addAction("Stop", lambda vm=v: self.stop_vm(vm))
self.vms[v]['stop'] = stop
menu.addMenu(submenu)
self.connect(menu, SIGNAL("aboutToShow()"), self.refresh_menu)
menu.addSeparator()
exitAction = menu.addAction("Exit", lambda: exit(0))
self.setContextMenu(menu)
self.setToolTip("VirtualBox Tray Console")
traySignal = "activated(QSystemTrayIcon::ActivationReason)"
QtCore.QObject.connect(self, QtCore.SIGNAL(traySignal), icon_activated)
def start_vm(self, vm):
self.showMessage("VirtualBox", "Starting %s" % vm)
vbox_manage (['startvm', '%s' % vm])
def stop_vm(self, vm):
self.showMessage("VirtualBox", "Stopping %s" % vm)
vbox_manage (['stopvm', '%s' % vm])
def refresh_menu(self):
running_vms = []
for v in list_vms('runningvms'):
running_vms.append(v)
for v in self.vms:
if v not in running_vms:
self.vms[v]['start'].setEnabled(True)
self.vms[v]['stop'].setDisabled(True)
else:
self.vms[v]['start'].setDisabled(True)
self.vms[v]['stop'].setEnabled(True)
def main():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
trayIcon = SystemTrayIcon(QtGui.QIcon(VBOX_PNG), w)
trayIcon.show()
sys.exit(app.exec_())
def vbox_manage(argv):
argv.insert(0, "%s/VBoxManage" % VBOX_HOME)
proc = Popen(argv, stdout=subprocess.PIPE)
(out, err) = proc.communicate()
return out.split(EOL)
def list_vms(what='vms'):
vms = []
out = vbox_manage(['list', what])
for l in out:
m = regex.match(l)
if m:
vms.append(m.group(1))
return vms
if __name__ == "__main__":
main()