VirtualBox

source: vbox/trunk/src/VBox/Main/glue/vboxapi.py@ 66619

Last change on this file since 66619 was 66619, checked in by vboxsync, 8 years ago

vboxapi.py: style adjustments

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.4 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: vboxapi.py 66619 2017-04-20 05:53:05Z vboxsync $
3"""
4VirtualBox Python API Glue.
5"""
6
7__copyright__ = \
8 """
9 Copyright (C) 2009-2016 Oracle Corporation
10
11 This file is part of VirtualBox Open Source Edition (OSE), as
12 available from http://www.virtualbox.org. This file is free software;
13 you can redistribute it and/or modify it under the terms of the GNU
14 General Public License (GPL) as published by the Free Software
15 Foundation, in version 2 as it comes in the "COPYING" file of the
16 VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18
19 The contents of this file may alternatively be used under the terms
20 of the Common Development and Distribution License Version 1.0
21 (CDDL) only, as it comes in the "COPYING.CDDL" file of the
22 VirtualBox OSE distribution, in which case the provisions of the
23 CDDL are applicable instead of those of the GPL.
24
25 You may elect to license modified versions of this file under the
26 terms and conditions of either the GPL or the CDDL or both.
27 """
28__version__ = "$Revision: 66619 $"
29
30
31# Note! To set Python bitness on OSX use 'export VERSIONER_PYTHON_PREFER_32_BIT=yes'
32
33
34# Standard Python imports.
35import os
36import sys
37import traceback
38
39
40if sys.version_info >= (3, 0):
41 xrange = range
42 long = int
43
44#
45# Globals, environment and sys.path changes.
46#
47import platform;
48VBoxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
49VBoxSdkDir = os.environ.get("VBOX_SDK_PATH", None)
50
51if VBoxBinDir is None:
52 if platform.system() == 'Darwin':
53 VBoxBinDir = '/Applications/VirtualBox.app/Contents/MacOS'
54 else: # Will be set by the installer
55 VBoxBinDir = "%VBOX_INSTALL_PATH%"
56else:
57 VBoxBinDir = os.path.abspath(VBoxBinDir)
58
59if VBoxSdkDir is None:
60 if platform.system() == 'Darwin':
61 VBoxSdkDir = '/Applications/VirtualBox.app/Contents/MacOS/sdk'
62 else: # Will be set by the installer
63 VBoxSdkDir = "%VBOX_SDK_PATH%"
64else:
65 VBoxSdkDir = os.path.abspath(VBoxSdkDir)
66
67os.environ["VBOX_PROGRAM_PATH"] = VBoxBinDir
68os.environ["VBOX_SDK_PATH"] = VBoxSdkDir
69sys.path.append(VBoxBinDir)
70
71
72#
73# Import the generated VirtualBox constants.
74#
75from .VirtualBox_constants import VirtualBoxReflectionInfo
76
77
78class PerfCollector(object):
79 """ This class provides a wrapper over IPerformanceCollector in order to
80 get more 'pythonic' interface.
81
82 To begin collection of metrics use setup() method.
83
84 To get collected data use query() method.
85
86 It is possible to disable metric collection without changing collection
87 parameters with disable() method. The enable() method resumes metric
88 collection.
89 """
90
91 def __init__(self, mgr, vbox):
92 """ Initializes the instance.
93
94 """
95 self.mgr = mgr
96 self.isMscom = (mgr.type == 'MSCOM')
97 self.collector = vbox.performanceCollector
98
99 def setup(self, names, objects, period, nsamples):
100 """ Discards all previously collected values for the specified
101 metrics, sets the period of collection and the number of retained
102 samples, enables collection.
103 """
104 self.collector.setupMetrics(names, objects, period, nsamples)
105
106 def enable(self, names, objects):
107 """ Resumes metric collection for the specified metrics.
108 """
109 self.collector.enableMetrics(names, objects)
110
111 def disable(self, names, objects):
112 """ Suspends metric collection for the specified metrics.
113 """
114 self.collector.disableMetrics(names, objects)
115
116 def query(self, names, objects):
117 """ Retrieves collected metric values as well as some auxiliary
118 information. Returns an array of dictionaries, one dictionary per
119 metric. Each dictionary contains the following entries:
120 'name': metric name
121 'object': managed object this metric associated with
122 'unit': unit of measurement
123 'scale': divide 'values' by this number to get float numbers
124 'values': collected data
125 'values_as_string': pre-processed values ready for 'print' statement
126 """
127 # Get around the problem with input arrays returned in output
128 # parameters (see #3953) for MSCOM.
129 if self.isMscom:
130 (values, names, objects, names_out, objects_out, units, scales, sequence_numbers,
131 indices, lengths) = self.collector.queryMetricsData(names, objects)
132 else:
133 (values, names_out, objects_out, units, scales, sequence_numbers,
134 indices, lengths) = self.collector.queryMetricsData(names, objects)
135 out = []
136 for i in xrange(0, len(names_out)):
137 scale = int(scales[i])
138 if scale != 1:
139 fmt = '%.2f%s'
140 else:
141 fmt = '%d %s'
142 out.append({
143 'name': str(names_out[i]),
144 'object': str(objects_out[i]),
145 'unit': str(units[i]),
146 'scale': scale,
147 'values': [int(values[j]) for j in xrange(int(indices[i]), int(indices[i]) + int(lengths[i]))],
148 'values_as_string': '[' + ', '.join([fmt % (int(values[j]) / scale, units[i]) for j in
149 xrange(int(indices[i]), int(indices[i]) + int(lengths[i]))]) + ']'
150 })
151 return out
152
153
154#
155# Attribute hacks.
156#
157def ComifyName(name):
158 return name[0].capitalize() + name[1:]
159
160
161## This is for saving the original DispatchBaseClass __getattr__ and __setattr__
162# method references.
163_g_dCOMForward = {
164 'getattr': None,
165 'setattr': None,
166}
167
168
169def _CustomGetAttr(self, sAttr):
170 """ Our getattr replacement for DispatchBaseClass. """
171 # Fastpath.
172 oRet = self.__class__.__dict__.get(sAttr)
173 if oRet is not None:
174 return oRet
175
176 # Try case-insensitivity workaround for class attributes (COM methods).
177 sAttrLower = sAttr.lower()
178 for k in list(self.__class__.__dict__.keys()):
179 if k.lower() == sAttrLower:
180 setattr(self.__class__, sAttr, self.__class__.__dict__[k])
181 return getattr(self, k)
182
183 # Slow path.
184 try:
185 return _g_dCOMForward['getattr'](self, ComifyName(sAttr))
186 except AttributeError:
187 return _g_dCOMForward['getattr'](self, sAttr)
188
189
190def _CustomSetAttr(self, sAttr, oValue):
191 """ Our setattr replacement for DispatchBaseClass. """
192 try:
193 return _g_dCOMForward['setattr'](self, ComifyName(sAttr), oValue)
194 except AttributeError:
195 return _g_dCOMForward['setattr'](self, sAttr, oValue)
196
197
198class PlatformBase(object):
199 """
200 Base class for the platform specific code.
201 """
202
203 def __init__(self, aoParams):
204 _ = aoParams
205
206 def getVirtualBox(self):
207 """
208 Gets a the IVirtualBox singleton.
209 """
210 return None
211
212 def getSessionObject(self, oIVBox):
213 """
214 Get a session object that can be used for opening machine sessions.
215
216 The oIVBox parameter is an getVirtualBox() return value, i.e. an
217 IVirtualBox reference.
218
219 See also openMachineSession.
220 """
221 _ = oIVBox
222 return None
223
224 def getType(self):
225 """ Returns the platform type (class name sans 'Platform'). """
226 return None
227
228 def isRemote(self):
229 """
230 Returns True if remote (web services) and False if local (COM/XPCOM).
231 """
232 return False
233
234 def getArray(self, oInterface, sAttrib):
235 """
236 Retrives the value of the array attribute 'sAttrib' from
237 interface 'oInterface'.
238
239 This is for hiding platform specific differences in attributes
240 returning arrays.
241 """
242 _ = oInterface
243 _ = sAttrib
244 return None
245
246 def setArray(self, oInterface, sAttrib, aoArray):
247 """
248 Sets the value (aoArray) of the array attribute 'sAttrib' in
249 interface 'oInterface'.
250
251 This is for hiding platform specific differences in attributes
252 setting arrays.
253 """
254 _ = oInterface
255 _ = sAttrib
256 _ = aoArray
257 return None
258
259 def initPerThread(self):
260 """
261 Does backend specific initialization for the calling thread.
262 """
263 return True
264
265 def deinitPerThread(self):
266 """
267 Does backend specific uninitialization for the calling thread.
268 """
269 return True
270
271 def createListener(self, oImplClass, dArgs):
272 """
273 Instantiates and wraps an active event listener class so it can be
274 passed to an event source for registration.
275
276 oImplClass is a class (type, not instance) which implements
277 IEventListener.
278
279 dArgs is a dictionary with string indexed variables. This may be
280 modified by the method to pass platform specific parameters. Can
281 be None.
282
283 This currently only works on XPCOM. COM support is not possible due to
284 shortcuts taken in the COM bridge code, which is not under our control.
285 Use passive listeners for COM and web services.
286 """
287 _ = oImplClass
288 _ = dArgs
289 raise Exception("No active listeners for this platform")
290
291 def waitForEvents(self, cMsTimeout):
292 """
293 Wait for events to arrive and process them.
294
295 The timeout (cMsTimeout) is in milliseconds for how long to wait for
296 events to arrive. A negative value means waiting for ever, while 0
297 does not wait at all.
298
299 Returns 0 if events was processed.
300 Returns 1 if timed out or interrupted in some way.
301 Returns 2 on error (like not supported for web services).
302
303 Raises an exception if the calling thread is not the main thread (the one
304 that initialized VirtualBoxManager) or if the time isn't an integer.
305 """
306 _ = cMsTimeout
307 return 2
308
309 def interruptWaitEvents(self):
310 """
311 Interrupt a waitForEvents call.
312 This is normally called from a worker thread to wake up the main thread.
313
314 Returns True on success, False on failure.
315 """
316 return False
317
318 def deinit(self):
319 """
320 Unitializes the platform specific backend.
321 """
322 return None
323
324 def queryInterface(self, oIUnknown, sClassName):
325 """
326 IUnknown::QueryInterface wrapper.
327
328 oIUnknown is who to ask.
329 sClassName is the name of the interface we're asking for.
330 """
331 return None
332
333 #
334 # Error (exception) access methods.
335 #
336
337 def xcptGetStatus(self, oXcpt):
338 """
339 Returns the COM status code from the VBox API given exception.
340 """
341 return None
342
343 def xcptIsDeadInterface(self, oXcpt):
344 """
345 Returns True if the exception indicates that the interface is dead, False if not.
346 """
347 return False
348
349 def xcptIsEqual(self, oXcpt, hrStatus):
350 """
351 Checks if the exception oXcpt is equal to the COM/XPCOM status code
352 hrStatus.
353
354 The oXcpt parameter can be any kind of object, we'll just return True
355 if it doesn't behave like a our exception class.
356
357 Will not raise any exception as long as hrStatus and self are not bad.
358 """
359 try:
360 hrXcpt = self.xcptGetStatus(oXcpt)
361 except AttributeError:
362 return False
363 if hrXcpt == hrStatus:
364 return True
365
366 # Fudge for 32-bit signed int conversion.
367 if 0x7fffffff < hrStatus <= 0xffffffff and hrXcpt < 0:
368 if (hrStatus - 0x100000000) == hrXcpt:
369 return True
370 return False
371
372 def xcptGetMessage(self, oXcpt):
373 """
374 Returns the best error message found in the COM-like exception.
375 Returns None to fall back on xcptToString.
376 Raises exception if oXcpt isn't our kind of exception object.
377 """
378 return None
379
380 def xcptGetBaseXcpt(self):
381 """
382 Returns the base exception class.
383 """
384 return None
385
386 def xcptSetupConstants(self, oDst):
387 """
388 Copy/whatever all error constants onto oDst.
389 """
390 return oDst
391
392 @staticmethod
393 def xcptCopyErrorConstants(oDst, oSrc):
394 """
395 Copy everything that looks like error constants from oDst to oSrc.
396 """
397 for sAttr in dir(oSrc):
398 if sAttr[0].isupper() and (sAttr[1].isupper() or sAttr[1] == '_'):
399 oAttr = getattr(oSrc, sAttr)
400 if type(oAttr) is int:
401 setattr(oDst, sAttr, oAttr)
402 return oDst
403
404
405class PlatformMSCOM(PlatformBase):
406 """
407 Platform specific code for MS COM.
408 """
409
410 ## @name VirtualBox COM Typelib definitions (should be generate)
411 #
412 # @remarks Must be updated when the corresponding VirtualBox.xidl bits
413 # are changed. Fortunately this isn't very often.
414 # @{
415 VBOX_TLB_GUID = '{D7569351-1750-46F0-936E-BD127D5BC264}'
416 VBOX_TLB_LCID = 0
417 VBOX_TLB_MAJOR = 1
418 VBOX_TLB_MINOR = 3
419 ## @}
420
421 def __init__(self, dParams):
422 PlatformBase.__init__(self, dParams)
423
424 #
425 # Since the code runs on all platforms, we have to do a lot of
426 # importing here instead of at the top of the file where it's normally located.
427 #
428 from win32com import universal
429 from win32com.client import gencache, DispatchBaseClass
430 from win32com.client import constants, getevents
431 import win32com
432 import pythoncom
433 import win32api
434 import winerror
435 from win32con import DUPLICATE_SAME_ACCESS
436 from win32api import GetCurrentThread, GetCurrentThreadId, DuplicateHandle, GetCurrentProcess
437 import threading
438
439 self.winerror = winerror
440
441 # Setup client impersonation in COM calls.
442 try:
443 pythoncom.CoInitializeSecurity(None,
444 None,
445 None,
446 pythoncom.RPC_C_AUTHN_LEVEL_DEFAULT,
447 pythoncom.RPC_C_IMP_LEVEL_IMPERSONATE,
448 None,
449 pythoncom.EOAC_NONE,
450 None)
451 except:
452 # handle RPC_E_TOO_LATE (repeat call of CoInitializeSecurity)
453 print("Warning: CoInitializeSecurity was already called")
454
455 pid = GetCurrentProcess()
456 self.tid = GetCurrentThreadId()
457 handle = DuplicateHandle(pid, GetCurrentThread(), pid, 0, 0, DUPLICATE_SAME_ACCESS)
458 self.handles = []
459 self.handles.append(handle)
460
461 # Hack the COM dispatcher base class so we can modify method and
462 # attribute names to match those in xpcom.
463 if _g_dCOMForward['setattr'] is None:
464 _g_dCOMForward['getattr'] = DispatchBaseClass.__dict__['__getattr__']
465 _g_dCOMForward['setattr'] = DispatchBaseClass.__dict__['__setattr__']
466 setattr(DispatchBaseClass, '__getattr__', _CustomGetAttr)
467 setattr(DispatchBaseClass, '__setattr__', _CustomSetAttr)
468
469 # Hack the exception base class so the users doesn't need to check for
470 # XPCOM or COM and do different things.
471 ## @todo
472
473 #
474 # Make sure the gencache is correct (we don't quite follow the COM
475 # versioning rules).
476 #
477 self.flushGenPyCache(win32com.client.gencache)
478 win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
479 win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBox')
480 win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBoxClient')
481
482 self.oClient = None ##< instance of client used to support lifetime of VBoxSDS
483 self.oIntCv = threading.Condition()
484 self.fInterrupted = False
485
486 _ = dParams
487
488 def flushGenPyCache(self, oGenCache):
489 """
490 Flushes VBox related files in the win32com gen_py cache.
491
492 This is necessary since we don't follow the typelib versioning rules
493 that everyeone else seems to subscribe to.
494 """
495 #
496 # The EnsureModule method have broken validation code, it doesn't take
497 # typelib module directories into account. So we brute force them here.
498 # (It's possible the directory approach is from some older pywin
499 # version or the result of runnig makepy or gencache manually, but we
500 # need to cover it as well.)
501 #
502 sName = oGenCache.GetGeneratedFileName(self.VBOX_TLB_GUID, self.VBOX_TLB_LCID,
503 self.VBOX_TLB_MAJOR, self.VBOX_TLB_MINOR)
504 sGenPath = oGenCache.GetGeneratePath()
505 if len(sName) > 36 and len(sGenPath) > 5:
506 sTypelibPath = os.path.join(sGenPath, sName)
507 if os.path.isdir(sTypelibPath):
508 import shutil
509 shutil.rmtree(sTypelibPath, ignore_errors=True)
510
511 #
512 # Ensure that our typelib is valid.
513 #
514 return oGenCache.EnsureModule(self.VBOX_TLB_GUID, self.VBOX_TLB_LCID, self.VBOX_TLB_MAJOR, self.VBOX_TLB_MINOR)
515
516 def getSessionObject(self, oIVBox):
517 _ = oIVBox
518 import win32com
519 from win32com.client import Dispatch
520 return win32com.client.Dispatch("VirtualBox.Session")
521
522 def getVirtualBox(self):
523 # Caching self.oClient is the trick for SDS. It allows to keep the
524 # VBoxSDS in the memory until the end of PlatformMSCOM lifetme.
525 if self.oClient is None:
526 import win32com
527 from win32com.client import Dispatch
528 self.oClient = win32com.client.Dispatch("VirtualBox.VirtualBoxClient")
529 return self.oClient.virtualBox
530
531 def getType(self):
532 return 'MSCOM'
533
534 def getArray(self, oInterface, sAttrib):
535 return oInterface.__getattr__(sAttrib)
536
537 def setArray(self, oInterface, sAttrib, aoArray):
538 #
539 # HACK ALERT!
540 #
541 # With pywin32 build 218, we're seeing type mismatch errors here for
542 # IGuestSession::environmentChanges (safearray of BSTRs). The Dispatch
543 # object (_oleobj_) seems to get some type conversion wrong and COM
544 # gets upset. So, we redo some of the dispatcher work here, picking
545 # the missing type information from the getter.
546 #
547 oOleObj = getattr(oInterface, '_oleobj_');
548 aPropMapGet = getattr(oInterface, '_prop_map_get_');
549 aPropMapPut = getattr(oInterface, '_prop_map_put_');
550 sComAttrib = sAttrib if sAttrib in aPropMapGet else ComifyName(sAttrib);
551 try:
552 aArgs, aDefaultArgs = aPropMapPut[sComAttrib];
553 aGetArgs = aPropMapGet[sComAttrib];
554 except KeyError: # fallback.
555 return oInterface.__setattr__(sAttrib, aoArray);
556
557 import pythoncom;
558 oOleObj.InvokeTypes(aArgs[0], # dispid
559 aArgs[1], # LCID
560 aArgs[2], # DISPATCH_PROPERTYPUT
561 (pythoncom.VT_HRESULT, 0), # retType - or void?
562 (aGetArgs[2],), # argTypes - trick: we get the type from the getter.
563 aoArray,); # The array
564
565 def initPerThread(self):
566 import pythoncom
567 pythoncom.CoInitializeEx(0)
568
569 def deinitPerThread(self):
570 import pythoncom
571 pythoncom.CoUninitialize()
572
573 def createListener(self, oImplClass, dArgs):
574 if True:
575 raise Exception('no active listeners on Windows as PyGatewayBase::QueryInterface() '
576 'returns new gateway objects all the time, thus breaking EventQueue '
577 'assumptions about the listener interface pointer being constants between calls ')
578 # Did this code ever really work?
579 d = {}
580 d['BaseClass'] = oImplClass
581 d['dArgs'] = dArgs
582 d['tlb_guid'] = PlatformMSCOM.VBOX_TLB_GUID
583 d['tlb_major'] = PlatformMSCOM.VBOX_TLB_MAJOR
584 d['tlb_minor'] = PlatformMSCOM.VBOX_TLB_MINOR
585 str_ = ""
586 str_ += "import win32com.server.util\n"
587 str_ += "import pythoncom\n"
588
589 str_ += "class ListenerImpl(BaseClass):\n"
590 str_ += " _com_interfaces_ = ['IEventListener']\n"
591 str_ += " _typelib_guid_ = tlb_guid\n"
592 str_ += " _typelib_version_ = tlb_major, tlb_minor\n"
593 str_ += " _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER\n"
594 # Maybe we'd better implement Dynamic invoke policy, to be more flexible here
595 str_ += " _reg_policy_spec_ = 'win32com.server.policy.EventHandlerPolicy'\n"
596
597 # capitalized version of listener method
598 str_ += " HandleEvent=BaseClass.handleEvent\n"
599 str_ += " def __init__(self): BaseClass.__init__(self, dArgs)\n"
600 str_ += "result = win32com.server.util.wrap(ListenerImpl())\n"
601 exec(str_, d, d)
602 return d['result']
603
604 def waitForEvents(self, timeout):
605 from win32api import GetCurrentThreadId
606 from win32event import INFINITE
607 from win32event import MsgWaitForMultipleObjects, \
608 QS_ALLINPUT, WAIT_TIMEOUT, WAIT_OBJECT_0
609 from pythoncom import PumpWaitingMessages
610 import types
611
612 if not isinstance(timeout, int):
613 raise TypeError("The timeout argument is not an integer")
614 if self.tid != GetCurrentThreadId():
615 raise Exception("wait for events from the same thread you inited!")
616
617 if timeout < 0:
618 cMsTimeout = INFINITE
619 else:
620 cMsTimeout = timeout
621 rc = MsgWaitForMultipleObjects(self.handles, 0, cMsTimeout, QS_ALLINPUT)
622 if WAIT_OBJECT_0 <= rc < WAIT_OBJECT_0 + len(self.handles):
623 # is it possible?
624 rc = 2
625 elif rc == WAIT_OBJECT_0 + len(self.handles):
626 # Waiting messages
627 PumpWaitingMessages()
628 rc = 0
629 else:
630 # Timeout
631 rc = 1
632
633 # check for interruption
634 self.oIntCv.acquire()
635 if self.fInterrupted:
636 self.fInterrupted = False
637 rc = 1
638 self.oIntCv.release()
639
640 return rc
641
642 def interruptWaitEvents(self):
643 """
644 Basically a python implementation of NativeEventQueue::postEvent().
645
646 The magic value must be in sync with the C++ implementation or this
647 won't work.
648
649 Note that because of this method we cannot easily make use of a
650 non-visible Window to handle the message like we would like to do.
651 """
652 from win32api import PostThreadMessage
653 from win32con import WM_USER
654
655 self.oIntCv.acquire()
656 self.fInterrupted = True
657 self.oIntCv.release()
658 try:
659 PostThreadMessage(self.tid, WM_USER, None, 0xf241b819)
660 except:
661 return False
662 return True
663
664 def deinit(self):
665 import pythoncom
666 from win32file import CloseHandle
667
668 for h in self.handles:
669 if h is not None:
670 CloseHandle(h)
671 self.handles = None
672 pythoncom.CoUninitialize()
673 pass
674
675 def queryInterface(self, oIUnknown, sClassName):
676 from win32com.client import CastTo
677 return CastTo(oIUnknown, sClassName)
678
679 def xcptGetStatus(self, oXcpt):
680 # The DISP_E_EXCEPTION + excptinfo fun needs checking up, only
681 # empirical info on it so far.
682 hrXcpt = oXcpt.hresult
683 if hrXcpt == self.winerror.DISP_E_EXCEPTION:
684 try:
685 hrXcpt = oXcpt.excepinfo[5]
686 except:
687 pass
688 return hrXcpt
689
690 def xcptIsDeadInterface(self, oXcpt):
691 return self.xcptGetStatus(oXcpt) in [
692 0x800706ba, -2147023174, # RPC_S_SERVER_UNAVAILABLE.
693 0x800706be, -2147023170, # RPC_S_CALL_FAILED.
694 0x800706bf, -2147023169, # RPC_S_CALL_FAILED_DNE.
695 0x80010108, -2147417848, # RPC_E_DISCONNECTED.
696 0x800706b5, -2147023179, # RPC_S_UNKNOWN_IF
697 ]
698
699 def xcptGetMessage(self, oXcpt):
700 if hasattr(oXcpt, 'excepinfo'):
701 try:
702 if len(oXcpt.excepinfo) >= 3:
703 sRet = oXcpt.excepinfo[2]
704 if len(sRet) > 0:
705 return sRet[0:]
706 except:
707 pass
708 if hasattr(oXcpt, 'strerror'):
709 try:
710 sRet = oXcpt.strerror
711 if len(sRet) > 0:
712 return sRet
713 except:
714 pass
715 return None
716
717 def xcptGetBaseXcpt(self):
718 import pythoncom
719
720 return pythoncom.com_error
721
722 def xcptSetupConstants(self, oDst):
723 import winerror
724
725 oDst = self.xcptCopyErrorConstants(oDst, winerror)
726
727 # XPCOM compatability constants.
728 oDst.NS_OK = oDst.S_OK
729 oDst.NS_ERROR_FAILURE = oDst.E_FAIL
730 oDst.NS_ERROR_ABORT = oDst.E_ABORT
731 oDst.NS_ERROR_NULL_POINTER = oDst.E_POINTER
732 oDst.NS_ERROR_NO_INTERFACE = oDst.E_NOINTERFACE
733 oDst.NS_ERROR_INVALID_ARG = oDst.E_INVALIDARG
734 oDst.NS_ERROR_OUT_OF_MEMORY = oDst.E_OUTOFMEMORY
735 oDst.NS_ERROR_NOT_IMPLEMENTED = oDst.E_NOTIMPL
736 oDst.NS_ERROR_UNEXPECTED = oDst.E_UNEXPECTED
737 return oDst
738
739
740class PlatformXPCOM(PlatformBase):
741 """
742 Platform specific code for XPCOM.
743 """
744
745 def __init__(self, dParams):
746 PlatformBase.__init__(self, dParams)
747 sys.path.append(VBoxSdkDir + '/bindings/xpcom/python/')
748 import xpcom.vboxxpcom
749 import xpcom
750 import xpcom.components
751 _ = dParams
752
753 def getSessionObject(self, oIVBox):
754 _ = oIVBox
755 import xpcom.components
756 return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
757
758 def getVirtualBox(self):
759 import xpcom.components
760 client = xpcom.components.classes["@virtualbox.org/VirtualBoxClient;1"].createInstance()
761 return client.virtualBox
762
763 def getType(self):
764 return 'XPCOM'
765
766 def getArray(self, oInterface, sAttrib):
767 return oInterface.__getattr__('get' + ComifyName(sAttrib))()
768
769 def setArray(self, oInterface, sAttrib, aoArray):
770 return oInterface.__getattr__('set' + ComifyName(sAttrib))(aoArray)
771
772 def initPerThread(self):
773 import xpcom
774 xpcom._xpcom.AttachThread()
775
776 def deinitPerThread(self):
777 import xpcom
778 xpcom._xpcom.DetachThread()
779
780 def createListener(self, oImplClass, dArgs):
781 d = {}
782 d['BaseClass'] = oImplClass
783 d['dArgs'] = dArgs
784 str = ""
785 str += "import xpcom.components\n"
786 str += "class ListenerImpl(BaseClass):\n"
787 str += " _com_interfaces_ = xpcom.components.interfaces.IEventListener\n"
788 str += " def __init__(self): BaseClass.__init__(self, dArgs)\n"
789 str += "result = ListenerImpl()\n"
790 exec(str, d, d)
791 return d['result']
792
793 def waitForEvents(self, timeout):
794 import xpcom
795 return xpcom._xpcom.WaitForEvents(timeout)
796
797 def interruptWaitEvents(self):
798 import xpcom
799 return xpcom._xpcom.InterruptWait()
800
801 def deinit(self):
802 import xpcom
803 xpcom._xpcom.DeinitCOM()
804
805 def queryInterface(self, oIUnknown, sClassName):
806 import xpcom.components
807 return oIUnknown.queryInterface(getattr(xpcom.components.interfaces, sClassName))
808
809 def xcptGetStatus(self, oXcpt):
810 return oXcpt.errno
811
812 def xcptIsDeadInterface(self, oXcpt):
813 return self.xcptGetStatus(oXcpt) in [
814 0x80004004, -2147467260, # NS_ERROR_ABORT
815 0x800706be, -2147023170, # NS_ERROR_CALL_FAILED (RPC_S_CALL_FAILED)
816 ]
817
818 def xcptGetMessage(self, oXcpt):
819 if hasattr(oXcpt, 'msg'):
820 try:
821 sRet = oXcpt.msg
822 if len(sRet) > 0:
823 return sRet
824 except:
825 pass
826 return None
827
828 def xcptGetBaseXcpt(self):
829 import xpcom
830 return xpcom.Exception
831
832 def xcptSetupConstants(self, oDst):
833 import xpcom
834 oDst = self.xcptCopyErrorConstants(oDst, xpcom.nsError)
835
836 # COM compatability constants.
837 oDst.E_ACCESSDENIED = -2147024891 # see VBox/com/defs.h
838 oDst.S_OK = oDst.NS_OK
839 oDst.E_FAIL = oDst.NS_ERROR_FAILURE
840 oDst.E_ABORT = oDst.NS_ERROR_ABORT
841 oDst.E_POINTER = oDst.NS_ERROR_NULL_POINTER
842 oDst.E_NOINTERFACE = oDst.NS_ERROR_NO_INTERFACE
843 oDst.E_INVALIDARG = oDst.NS_ERROR_INVALID_ARG
844 oDst.E_OUTOFMEMORY = oDst.NS_ERROR_OUT_OF_MEMORY
845 oDst.E_NOTIMPL = oDst.NS_ERROR_NOT_IMPLEMENTED
846 oDst.E_UNEXPECTED = oDst.NS_ERROR_UNEXPECTED
847 oDst.DISP_E_EXCEPTION = -2147352567 # For COM compatability only.
848 return oDst
849
850
851class PlatformWEBSERVICE(PlatformBase):
852 """
853 VirtualBox Web Services API specific code.
854 """
855
856 def __init__(self, dParams):
857 PlatformBase.__init__(self, dParams)
858 # Import web services stuff. Fix the sys.path the first time.
859 sWebServLib = os.path.join(VBoxSdkDir, 'bindings', 'webservice', 'python', 'lib')
860 if sWebServLib not in sys.path:
861 sys.path.append(sWebServLib)
862 import VirtualBox_wrappers
863 from VirtualBox_wrappers import IWebsessionManager2
864
865 # Initialize instance variables from parameters.
866 if dParams is not None:
867 self.user = dParams.get("user", "")
868 self.password = dParams.get("password", "")
869 self.url = dParams.get("url", "")
870 else:
871 self.user = ""
872 self.password = ""
873 self.url = None
874 self.vbox = None
875 self.wsmgr = None
876
877 #
878 # Base class overrides.
879 #
880
881 def getSessionObject(self, oIVBox):
882 return self.wsmgr.getSessionObject(oIVBox)
883
884 def getVirtualBox(self):
885 return self.connect(self.url, self.user, self.password)
886
887 def getType(self):
888 return 'WEBSERVICE'
889
890 def isRemote(self):
891 """ Returns True if remote VBox host, False if local. """
892 return True
893
894 def getArray(self, oInterface, sAttrib):
895 return oInterface.__getattr__(sAttrib)
896
897 def setArray(self, oInterface, sAttrib, aoArray):
898 return oInterface.__setattr__(sAttrib, aoArray)
899
900 def waitForEvents(self, timeout):
901 # Webservices cannot do that yet
902 return 2
903
904 def interruptWaitEvents(self, timeout):
905 # Webservices cannot do that yet
906 return False
907
908 def deinit(self):
909 try:
910 self.disconnect()
911 except:
912 pass
913
914 def queryInterface(self, oIUnknown, sClassName):
915 d = {}
916 d['oIUnknown'] = oIUnknown
917 str = ""
918 str += "from VirtualBox_wrappers import " + sClassName + "\n"
919 str += "result = " + sClassName + "(oIUnknown.mgr, oIUnknown.handle)\n"
920 # wrong, need to test if class indeed implements this interface
921 exec(str, d, d)
922 return d['result']
923
924 #
925 # Web service specific methods.
926 #
927
928 def connect(self, url, user, passwd):
929 if self.vbox is not None:
930 self.disconnect()
931 from VirtualBox_wrappers import IWebsessionManager2
932
933 if url is None:
934 url = ""
935 self.url = url
936 if user is None:
937 user = ""
938 self.user = user
939 if passwd is None:
940 passwd = ""
941 self.password = passwd
942 self.wsmgr = IWebsessionManager2(self.url)
943 self.vbox = self.wsmgr.logon(self.user, self.password)
944 if not self.vbox.handle:
945 raise Exception("cannot connect to '" + self.url + "' as '" + self.user + "'")
946 return self.vbox
947
948 def disconnect(self):
949 if self.vbox is not None and self.wsmgr is not None:
950 self.wsmgr.logoff(self.vbox)
951 self.vbox = None
952 self.wsmgr = None
953
954
955## The current (last) exception class.
956# This is reinitalized whenever VirtualBoxManager is called, so it will hold
957# the reference to the error exception class for the last platform/style that
958# was used. Most clients does talk to multiple VBox instance on different
959# platforms at the same time, so this should be sufficent for most uses and
960# be way simpler to use than VirtualBoxManager::oXcptClass.
961CurXctpClass = None
962
963
964class VirtualBoxManager(object):
965 """
966 VirtualBox API manager class.
967
968 The API users will have to instantiate this. If no parameters are given,
969 it will default to interface with the VirtualBox running on the local
970 machine. sStyle can be None (default), MSCOM, XPCOM or WEBSERVICES. Most
971 users will either be specifying None or WEBSERVICES.
972
973 The dPlatformParams is an optional dictionary for passing parameters to the
974 WEBSERVICE backend.
975 """
976
977 class Statuses(object):
978 def __init__(self):
979 pass
980
981 def __init__(self, sStyle=None, dPlatformParams=None):
982 if sStyle is None:
983 if sys.platform == 'win32':
984 sStyle = "MSCOM"
985 else:
986 sStyle = "XPCOM"
987 if sStyle == 'XPCOM':
988 self.platform = PlatformXPCOM(dPlatformParams)
989 elif sStyle == 'MSCOM':
990 self.platform = PlatformMSCOM(dPlatformParams)
991 elif sStyle == 'WEBSERVICE':
992 self.platform = PlatformWEBSERVICE(dPlatformParams)
993 else:
994 raise Exception('Unknown sStyle=%s' % (sStyle,))
995 self.style = sStyle
996 self.type = self.platform.getType()
997 self.remote = self.platform.isRemote()
998 ## VirtualBox API constants (for webservices, enums are symbolic).
999 self.constants = VirtualBoxReflectionInfo(sStyle == "WEBSERVICE")
1000
1001 ## Status constants.
1002 self.statuses = self.platform.xcptSetupConstants(VirtualBoxManager.Statuses())
1003 ## @todo Add VBOX_E_XXX to statuses? They're already in constants...
1004 ## Dictionary for errToString, built on demand.
1005 self._dErrorValToName = None
1006
1007 ## The exception class for the selected platform.
1008 self.oXcptClass = self.platform.xcptGetBaseXcpt()
1009 global CurXcptClass
1010 CurXcptClass = self.oXcptClass
1011
1012 # Get the virtualbox singleton.
1013 try:
1014 self.vbox = self.platform.getVirtualBox()
1015 except NameError:
1016 print("Installation problem: check that appropriate libs in place")
1017 traceback.print_exc()
1018 raise
1019 except Exception:
1020 _, e, _ = sys.exc_info()
1021 print("init exception: ", e)
1022 traceback.print_exc()
1023 if self.remote:
1024 self.vbox = None
1025 else:
1026 raise e
1027
1028 def __del__(self):
1029 self.deinit()
1030
1031 def getPythonApiRevision(self):
1032 """
1033 Returns a Python API revision number.
1034 This will be incremented when features are added to this file.
1035 """
1036 return 3
1037
1038 @property
1039 def mgr(self):
1040 """
1041 This used to be an attribute referring to a session manager class with
1042 only one method called getSessionObject. It moved into this class.
1043 """
1044 return self;
1045
1046 #
1047 # Wrappers for self.platform methods.
1048 #
1049 def getVirtualBox(self):
1050 """ See PlatformBase::getVirtualBox(). """
1051 return self.platform.getVirtualBox()
1052
1053 def getSessionObject(self, oIVBox):
1054 """ See PlatformBase::getSessionObject(). """
1055 return self.platform.getSessionObject(oIVBox)
1056
1057 def getArray(self, oInterface, sAttrib):
1058 """ See PlatformBase::getArray(). """
1059 return self.platform.getArray(oInterface, sAttrib)
1060
1061 def setArray(self, oInterface, sAttrib, aoArray):
1062 """ See PlatformBase::setArray(). """
1063 return self.platform.setArray(oInterface, sAttrib, aoArray)
1064
1065 def createListener(self, oImplClass, dArgs=None):
1066 """ See PlatformBase::createListener(). """
1067 return self.platform.createListener(oImplClass, dArgs)
1068
1069 def waitForEvents(self, cMsTimeout):
1070 """ See PlatformBase::waitForEvents(). """
1071 return self.platform.waitForEvents(cMsTimeout)
1072
1073 def interruptWaitEvents(self):
1074 """ See PlatformBase::interruptWaitEvents(). """
1075 return self.platform.interruptWaitEvents()
1076
1077 def queryInterface(self, oIUnknown, sClassName):
1078 """ See PlatformBase::queryInterface(). """
1079 return self.platform.queryInterface(oIUnknown, sClassName)
1080
1081 #
1082 # Init and uninit.
1083 #
1084 def initPerThread(self):
1085 """ See PlatformBase::deinitPerThread(). """
1086 self.platform.initPerThread()
1087
1088 def deinitPerThread(self):
1089 """ See PlatformBase::deinitPerThread(). """
1090 return self.platform.deinitPerThread()
1091
1092 def deinit(self):
1093 """
1094 For unitializing the manager.
1095 Do not access it after calling this method.
1096 """
1097 if hasattr(self, "vbox") and self.vbox is not None:
1098 del self.vbox
1099 self.vbox = None
1100 if hasattr(self, "platform") and self.platform is not None:
1101 self.platform.deinit()
1102 self.platform = None
1103 return True
1104
1105 #
1106 # Utility methods.
1107 #
1108 def openMachineSession(self, oIMachine, fPermitSharing=True):
1109 """
1110 Attempts to open the a session to the machine.
1111 Returns a session object on success.
1112 Raises exception on failure.
1113 """
1114 oSession = self.getSessionObject(self.vbox);
1115 if fPermitSharing:
1116 eType = self.constants.LockType_Shared
1117 else:
1118 eType = self.constants.LockType_Write
1119 oIMachine.lockMachine(oSession, eType)
1120 return oSession
1121
1122 def closeMachineSession(self, oSession):
1123 """
1124 Closes a session opened by openMachineSession.
1125 Ignores None parameters.
1126 """
1127 if oSession is not None:
1128 oSession.unlockMachine()
1129 return True
1130
1131 def getPerfCollector(self, oIVBox):
1132 """
1133 Returns a helper class (PerfCollector) for accessing performance
1134 collector goodies. See PerfCollector for details.
1135 """
1136 return PerfCollector(self, oIVBox)
1137
1138 def getBinDir(self):
1139 """
1140 Returns the VirtualBox binary directory.
1141 """
1142 global VBoxBinDir
1143 return VBoxBinDir
1144
1145 def getSdkDir(self):
1146 """
1147 Returns the VirtualBox SDK directory.
1148 """
1149 global VBoxSdkDir
1150 return VBoxSdkDir
1151
1152 #
1153 # Error code utilities.
1154 #
1155 ## @todo port to webservices!
1156 def xcptGetStatus(self, oXcpt=None):
1157 """
1158 Gets the status code from an exception. If the exception parameter
1159 isn't specified, the current exception is examined.
1160 """
1161 if oXcpt is None:
1162 oXcpt = sys.exc_info()[1]
1163 return self.platform.xcptGetStatus(oXcpt)
1164
1165 def xcptIsDeadInterface(self, oXcpt=None):
1166 """
1167 Returns True if the exception indicates that the interface is dead,
1168 False if not. If the exception parameter isn't specified, the current
1169 exception is examined.
1170 """
1171 if oXcpt is None:
1172 oXcpt = sys.exc_info()[1]
1173 return self.platform.xcptIsDeadInterface(oXcpt)
1174
1175 def xcptIsOurXcptKind(self, oXcpt=None):
1176 """
1177 Checks if the exception is one that could come from the VBox API. If
1178 the exception parameter isn't specified, the current exception is
1179 examined.
1180 """
1181 if self.oXcptClass is None: # @todo find the exception class for web services!
1182 return False
1183 if oXcpt is None:
1184 oXcpt = sys.exc_info()[1]
1185 return isinstance(oXcpt, self.oXcptClass)
1186
1187 def xcptIsEqual(self, oXcpt, hrStatus):
1188 """
1189 Checks if the exception oXcpt is equal to the COM/XPCOM status code
1190 hrStatus.
1191
1192 The oXcpt parameter can be any kind of object, we'll just return True
1193 if it doesn't behave like a our exception class. If it's None, we'll
1194 query the current exception and examine that.
1195
1196 Will not raise any exception as long as hrStatus and self are not bad.
1197 """
1198 if oXcpt is None:
1199 oXcpt = sys.exc_info()[1]
1200 return self.platform.xcptIsEqual(oXcpt, hrStatus)
1201
1202 def xcptIsNotEqual(self, oXcpt, hrStatus):
1203 """
1204 Negated xcptIsEqual.
1205 """
1206 return not self.xcptIsEqual(oXcpt, hrStatus)
1207
1208 def xcptToString(self, hrStatusOrXcpt=None):
1209 """
1210 Converts the specified COM status code, or the status code of the
1211 specified exception, to a C constant string. If the parameter isn't
1212 specified (is None), the current exception is examined.
1213 """
1214
1215 # Deal with exceptions.
1216 if hrStatusOrXcpt is None or self.xcptIsOurXcptKind(hrStatusOrXcpt):
1217 hrStatus = self.xcptGetStatus(hrStatusOrXcpt)
1218 else:
1219 hrStatus = hrStatusOrXcpt
1220
1221 # Build the dictionary on demand.
1222 if self._dErrorValToName is None:
1223 dErrorValToName = dict()
1224 for sKey in dir(self.statuses):
1225 if sKey[0].isupper():
1226 oValue = getattr(self.statuses, sKey)
1227 if type(oValue) is int:
1228 dErrorValToName[oValue] = sKey
1229 self._dErrorValToName = dErrorValToName
1230
1231 # Do the lookup, falling back on formatting the status number.
1232 try:
1233 sStr = self._dErrorValToName[int(hrStatus)]
1234 except KeyError:
1235 hrLong = long(hrStatus)
1236 sStr = '%#x (%d)' % (hrLong, hrLong)
1237 return sStr
1238
1239 def xcptGetMessage(self, oXcpt=None):
1240 """
1241 Returns the best error message found in the COM-like exception. If the
1242 exception parameter isn't specified, the current exception is examined.
1243 """
1244 if oXcpt is None:
1245 oXcpt = sys.exc_info()[1]
1246 sRet = self.platform.xcptGetMessage(oXcpt)
1247 if sRet is None:
1248 sRet = self.xcptToString(oXcpt)
1249 return sRet
1250
1251 # Legacy, remove in a day or two.
1252 errGetStatus = xcptGetStatus
1253 errIsDeadInterface = xcptIsDeadInterface
1254 errIsOurXcptKind = xcptIsOurXcptKind
1255 errGetMessage = xcptGetMessage
1256
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette