Page 1 of 1

VirtualBox tool

Posted: 4. Nov 2010, 00:17
by damNageHack
Maybe you are interested in this useful script that I want to share. :D

VirtualBoxLauncher
Creates nice shortcuts in a XFCE launcher to directly start your virtual machines.

Image

Howto:
0. Make sure that VirtualBox is installed properly with also the SDK.
1. Create a launcher in the XFCE panel called "VirtualBox".
2. Adjust the path in the script that the python bindings will be found. Sorry, I did not find a better way to fix that.
3. Run the script. It will look for a launcher with the name "VirtualBox" and append new entries for each machine.
4. Log off from XFCE.
5. Copy the created file launcher-XXX.rc.new into launcher-XXX.rc.
6. Log into XFCE again.
7. Enjoy!

You can get the icons from virtualbox.org, place them to /usr/local/share/pixmaps and enhance the images list in the script depending on your needs.
:arrow: http://www.virtualbox.org/browser/trunk ... Box/images

Code: Select all

#!/usr/bin/python

import sys,os,ConfigParser

#FIXME: expand python path for VirtualBox XPCOM
#http://code.google.com/p/vboxweb/issues/detail?id=12
#http://code.google.com/p/vboxweb/issues/detail?id=16
vbox = "/usr/lib/virtualbox"
sys.path.append(vbox+"/sdk/bindings/xpcom/python")
sys.path.append(vbox) #for VBoxPython.so
import vboxapi

path = os.path.expanduser("~/.config/xfce4/panel")
match = ("launcher-",".rc")
option = ("Entry 0","Name")
title = "VirtualBox"
keep = ("Global",option[0])

def image(k):
	images = {
	"Other":"os_other",
	"WindowsXP":"os_winxp",
	"Linux":"os_linux_other"
	}
	return images.has_key(k) and images[k] or None

def template(m):
	return {
	"Name":m.name,
	"Icon":image(m.OSTypeId),
	"Exec":"VBoxManage startvm "+m.id
	}

def load():
	result = []
	for file in os.listdir(path):
		if file.startswith(match[0]) and file.endswith(match[1]):
			config = ConfigParser.ConfigParser()
			config.optionxform = str #case sensitive!
			config.read(os.path.join(path,file))
			if config.has_option(*option) and config.get(*option)==title:
				print "VirtualBox launcher configuration:",file
				result.append((config,file))
	return result
				
def generate(config,machines):
	for section in config.sections():
		if not section in keep:
			config.remove_section(section)
	count = 0
	print "Generate machine entries ..."
	for m in machines:
		t = m.getGuestProperty("/VirtualBox/GuestInfo/OS/Product")[0]
		print "%s (%s)"%(m.name,t)
		count += 1
		section = option[0][:-1]+str(count)
		config.add_section(section)
		for item in config.items(option[0]):
			if template(m).has_key(item[0]) and template(m)[item[0]]:
				config.set(section, item[0], template(m)[item[0]])
			else:
				config.set(section, *item)
	return config

def write(config,launcher):
	file = os.path.join(path,launcher[1]+".new")
	with open(file, 'wb') as fp:
		config.write(fp)
		print "New configuration written to",file

def main():
	virtualBoxManager = vboxapi.VirtualBoxManager(None,None)
	machines = virtualBoxManager.getArray(virtualBoxManager.vbox, 'machines')
	for launcher in load():
		config = generate(launcher[0],machines)
		write(config,launcher)
	
if __name__=="__main__":
	main()

Re: VirtualBox tool

Posted: 4. Nov 2010, 08:19
by zAchAry
Very cool, thank you for sharing :)
Does it work only with Xfce Panel, or it can be done with LXDE Panel as well?

Re: VirtualBox tool

Posted: 4. Nov 2010, 08:55
by damNageHack
I am glad to read that my script is useful.
Only XFCE is supported right now, sorry.

I will see if I can find some time to do another one for LXDE. As far as I know, there is no Launcher in that kind of XFCE available in LXDE, so maybe we have to use a new panel for all the VBox buttons, let's see what's possible.

Re: VirtualBox tool

Posted: 4. Nov 2010, 09:41
by zAchAry
damNageHack wrote:As far as I know, there is no Launcher in that kind of XFCE available in LXDE, so maybe we have to use a new panel for all the VBox buttons, let's see what's possible.
I wish, only for this circumstance, that LXDE had an Xfce-like Launcher, but usually I like more the LXDE Launcher since there is a series of Launchers in the same line, as a one element, I wish that Xfce had that, too.

Concerning to the separate panel of LXDE: You can auto-hide an LXDE Panel by doing the following: Panel Preferences > Advanced > Automatic hiding > Minimize panel when not in use

Re: VirtualBox tool

Posted: 4. Nov 2010, 10:06
by Shador
damNageHack wrote:I will see if I can find some time to do another one for LXDE. As far as I know, there is no Launcher in that kind of XFCE available in LXDE, so maybe we have to use a new panel for all the VBox buttons, let's see what's possible.
Maybe you could use the notification area to make it "cross-panel/-DE" compatible. Of course, that would require it to run as a background daemon.
It shouldn't be memory and cpu time consuming then. ;)

Re: VirtualBox tool

Posted: 4. Nov 2010, 10:13
by damNageHack
Shador wrote:Maybe you could use the notification area to make it "cross-panel/-DE" compatible. Of course, that would require it to run as a background daemon.
Yeah, great idea! :) It will then be also compliant to the freedesktop.org standards.
Shador wrote: It shouldn't be memory and cpu time consuming then. ;)
No, I do not think so. Due to the application will sleep most of the time, the panel does not consume either memory and cpu time due to more options in the launcher. The application has only to show some options in a list and run a command if one of them gets clicked. But that leads me to another thought, when we'll use the notification area, it is required to implement the user interface by our own, an advantage of the launcher is that it ships this logic already.
Keep it simple and smart! ;)

Re: VirtualBox tool

Posted: 4. Nov 2010, 20:03
by thenktor
Shador wrote:
damNageHack wrote:Maybe you could use the notification area to make it "cross-panel/-DE" compatible.
ACK!

EDIT: ACK is not known across english speakers. It means: "I agree".

Re: VirtualBox tool

Posted: 4. Nov 2010, 20:05
by zAchAry
thenktor wrote:ACK!
Ok, now I really dunno what you wrote, pick (just) one: ACK - What does ACK stand for?

Re: VirtualBox tool

Posted: 4. Nov 2010, 20:11
by damNageHack
It stands for my nick, of course :arrow: hACK. :D No not really, I am kidding.
Please come back to the topic!