vboxapi.py revision b24a4c8472377f462bad08867b6203fe8bbe9663
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# Copyright (C) 2009 Sun Microsystems, Inc.
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# This file is part of VirtualBox Open Source Edition (OSE), as
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# available from http://www.virtualbox.org. This file is free software;
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# you can redistribute it and/or modify it under the terms of the GNU
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# General Public License (GPL) as published by the Free Software
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# Foundation, in version 2 as it comes in the "COPYING" file of the
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# Clara, CA 95054 USA or visit http://www.sun.com if you need
5f40efb5cd27c6ff21ca70bf3271564e7e79e3a4vboxsync# additional information or have any questions.
08122b11035de1e54ce1e665dff7260fc548db72vboxsyncVboxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
2561352ae77d8f2f825526a6cbafa34b45f16972vboxsync # Will be set by the installer
c65e2fedaf400b449a85ae6db7b84858f2613708vboxsyncfrom VirtualBox_constants import VirtualBoxReflectionInfo
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync """ This class provides a wrapper over IPerformanceCollector in order to
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync get more 'pythonic' interface.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync To begin collection of metrics use setup() method.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync To get collected data use query() method.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync It is possible to disable metric collection without changing collection
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync parameters with disable() method. The enable() method resumes metric
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync collection.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync """ Initializes the instance.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync Pass an instance of IVirtualBox as parameter.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync """ Discards all previously collected values for the specified
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync metrics, sets the period of collection and the number of retained
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync samples, enables collection.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync self.collector.setupMetrics(names, objects, period, nsamples)
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync """ Resumes metric collection for the specified metrics.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync """ Suspends metric collection for the specified metrics.
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync """ Retrieves collected metric values as well as some auxiliary
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync information. Returns an array of dictionaries, one dictionary per
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync metric. Each dictionary contains the following entries:
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync 'name': metric name
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync 'object': managed object this metric associated with
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync 'unit': unit of measurement
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync 'scale': divide 'values' by this number to get float numbers
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync 'values': collected data
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync 'values_as_string': pre-processed values ready for 'print' statement
a7f701e8c51193f7c21137cc173ea5f86e53cac2vboxsync # Get around the problem with input arrays returned in output parameters (see #3953).
a7f701e8c51193f7c21137cc173ea5f86e53cac2vboxsync (values, names, objects, names_out, objects_out, units, scales, sequence_numbers,
a7f701e8c51193f7c21137cc173ea5f86e53cac2vboxsync indices, lengths) = self.collector.queryMetricsData(names, objects)
a7f701e8c51193f7c21137cc173ea5f86e53cac2vboxsync (values, names_out, objects_out, units, scales, sequence_numbers,
a7f701e8c51193f7c21137cc173ea5f86e53cac2vboxsync indices, lengths) = self.collector.queryMetricsData(names, objects)
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync 'values':[int(values[j]) for j in xrange(int(indices[i]), int(indices[i])+int(lengths[i]))],
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync 'values_as_string':'['+', '.join([fmt % (int(values[j])/scale, units[i]) for j in xrange(int(indices[i]), int(indices[i])+int(lengths[i]))])+']'
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync 'setattr' : None}
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync # try case-insensitivity workaround for class attributes (COM methods)
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync self.__class__.__dict__[attr] = self.__class__.__dict__[k]
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync return _COMForward['getattr'](self,ComifyName(attr))
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync return _COMForward['setattr'](self, ComifyName(attr), value)
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync # Class to fake access to constants in style of foo.bar.boo
08122b11035de1e54ce1e665dff7260fc548db72vboxsync self.__dict__['_depth']=parent.__dict__['_depth']+1
7baa1c3bb51b48e79eee63a69e341442e342a18evboxsync if fake != None:
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync while parent != None:
7baa1c3bb51b48e79eee63a69e341442e342a18evboxsync if name is not None:
da570ef57fe454ae2d9d5d88a7bfea214723dbb1vboxsync self.__dict__['_rootFake'] = PlatformMSCOM.ConstantFake(None, None)
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync VBOX_TLB_GUID = '{46137EEC-703B-4FE5-AFD4-7C9BBBBA0259}'
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync from win32com.client import gencache, DispatchBaseClass
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync from win32api import GetCurrentThread,GetCurrentThreadId,DuplicateHandle,GetCurrentProcess
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync handle = DuplicateHandle(pid, GetCurrentThread(), pid, 0, 0, DUPLICATE_SAME_ACCESS)
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync _COMForward['getattr'] = DispatchBaseClass.__dict__['__getattr__']
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync DispatchBaseClass.__dict__['__getattr__'] = CustomGetAttr
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync _COMForward['setattr'] = DispatchBaseClass.__dict__['__setattr__']
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync DispatchBaseClass.__dict__['__setattr__'] = CustomSetAttr
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBox')
2ea0ec406117609d51bd5ac51cbb3d4f0de9a16dvboxsync return win32com.client.Dispatch("VirtualBox.Session")
2ea0ec406117609d51bd5ac51cbb3d4f0de9a16dvboxsync return win32com.client.Dispatch("VirtualBox.VirtualBox")
08122b11035de1e54ce1e665dff7260fc548db72vboxsync return 'MSCOM'
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync #str += "import win32com.server.register\n"
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync #str += "from win32com import universal\n"
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync #str += "universal.RegisterInterfaces(tlb_guid, 0, 1, 0, ['"+iface+"'])\n"
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync #str += " _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER\n"
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync #str += " _reg_clsid_ = '{F21202A2-959A-4149-B1C3-68B9013F3335}'\n"
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync #str += " _reg_progid_ = 'VirtualBox."+iface+"Impl'\n"
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync #str += " _reg_desc_ = 'Generated callback implementation class'\n"
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync #str += " _reg_policy_spec_ = 'win32com.server.policy.EventHandlerPolicy'\n"
0637d4d8793a5ce81c481f2cac4a8daf0fb91683vboxsync # generate capitalized version of callbacks - that's how Python COM
0637d4d8793a5ce81c481f2cac4a8daf0fb91683vboxsync # looks them up on Windows
3acd338a7a2154c8d73de9a2629bc7057b051334vboxsync str += " def __init__(self): BaseClass.__init__(self, arg)\n"
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync #str += "win32com.server.register.UseCommandLine("+iface+"Impl)\n"
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync str += "result = win32com.server.util.wrap("+iface+"Impl())\n"
d6bdf8a836b8e7d95eec19c254cd39161731d48fvboxsync return d['result']
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync from win32event import MsgWaitForMultipleObjects, \
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync raise Exception("wait for events from the same thread you inited!")
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync rc = MsgWaitForMultipleObjects(self.handles, 0, timeout, QS_ALLINPUT)
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync if rc >= WAIT_OBJECT_0 and rc < WAIT_OBJECT_0+len(self.handles):
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync # is it possible?
a23d9b6011c292ab4d858fc7d83a2216843cd54evboxsync # Waiting messages
36a0cf44771c76b56b4a6489136e3adf0343df0bvboxsync if h is not None:
08122b11035de1e54ce1e665dff7260fc548db72vboxsync sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
08122b11035de1e54ce1e665dff7260fc548db72vboxsync return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
08122b11035de1e54ce1e665dff7260fc548db72vboxsync return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
08122b11035de1e54ce1e665dff7260fc548db72vboxsync return 'XPCOM'
d6bdf8a836b8e7d95eec19c254cd39161731d48fvboxsync str += " _com_interfaces_ = xpcom.components.interfaces."+iface+"\n"
3acd338a7a2154c8d73de9a2629bc7057b051334vboxsync str += " def __init__(self): BaseClass.__init__(self, arg)\n"
d6bdf8a836b8e7d95eec19c254cd39161731d48fvboxsync return d['result']
08122b11035de1e54ce1e665dff7260fc548db72vboxsync sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
b24a4c8472377f462bad08867b6203fe8bbe9663vboxsync # not really needed, but just fail early if misconfigured
5857f4e58ce2ef50d7f0c450fe4897026f9a9c3dvboxsync from VirtualBox_wrappers import IWebsessionManager2
5857f4e58ce2ef50d7f0c450fe4897026f9a9c3dvboxsync if params is not None:
b24a4c8472377f462bad08867b6203fe8bbe9663vboxsync from VirtualBox_wrappers import IWebsessionManager2
b24a4c8472377f462bad08867b6203fe8bbe9663vboxsync if user is None:
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync self.vbox = self.wsmgr.logon(self.user, self.password)
b24a4c8472377f462bad08867b6203fe8bbe9663vboxsync if self.vbox is not None and self.wsmgr is not None:
5857f4e58ce2ef50d7f0c450fe4897026f9a9c3dvboxsync return None
08122b11035de1e54ce1e665dff7260fc548db72vboxsync return 'WEBSERVICE'
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync # Webservices cannot do that yet
08122b11035de1e54ce1e665dff7260fc548db72vboxsync exec "self.platform = Platform"+style+"(platparams)"
08122b11035de1e54ce1e665dff7260fc548db72vboxsync print "init exception: ",e