vboxapi.py revision 4fea7a40e3d4c0e903287c0ee728512f3e63df72
#
# Copyright (C) 2009 Oracle Corporation
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the "COPYING" file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#
import traceback
# To set Python bitness on OSX use 'export VERSIONER_PYTHON_PREFER_32_BIT=yes'
if VboxBinDir is None:
# Will be set by the installer
VboxBinDir = "%VBOX_INSTALL_PATH%"
if VboxSdkDir is None:
# Will be set by the installer
VboxSdkDir = "%VBOX_SDK_PATH%"
from VirtualBox_constants import VirtualBoxReflectionInfo
class PerfCollector:
""" This class provides a wrapper over IPerformanceCollector in order to
get more 'pythonic' interface.
To begin collection of metrics use setup() method.
To get collected data use query() method.
It is possible to disable metric collection without changing collection
parameters with disable() method. The enable() method resumes metric
collection.
"""
""" Initializes the instance.
"""
""" Discards all previously collected values for the specified
metrics, sets the period of collection and the number of retained
samples, enables collection.
"""
""" Resumes metric collection for the specified metrics.
"""
""" Suspends metric collection for the specified metrics.
"""
""" Retrieves collected metric values as well as some auxiliary
information. Returns an array of dictionaries, one dictionary per
metric. Each dictionary contains the following entries:
'name': metric name
'object': managed object this metric associated with
'unit': unit of measurement
'scale': divide 'values' by this number to get float numbers
'values': collected data
'values_as_string': pre-processed values ready for 'print' statement
"""
# Get around the problem with input arrays returned in output
# parameters (see #3953) for MSCOM.
else:
out = []
if scale != 1:
fmt = '%.2f%s'
else:
fmt = '%d %s'
'scale':scale,
'values_as_string':'['+', '.join([fmt % (int(values[j])/scale, units[i]) for j in xrange(int(indices[i]), int(indices[i])+int(lengths[i]))])+']'
})
return out
def ComifyName(name):
_COMForward = { 'getattr' : None,
'setattr' : None}
# fastpath
# try case-insensitivity workaround for class attributes (COM methods)
try:
except AttributeError:
try:
except AttributeError:
class PlatformMSCOM:
# Class to fake access to constants in style of foo.bar.boo
class ConstantFake:
try:
except:
raise AttributeError
import win32com
raise AttributeError
if fake != None:
return fake
try:
while parent != None:
if name is not None:
else:
except AttributeError,e:
return fake
class InterfacesWrapper:
def __getattr__(self, a):
import win32com
if a.startswith("__"):
raise AttributeError
try:
except AttributeError,e:
VBOX_TLB_GUID = '{46137EEC-703B-4FE5-AFD4-7C9BBBBA0259}'
VBOX_TLB_LCID = 0
VBOX_TLB_MAJOR = 1
VBOX_TLB_MINOR = 0
import win32com
import pythoncom
import win32api
from win32con import DUPLICATE_SAME_ACCESS
pid = GetCurrentProcess()
import win32com
def getVirtualBox(self):
import win32com
return 'MSCOM'
return False
def initPerThread(self):
import pythoncom
def deinitPerThread(self):
import pythoncom
d = {}
d['BaseClass'] = impl
d['arg'] = arg
str = ""
str += "import win32com.server.util\n"
str += "import pythoncom\n"
str += "class ListenerImpl(BaseClass):\n"
str += " _com_interfaces_ = ['IEventListener']\n"
str += " _typelib_guid_ = tlb_guid\n"
str += " _typelib_version_ = 1, 0\n"
str += " _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER\n"
# Maybe we'd better implement Dynamic invoke policy, to be more flexible here
str += " _reg_policy_spec_ = 'win32com.server.policy.EventHandlerPolicy'\n"
# capitalized version of listener method
str += " HandleEvent=BaseClass.handleEvent\n"
str += " def __init__(self): BaseClass.__init__(self, arg)\n"
str += "result = win32com.server.util.wrap(ListenerImpl())\n"
exec (str,d,d)
return d['result']
from win32api import GetCurrentThreadId
from win32event import MsgWaitForMultipleObjects, \
from pythoncom import PumpWaitingMessages
raise Exception("wait for events from the same thread you inited!")
# is it possible?
pass
# Waiting messages
else:
# Timeout
pass
def interruptWaitEvents(self):
from win32api import PostThreadMessage
import pythoncom
from win32file import CloseHandle
if h is not None:
CloseHandle(h)
pass
class PlatformXPCOM:
import xpcom
import xpcom.components
import xpcom.components
def getVirtualBox(self):
import xpcom.components
return 'XPCOM'
return False
def initPerThread(self):
import xpcom
def deinitPerThread(self):
import xpcom
d = {}
d['BaseClass'] = impl
d['arg'] = arg
str = ""
str += "import xpcom.components\n"
str += "class ListenerImpl(BaseClass):\n"
str += " _com_interfaces_ = xpcom.components.interfaces.IEventListener\n"
str += " def __init__(self): BaseClass.__init__(self, arg)\n"
str += "result = ListenerImpl()\n"
exec (str,d,d)
return d['result']
import xpcom
def interruptWaitEvents(self):
import xpcom
import xpcom
import xpcom.components
class PlatformWEBSERVICE:
# 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:
else:
def getVirtualBox(self):
from VirtualBox_wrappers import IWebsessionManager2
if url is None:
url = ""
if user is None:
user = ""
if passwd is None:
passwd = ""
def disconnect(self):
return 'WEBSERVICE'
return True
def initPerThread(self):
pass
def deinitPerThread(self):
pass
raise Exception("no active listeners for webservices")
# Webservices cannot do that yet
pass
# Webservices cannot do that yet
pass
try:
except:
pass
d = {}
d['obj'] = obj
str = ""
# wrong, need to test if class indeed implements this interface
exec (str,d,d)
return d['result']
class SessionManager:
class VirtualBoxManager:
if style is None:
style = "MSCOM"
else:
style = "XPCOM"
# for webservices, enums are symbolic
try:
print "Installation problem: check that appropriate libs in place"
raise ne
except Exception,e:
print "init exception: ",e
else:
raise e
def getVirtualBox(self):
def initPerThread(self):
if permitSharing:
else:
return session
if session is not None:
def deinitPerThread(self):
def interruptWaitEvents(self):
global VboxBinDir
return VboxBinDir
global VboxSdkDir
return VboxSdkDir