vboxapi.py revision 5c57f3ae389a4fd2d1614c2a02a52bc7d56ee7b5
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# Copyright (C) 2009 Sun Microsystems, Inc.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# This file is part of VirtualBox Open Source Edition (OSE), as
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# available from http://www.virtualbox.org. This file is free software;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# you can redistribute it and/or modify it under the terms of the GNU
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# General Public License (GPL) as published by the Free Software
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# Foundation, in version 2 as it comes in the "COPYING" file of the
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# Clara, CA 95054 USA or visit http://www.sun.com if you need
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync# additional information or have any questions.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncimport sys,os
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncimport traceback
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncVboxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncVboxSdkDir = os.environ.get("VBOX_SDK_PATH", None)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncif VboxBinDir is None:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync # Will be set by the installer
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync VboxBinDir = "%VBOX_INSTALL_PATH%"
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncif VboxSdkDir is None:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync VboxSdkDir = os.path.join(VboxBinDir,"sdk")
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncos.environ["VBOX_PROGRAM_PATH"] = VboxBinDir
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncos.environ["VBOX_SDK_PATH"] = VboxSdkDir
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncsys.path.append(VboxBinDir)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncfrom VirtualBox_constants import VirtualBoxReflectionInfo
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncclass PerfCollector:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """ This class provides a wrapper over IPerformanceCollector in order to
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync get more 'pythonic' interface.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync To begin collection of metrics use setup() method.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync To get collected data use query() method.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync It is possible to disable metric collection without changing collection
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync parameters with disable() method. The enable() method resumes metric
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync collection.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def __init__(self, mgr, vbox):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """ Initializes the instance.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.mgr = mgr
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.isMscom = (mgr.type == 'MSCOM')
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.collector = vbox.performanceCollector
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def setup(self, names, objects, period, nsamples):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """ Discards all previously collected values for the specified
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync metrics, sets the period of collection and the number of retained
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync samples, enables collection.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.collector.setupMetrics(names, objects, period, nsamples)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def enable(self, names, objects):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """ Resumes metric collection for the specified metrics.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.collector.enableMetrics(names, objects)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def disable(self, names, objects):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """ Suspends metric collection for the specified metrics.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.collector.disableMetrics(names, objects)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def query(self, names, objects):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """ Retrieves collected metric values as well as some auxiliary
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync information. Returns an array of dictionaries, one dictionary per
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync metric. Each dictionary contains the following entries:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'name': metric name
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'object': managed object this metric associated with
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'unit': unit of measurement
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'scale': divide 'values' by this number to get float numbers
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'values': collected data
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'values_as_string': pre-processed values ready for 'print' statement
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync """
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync # Get around the problem with input arrays returned in output
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync # parameters (see #3953) for MSCOM.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if self.isMscom:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync (values, names, objects, names_out, objects_out, units, scales, sequence_numbers,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync indices, lengths) = self.collector.queryMetricsData(names, objects)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync else:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync (values, names_out, objects_out, units, scales, sequence_numbers,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync indices, lengths) = self.collector.queryMetricsData(names, objects)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync out = []
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync for i in xrange(0, len(names_out)):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync scale = int(scales[i])
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if scale != 1:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync fmt = '%.2f%s'
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync else:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync fmt = '%d %s'
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync out.append({
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'name':str(names_out[i]),
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'object':str(objects_out[i]),
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'unit':str(units[i]),
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'scale':scale,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'values':[int(values[j]) for j in xrange(int(indices[i]), int(indices[i])+int(lengths[i]))],
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'values_as_string':'['+', '.join([fmt % (int(values[j])/scale, units[i]) for j in xrange(int(indices[i]), int(indices[i])+int(lengths[i]))])+']'
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync })
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return out
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncdef ComifyName(name):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return name[0].capitalize()+name[1:]
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync_COMForward = { 'getattr' : None,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync 'setattr' : None}
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncdef CustomGetAttr(self, attr):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync # fastpath
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if self.__class__.__dict__.get(attr) != None:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return self.__class__.__dict__.get(attr)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync # try case-insensitivity workaround for class attributes (COM methods)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync for k in self.__class__.__dict__.keys():
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if k.lower() == attr.lower():
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.__class__.__dict__[attr] = self.__class__.__dict__[k]
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return getattr(self, k)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync try:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return _COMForward['getattr'](self,ComifyName(attr))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync except AttributeError:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return _COMForward['getattr'](self,attr)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncdef CustomSetAttr(self, attr, value):
2d8f7775ea7361ee20e62a6463e996d9234c7029vboxsync try:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return _COMForward['setattr'](self, ComifyName(attr), value)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync except AttributeError:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return _COMForward['setattr'](self, attr, value)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncclass PlatformMSCOM:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync # Class to fake access to constants in style of foo.bar.boo
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync class ConstantFake:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def __init__(self, parent, name):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.__dict__['_parent'] = parent
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.__dict__['_name'] = name
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.__dict__['_consts'] = {}
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync try:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.__dict__['_depth']=parent.__dict__['_depth']+1
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync except:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.__dict__['_depth']=0
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if self.__dict__['_depth'] > 4:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync raise AttributeError
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def __getattr__(self, attr):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync import win32com
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync from win32com.client import constants
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if attr.startswith("__"):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync raise AttributeError
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync consts = self.__dict__['_consts']
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync fake = consts.get(attr, None)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if fake != None:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return fake
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync try:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync name = self.__dict__['_name']
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync parent = self.__dict__['_parent']
689e8c44f580478449d47bd48c9af52e82a028aevboxsync while parent != None:
689e8c44f580478449d47bd48c9af52e82a028aevboxsync if parent._name is not None:
689e8c44f580478449d47bd48c9af52e82a028aevboxsync name = parent._name+'_'+name
689e8c44f580478449d47bd48c9af52e82a028aevboxsync parent = parent._parent
689e8c44f580478449d47bd48c9af52e82a028aevboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if name is not None:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync name += "_" + attr
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync else:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync name = attr
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return win32com.client.constants.__getattr__(name)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync except AttributeError,e:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync fake = PlatformMSCOM.ConstantFake(self, attr)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync consts[attr] = fake
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return fake
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync class InterfacesWrapper:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def __init__(self):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync self.__dict__['_rootFake'] = PlatformMSCOM.ConstantFake(None, None)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync def __getattr__(self, a):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync import win32com
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync from win32com.client import constants
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if a.startswith("__"):
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync raise AttributeError
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync try:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return win32com.client.constants.__getattr__(a)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync except AttributeError,e:
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return self.__dict__['_rootFake'].__getattr__(a)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync VBOX_TLB_GUID = '{46137EEC-703B-4FE5-AFD4-7C9BBBBA0259}'
VBOX_TLB_LCID = 0
VBOX_TLB_MAJOR = 1
VBOX_TLB_MINOR = 0
def __init__(self, params):
from win32com import universal
from win32com.client import gencache, DispatchBaseClass
from win32com.client import constants, getevents
import win32com
import pythoncom
import win32api
from win32con import DUPLICATE_SAME_ACCESS
from win32api import GetCurrentThread,GetCurrentThreadId,DuplicateHandle,GetCurrentProcess
pid = GetCurrentProcess()
self.tid = GetCurrentThreadId()
handle = DuplicateHandle(pid, GetCurrentThread(), pid, 0, 0, DUPLICATE_SAME_ACCESS)
self.handles = []
self.handles.append(handle)
_COMForward['getattr'] = DispatchBaseClass.__dict__['__getattr__']
DispatchBaseClass.__dict__['__getattr__'] = CustomGetAttr
_COMForward['setattr'] = DispatchBaseClass.__dict__['__setattr__']
DispatchBaseClass.__dict__['__setattr__'] = CustomSetAttr
win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBox')
win32com.client.gencache.EnsureDispatch('VirtualBox.CallbackWrapper')
def getSessionObject(self, vbox):
import win32com
from win32com.client import Dispatch
return win32com.client.Dispatch("VirtualBox.Session")
def getVirtualBox(self):
import win32com
from win32com.client import Dispatch
return win32com.client.Dispatch("VirtualBox.VirtualBox")
def getType(self):
return 'MSCOM'
def getRemote(self):
return False
def getArray(self, obj, field):
return obj.__getattr__(field)
def initPerThread(self):
import pythoncom
pythoncom.CoInitializeEx(0)
def deinitPerThread(self):
import pythoncom
pythoncom.CoUninitialize()
def createCallback(self, iface, impl, arg):
d = {}
d['BaseClass'] = impl
d['arg'] = arg
d['tlb_guid'] = PlatformMSCOM.VBOX_TLB_GUID
str = ""
str += "import win32com.server.util\n"
str += "import pythoncom\n"
str += "class "+iface+"Impl(BaseClass):\n"
str += " _com_interfaces_ = ['"+iface+"']\n"
str += " _typelib_guid_ = tlb_guid\n"
str += " _typelib_version_ = 1, 0\n"
# generate capitalized version of callback methods -
# that's how Python COM looks them up
for m in dir(impl):
if m.startswith("on"):
str += " "+ComifyName(m)+"=BaseClass."+m+"\n"
str += " def __init__(self): BaseClass.__init__(self, arg)\n"
str += "result = win32com.client.Dispatch('VirtualBox.CallbackWrapper')\n"
str += "result.SetLocalObject("+iface+"Impl())\n"
exec (str,d,d)
return d['result']
def waitForEvents(self, timeout):
from win32api import GetCurrentThreadId
from win32event import MsgWaitForMultipleObjects, \
QS_ALLINPUT, WAIT_TIMEOUT, WAIT_OBJECT_0
from pythoncom import PumpWaitingMessages
if (self.tid != GetCurrentThreadId()):
raise Exception("wait for events from the same thread you inited!")
rc = MsgWaitForMultipleObjects(self.handles, 0, timeout, QS_ALLINPUT)
if rc >= WAIT_OBJECT_0 and rc < WAIT_OBJECT_0+len(self.handles):
# is it possible?
pass
elif rc==WAIT_OBJECT_0 + len(self.handles):
# Waiting messages
PumpWaitingMessages()
else:
# Timeout
pass
def deinit(self):
import pythoncom
from win32file import CloseHandle
for h in self.handles:
if h is not None:
CloseHandle(h)
self.handles = None
pythoncom.CoUninitialize()
pass
class PlatformXPCOM:
def __init__(self, params):
sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
import xpcom.vboxxpcom
import xpcom
import xpcom.components
def getSessionObject(self, vbox):
import xpcom.components
return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
def getVirtualBox(self):
import xpcom.components
return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
def getType(self):
return 'XPCOM'
def getRemote(self):
return False
def getArray(self, obj, field):
return obj.__getattr__('get'+ComifyName(field))()
def initPerThread(self):
import xpcom
xpcom._xpcom.AttachThread()
def deinitPerThread(self):
import xpcom
xpcom._xpcom.DetachThread()
def createCallback(self, iface, impl, arg):
d = {}
d['BaseClass'] = impl
d['arg'] = arg
str = ""
str += "import xpcom.components\n"
str += "class "+iface+"Impl(BaseClass):\n"
str += " _com_interfaces_ = xpcom.components.interfaces."+iface+"\n"
str += " def __init__(self): BaseClass.__init__(self, arg)\n"
str += "result = xpcom.components.classes['@virtualbox.org/VirtualBoxCallback;1'].createInstance()\n"
str += "result.setLocalObject("+iface+"Impl())\n"
exec (str,d,d)
return d['result']
def waitForEvents(self, timeout):
import xpcom
xpcom._xpcom.WaitForEvents(timeout)
def deinit(self):
import xpcom
xpcom._xpcom.DeinitCOM()
class PlatformWEBSERVICE:
def __init__(self, params):
sys.path.append(os.path.join(VboxSdkDir,'bindings', 'webservice', 'python', 'lib'))
# not really needed, but just fail early if misconfigured
import VirtualBox_services
import VirtualBox_wrappers
from VirtualBox_wrappers import IWebsessionManager2
if params is not None:
self.user = params.get("user", "")
self.password = params.get("password", "")
self.url = params.get("url", "")
else:
self.user = ""
self.password = ""
self.url = None
self.vbox = None
def getSessionObject(self, vbox):
return self.wsmgr.getSessionObject(vbox)
def getVirtualBox(self):
return self.connect(self.url, self.user, self.password)
def connect(self, url, user, passwd):
if self.vbox is not None:
self.disconnect()
from VirtualBox_wrappers import IWebsessionManager2
if url is None:
url = ""
self.url = url
if user is None:
user = ""
self.user = user
if passwd is None:
passwd = ""
self.password = passwd
self.wsmgr = IWebsessionManager2(self.url)
self.vbox = self.wsmgr.logon(self.user, self.password)
if not self.vbox.handle:
raise Exception("cannot connect to '"+self.url+"' as '"+self.user+"'")
return self.vbox
def disconnect(self):
if self.vbox is not None and self.wsmgr is not None:
self.wsmgr.logoff(self.vbox)
self.vbox = None
self.wsmgr = None
def getType(self):
return 'WEBSERVICE'
def getRemote(self):
return True
def getArray(self, obj, field):
return obj.__getattr__(field)
def initPerThread(self):
pass
def deinitPerThread(self):
pass
def createCallback(self, iface, impl, arg):
raise Exception("no callbacks for webservices")
def waitForEvents(self, timeout):
# Webservices cannot do that yet
pass
def deinit(self):
try:
disconnect()
except:
pass
class SessionManager:
def __init__(self, mgr):
self.mgr = mgr
def getSessionObject(self, vbox):
return self.mgr.platform.getSessionObject(vbox)
class VirtualBoxManager:
def __init__(self, style, platparams):
if style is None:
if sys.platform == 'win32':
style = "MSCOM"
else:
style = "XPCOM"
exec "self.platform = Platform"+style+"(platparams)"
# for webservices, enums are symbolic
self.constants = VirtualBoxReflectionInfo(style == "WEBSERVICE")
self.type = self.platform.getType()
self.remote = self.platform.getRemote()
self.style = style
self.mgr = SessionManager(self)
try:
self.vbox = self.platform.getVirtualBox()
except NameError,ne:
print "Installation problem: check that appropriate libs in place"
traceback.print_exc()
raise ne
except Exception,e:
print "init exception: ",e
traceback.print_exc()
if self.remote:
self.vbox = None
else:
raise e
def getArray(self, obj, field):
return self.platform.getArray(obj, field)
def getVirtualBox(self):
return self.platform.getVirtualBox()
def __del__(self):
self.deinit()
def deinit(self):
if hasattr(self, "vbox"):
del self.vbox
self.vbox = None
if hasattr(self, "platform"):
self.platform.deinit()
self.platform = None
def initPerThread(self):
self.platform.initPerThread()
def openMachineSession(self, machineId):
session = self.mgr.getSessionObject(self.vbox)
self.vbox.openSession(session, machineId)
return session
def closeMachineSession(self, session):
session.close()
def deinitPerThread(self):
self.platform.deinitPerThread()
def createCallback(self, iface, impl, arg):
return self.platform.createCallback(iface, impl, arg)
def waitForEvents(self, timeout):
return self.platform.waitForEvents(timeout)
def getPerfCollector(self, vbox):
return PerfCollector(self, vbox)