vboxapi.py revision 29179bad4ddd202d265455fed7c5eae723859a8e
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync# Copyright (C) 2009 Oracle Corporation
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.
b5b8f3d0d95893edd81062c1b5d0bc455cc79bc1vboxsync# To set Python bitness on OSX use 'export VERSIONER_PYTHON_PREFER_32_BIT=yes'
08122b11035de1e54ce1e665dff7260fc548db72vboxsyncVboxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
2561352ae77d8f2f825526a6cbafa34b45f16972vboxsync # Will be set by the installer
b5b8f3d0d95893edd81062c1b5d0bc455cc79bc1vboxsync # 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 """ 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
8b2f1e9e24bf9cb4340412463a0e75c4e0b035a6vboxsync # Get around the problem with input arrays returned in output
aaa80e9df329609078dea844a11f2611443b677avboxsync # parameters (see #3953) for MSCOM.
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__']
8b2f1e9e24bf9cb4340412463a0e75c4e0b035a6vboxsync DispatchBaseClass.__dict__['__getattr__'] = CustomGetAttr
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync _COMForward['setattr'] = DispatchBaseClass.__dict__['__setattr__']
8b2f1e9e24bf9cb4340412463a0e75c4e0b035a6vboxsync DispatchBaseClass.__dict__['__setattr__'] = CustomSetAttr
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
5cb8545771849c97101a5ee9bb57d0fdac922c44vboxsync win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBox')
5c57f3ae389a4fd2d1614c2a02a52bc7d56ee7b5vboxsync win32com.client.gencache.EnsureDispatch('VirtualBox.CallbackWrapper')
1d96c25e994a2160c1697208bb398d79ca117c4avboxsync return win32com.client.Dispatch("VirtualBox.Session")
2ea0ec406117609d51bd5ac51cbb3d4f0de9a16dvboxsync return win32com.client.Dispatch("VirtualBox.VirtualBox")
08122b11035de1e54ce1e665dff7260fc548db72vboxsync return 'MSCOM'
17c83edfe874290c12f93dd01ee0302045b5a659vboxsync str += " _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER\n"
17c83edfe874290c12f93dd01ee0302045b5a659vboxsync # Maybe we'd better implement Dynamic invoke policy, to be more flexible here
17c83edfe874290c12f93dd01ee0302045b5a659vboxsync str += " _reg_policy_spec_ = 'win32com.server.policy.EventHandlerPolicy'\n"
70fc3227c8df64856d2282ee60fee11fe35cd5devboxsync # generate capitalized version of callback methods -
5c57f3ae389a4fd2d1614c2a02a52bc7d56ee7b5vboxsync # that's how Python COM looks them up
3acd338a7a2154c8d73de9a2629bc7057b051334vboxsync str += " def __init__(self): BaseClass.__init__(self, arg)\n"
5c57f3ae389a4fd2d1614c2a02a52bc7d56ee7b5vboxsync str += "result = win32com.client.Dispatch('VirtualBox.CallbackWrapper')\n"
17c83edfe874290c12f93dd01ee0302045b5a659vboxsync str += "result.SetLocalObject(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"
d879e0dbc8f0a06aea09adc18f3be9711918bd19vboxsync str += "result = xpcom.components.classes['@virtualbox.org/CallbackWrapper;1'].createInstance()\n"
d6bdf8a836b8e7d95eec19c254cd39161731d48fvboxsync return d['result']
0c657f93ac727a3a1644497331d86fbcbc3722aavboxsync sys.path.append(os.path.join(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:
355edde34018a687b4a4d17054c378b780e3d8abvboxsync return self.connect(self.url, self.user, self.password)
b24a4c8472377f462bad08867b6203fe8bbe9663vboxsync from VirtualBox_wrappers import IWebsessionManager2
355edde34018a687b4a4d17054c378b780e3d8abvboxsync if url is None:
b24a4c8472377f462bad08867b6203fe8bbe9663vboxsync if user is None:
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync self.vbox = self.wsmgr.logon(self.user, self.password)
355edde34018a687b4a4d17054c378b780e3d8abvboxsync raise Exception("cannot connect to '"+self.url+"' as '"+self.user+"'")
b24a4c8472377f462bad08867b6203fe8bbe9663vboxsync if self.vbox is not None and self.wsmgr is not None:
08122b11035de1e54ce1e665dff7260fc548db72vboxsync return 'WEBSERVICE'
c2e62d39261f9f69ab4e14b2bbd986bf1b1faaf9vboxsync # Webservices cannot do that yet
619da14dbf1f40ada039a6ebceedec8a6abe7696vboxsync # Webservices cannot do that yet
760ec8be64fa28d2bc7f493d707385c757a12e8evboxsync exec "self.platform = Platform"+style+"(platparams)"
760ec8be64fa28d2bc7f493d707385c757a12e8evboxsync # for webservices, enums are symbolic
760ec8be64fa28d2bc7f493d707385c757a12e8evboxsync self.constants = VirtualBoxReflectionInfo(style == "WEBSERVICE")
0c657f93ac727a3a1644497331d86fbcbc3722aavboxsync print "Installation problem: check that appropriate libs in place"
08122b11035de1e54ce1e665dff7260fc548db72vboxsync print "init exception: ",e
29179bad4ddd202d265455fed7c5eae723859a8evboxsync if session is not None: