Changeset 103028 in vbox for trunk/src/VBox/Main/glue
- Timestamp:
- Jan 24, 2024 3:53:59 PM (12 months ago)
- Location:
- trunk/src/VBox/Main/glue/python
- Files:
-
- 3 added
- 1 copied
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/glue/python/Config.kmk
r102983 r103028 1 1 # $Id$ 2 2 ## @file 3 # kBuild Configuration file for VBoxShell.3 # kBuild Configuration file for Main glue for Python. 4 4 # 5 5 … … 26 26 # 27 27 28 VBOX_ VBOXSHELL_CONFIG_KMK_INCLUDED = 128 VBOX_MAIN_GLUE_PYTHON_CONFIG_KMK_INCLUDED = 1 29 29 30 30 # Include the top-level configure file. … … 36 36 # Globals 37 37 # 38 VBOX_PATH_ VBOXSHELL_SRC := $(PATH_ROOT)/src/VBox/Frontends/VBoxShell38 VBOX_PATH_MAIN_GLUE_PYTHON_SRC := $(PATH_ROOT)/src/VBox/Main/glue/python 39 39 40 40 # 41 41 # List of python sources that should be linted. 42 42 # 43 VBOX_ VBOXSHELL_PYTHON_SOURCES:=44 VBOX_ VBOXSHELL_PYLINT_TARGETS:=43 VBOX_MAIN_GLUE_PYTHON_PYTHON_SOURCES := 44 VBOX_MAIN_GLUE_PYTHON_PYLINT_TARGETS := 45 45 46 46 ifdef VBOX_WITH_PYLINT … … 51 51 # Process python sources. 52 52 # 53 if1of ($(KBUILD_TARGET), win os2) 54 VBOX_PYTHONPATH_VBOXSHELL = $(PYTHONPATH);$(VBOX_PATH_VBOXSHELL_SRC) 55 else 56 VBOX_PYTHONPATH_VBOXSHELL = $(PYTHONPATH):$(VBOX_PATH_VBOXSHELL_SRC) 57 endif 53 VBOX_PYTHONPATH_MAIN_GLUE_PYTHON = $(PYTHONPATH):$(VBOX_PATH_MAIN_GLUE_PYTHON_SRC) 58 54 BLDDIRS += $(PATH_TARGET)/pylint 59 55 60 define def_vbox_ vboxshell_py_check56 define def_vbox_main_glue_python_py_check 61 57 $(eval name:=$(basename $(notdir $(py)))) 62 58 … … 66 62 ifdef VBOX_WITH_PYLINT 67 63 $(QUIET2)$(call MSG_L1,Subjecting $(py) to pylint...) 68 $(QUIET)$(REDIRECT) -C "$(dir $(py))" -E LC_ALL=C -E PYTHONPATH="$(VBOX_PYTHONPATH_ VBOXSHELL)" -- \69 $(VBOX_PYLINT) --rcfile=$(VBOX_PATH_ VBOXSHELL_SRC)/pylintrc $$(VBOX_PYLINT_FLAGS) $$($(py)_VBOX_PYLINT_FLAGS) ./$(notdir $(py))64 $(QUIET)$(REDIRECT) -C "$(dir $(py))" -E LC_ALL=C -E PYTHONPATH="$(VBOX_PYTHONPATH_MAIN_GLUE_PYTHON)" -- \ 65 $(VBOX_PYLINT) --rcfile=$(VBOX_PATH_MAIN_GLUE_PYTHON_SRC)/pylintrc $$(VBOX_PYLINT_FLAGS) $$($(py)_VBOX_PYLINT_FLAGS) ./$(notdir $(py)) 70 66 endif 71 67 $(QUIET)$(APPEND) -t "$(PATH_TARGET)/pylint/$(name).o" 72 68 TESTING += $(name)-py-phony.o 73 VBOX_ VBOXSHELL_PYLINT_TARGETS += $(PATH_TARGET)/pylint/$(name).o74 endef # def_vbox_ vboxshell_py_check69 VBOX_MAIN_GLUE_PYTHON_PYLINT_TARGETS += $(PATH_TARGET)/pylint/$(name).o 70 endef # def_vbox_main_glue_python_py_check 75 71 76 72 77 define def_vbox_ vboxshell_process_python_sources73 define def_vbox_main_glue_python_process_python_sources 78 74 if $(words $(_SUB_MAKEFILE_STACK)) <= 0 || "$1" == "FORCE" 79 $(foreach py, $(VBOX_ VBOXSHELL_PYTHON_SOURCES), $(eval $(def_vbox_vboxshell_py_check)))75 $(foreach py, $(VBOX_MAIN_GLUE_PYTHON_PYTHON_SOURCES), $(eval $(def_vbox_main_glue_python_py_check))) 80 76 endif 81 77 endef -
trunk/src/VBox/Main/glue/python/vboxapi.py
r103027 r103028 1 1 # -*- coding: utf-8 -*- 2 2 # $Id$ 3 # pylint: disable=import-error -- for cross-platform Win32 imports 4 # pylint: disable=unused-import 5 # pylint: disable=protected-access -- for XPCOM _xpcom member 3 6 """ 4 7 VirtualBox Python API Glue. … … 49 52 50 53 if sys.version_info >= (3, 0): 51 xrange = range 52 long = int 54 xrange = range # pylint: disable=invalid-name 55 long = int # pylint: disable=invalid-name 53 56 54 57 # … … 56 59 # 57 60 import platform 58 VBoxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)59 VBoxSdkDir = os.environ.get("VBOX_SDK_PATH", None)60 61 if VBoxBinDir is None:61 g_sVBoxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None) 62 g_sVBoxSdkDir = os.environ.get("VBOX_SDK_PATH", None) 63 64 if g_sVBoxBinDir is None: 62 65 if platform.system() == 'Darwin': 63 VBoxBinDir = '/Applications/VirtualBox.app/Contents/MacOS'66 g_sVBoxBinDir = '/Applications/VirtualBox.app/Contents/MacOS' 64 67 else: # Will be set by the installer 65 VBoxBinDir = "%VBOX_INSTALL_PATH%"68 g_sVBoxBinDir = "%VBOX_INSTALL_PATH%" 66 69 else: 67 VBoxBinDir = os.path.abspath(VBoxBinDir)68 69 if VBoxSdkDir is None:70 g_sVBoxBinDir = os.path.abspath(g_sVBoxBinDir) 71 72 if g_sVBoxSdkDir is None: 70 73 if platform.system() == 'Darwin': 71 VBoxSdkDir = '/Applications/VirtualBox.app/Contents/MacOS/sdk'74 g_sVBoxSdkDir = '/Applications/VirtualBox.app/Contents/MacOS/sdk' 72 75 else: # Will be set by the installer 73 VBoxSdkDir = "%VBOX_SDK_PATH%"76 g_sVBoxSdkDir = "%VBOX_SDK_PATH%" 74 77 else: 75 VBoxSdkDir = os.path.abspath(VBoxSdkDir)76 77 os.environ["VBOX_PROGRAM_PATH"] = VBoxBinDir78 os.environ["VBOX_SDK_PATH"] = VBoxSdkDir79 sys.path.append( VBoxBinDir)78 g_sVBoxSdkDir = os.path.abspath(g_sVBoxSdkDir) 79 80 os.environ["VBOX_PROGRAM_PATH"] = g_sVBoxBinDir 81 os.environ["VBOX_SDK_PATH"] = g_sVBoxSdkDir 82 sys.path.append(g_sVBoxBinDir) 80 83 81 84 … … 104 107 """ 105 108 self.mgr = mgr 106 self.isMscom = (mgr.type == 'MSCOM')109 self.isMscom = mgr.type == 'MSCOM' 107 110 self.collector = vbox.performanceCollector 108 111 … … 138 141 # parameters (see #3953) for MSCOM. 139 142 if self.isMscom: 140 (values, names, objects, names_out, objects_out, units, scales, sequence_numbers,143 (values, names, objects, names_out, objects_out, units, scales, _sequence_numbers, 141 144 indices, lengths) = self.collector.queryMetricsData(names, objects) 142 145 else: 143 (values, names_out, objects_out, units, scales, sequence_numbers,146 (values, names_out, objects_out, units, scales, _sequence_numbers, 144 147 indices, lengths) = self.collector.queryMetricsData(names, objects) 145 148 out = [] 146 for i in xrange(0, len(names_out)):149 for i in enumerate(names_out): 147 150 scale = int(scales[i]) 148 151 if scale != 1: … … 165 168 # Attribute hacks. 166 169 # 167 def ComifyName(name):170 def comifyName(name): 168 171 return name[0].capitalize() + name[1:] 169 172 … … 171 174 ## This is for saving the original DispatchBaseClass __getattr__ and __setattr__ 172 175 # method references. 173 _g_dCOMForward = { 174 'getattr': None, 175 'setattr': None, 176 } 176 _g_dCOMForward = {} 177 177 178 178 … … 193 193 # Slow path. 194 194 try: 195 return _g_dCOMForward['getattr'](self, ComifyName(sAttr))195 return _g_dCOMForward['getattr'](self, comifyName(sAttr)) 196 196 except AttributeError: 197 197 return _g_dCOMForward['getattr'](self, sAttr) … … 201 201 """ Our setattr replacement for DispatchBaseClass. """ 202 202 try: 203 return _g_dCOMForward['setattr'](self, ComifyName(sAttr), oValue)203 return _g_dCOMForward['setattr'](self, comifyName(sAttr), oValue) 204 204 except AttributeError: 205 205 return _g_dCOMForward['setattr'](self, sAttr, oValue) … … 331 331 return None 332 332 333 def queryInterface(self, oIUnknown,sClassName):333 def queryInterface(self, _oIUnknown, _sClassName): 334 334 """ 335 335 IUnknown::QueryInterface wrapper. … … 344 344 # 345 345 346 def xcptGetStatus(self, oXcpt):346 def xcptGetStatus(self, _oXcpt): 347 347 """ 348 348 Returns the COM status code from the VBox API given exception. 349 349 """ 350 r eturn None351 352 def xcptIsDeadInterface(self, oXcpt):350 raise AttributeError 351 352 def xcptIsDeadInterface(self, _oXcpt): 353 353 """ 354 354 Returns True if the exception indicates that the interface is dead, False if not. … … 379 379 return False 380 380 381 def xcptGetMessage(self, oXcpt):381 def xcptGetMessage(self, _oXcpt): 382 382 """ 383 383 Returns the best error message found in the COM-like exception. … … 407 407 if sAttr[0].isupper() and (sAttr[1].isupper() or sAttr[1] == '_'): 408 408 oAttr = getattr(oSrc, sAttr) 409 if type(oAttr) is int:409 if isinstance(oAttr, int): 410 410 setattr(oDst, sAttr, oAttr) 411 411 return oDst … … 447 447 448 448 self.winerror = winerror 449 self.oHandle = None; 449 450 450 451 # Setup client impersonation in COM calls. … … 472 473 # Hack the COM dispatcher base class so we can modify method and 473 474 # attribute names to match those in xpcom. 474 if _g_dCOMForward['setattr'] is None:475 if 'getattr' not in _g_dCOMForward: 475 476 _g_dCOMForward['getattr'] = DispatchBaseClass.__dict__['__getattr__'] 477 setattr(DispatchBaseClass, '__getattr__', _CustomGetAttr) 478 479 if 'setattr' not in _g_dCOMForward: 476 480 _g_dCOMForward['setattr'] = DispatchBaseClass.__dict__['__setattr__'] 477 setattr(DispatchBaseClass, '__getattr__', _CustomGetAttr)478 481 setattr(DispatchBaseClass, '__setattr__', _CustomSetAttr) 479 482 … … 543 546 544 547 def getArray(self, oInterface, sAttrib): 545 return oInterface.__getattr__(sAttrib)548 return getattr(oInterface, sAttrib) 546 549 547 550 def setArray(self, oInterface, sAttrib, aoArray): … … 558 561 aPropMapGet = getattr(oInterface, '_prop_map_get_') 559 562 aPropMapPut = getattr(oInterface, '_prop_map_put_') 560 sComAttrib = sAttrib if sAttrib in aPropMapGet else ComifyName(sAttrib)563 sComAttrib = sAttrib if sAttrib in aPropMapGet else comifyName(sAttrib) 561 564 try: 562 aArgs, aDefaultArgs = aPropMapPut[sComAttrib]563 aGetArgs = aPropMapGet[sComAttrib]565 aArgs, _aDefaultArgs = aPropMapPut[sComAttrib] 566 aGetArgs = aPropMapGet[sComAttrib] 564 567 except KeyError: # fallback. 565 return oInterface.__setattr__(sAttrib, aoArray)568 return setattr(oInterface, sAttrib, aoArray) 566 569 567 570 import pythoncom … … 572 575 (aGetArgs[2],), # argTypes - trick: we get the type from the getter. 573 576 aoArray,) # The array 577 return True 574 578 575 579 def initPerThread(self): … … 582 586 583 587 def createListener(self, oImplClass, dArgs): 584 if True: 585 raise Exception('no active listeners on Windows as PyGatewayBase::QueryInterface() ' 586 'returns new gateway objects all the time, thus breaking EventQueue ' 587 'assumptions about the listener interface pointer being constants between calls ') 588 # Did this code ever really work? 589 d = {} 590 d['BaseClass'] = oImplClass 591 d['dArgs'] = dArgs 592 d['tlb_guid'] = PlatformMSCOM.VBOX_TLB_GUID 593 d['tlb_major'] = PlatformMSCOM.VBOX_TLB_MAJOR 594 d['tlb_minor'] = PlatformMSCOM.VBOX_TLB_MINOR 595 str_ = "" 596 str_ += "import win32com.server.util\n" 597 str_ += "import pythoncom\n" 598 599 str_ += "class ListenerImpl(BaseClass):\n" 600 str_ += " _com_interfaces_ = ['IEventListener']\n" 601 str_ += " _typelib_guid_ = tlb_guid\n" 602 str_ += " _typelib_version_ = tlb_major, tlb_minor\n" 603 str_ += " _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER\n" 604 # Maybe we'd better implement Dynamic invoke policy, to be more flexible here 605 str_ += " _reg_policy_spec_ = 'win32com.server.policy.EventHandlerPolicy'\n" 606 607 # capitalized version of listener method 608 str_ += " HandleEvent=BaseClass.handleEvent\n" 609 str_ += " def __init__(self): BaseClass.__init__(self, dArgs)\n" 610 str_ += "result = win32com.server.util.wrap(ListenerImpl())\n" 611 exec(str_, d, d) 612 return d['result'] 613 614 def waitForEvents(self, timeout): 588 raise Exception('no active listeners on Windows as PyGatewayBase::QueryInterface() ' 589 'returns new gateway objects all the time, thus breaking EventQueue ' 590 'assumptions about the listener interface pointer being constants between calls ') 591 592 def waitForEvents(self, cMsTimeout): 615 593 from win32api import GetCurrentThreadId 616 594 from win32event import INFINITE … … 619 597 import types 620 598 621 if not isinstance( timeout, int):599 if not isinstance(cMsTimeout, int): 622 600 raise TypeError("The timeout argument is not an integer") 623 601 if self.tid != GetCurrentThreadId(): 624 602 raise Exception("wait for events from the same thread you inited!") 625 603 626 if timeout < 0:604 if cMsTimeout < 0: 627 605 cMsTimeout = INFINITE 628 else:629 cMsTimeout = timeout630 606 rc = MsgWaitForMultipleObjects(self.aoHandles, 0, cMsTimeout, QS_ALLINPUT) 631 607 if WAIT_OBJECT_0 <= rc < WAIT_OBJECT_0 + len(self.aoHandles): … … 757 733 def __init__(self, dParams): 758 734 PlatformBase.__init__(self, dParams) 759 sys.path.append( VBoxSdkDir + '/bindings/xpcom/python/')735 sys.path.append(g_sVBoxSdkDir + '/bindings/xpcom/python/') 760 736 import xpcom.vboxxpcom 761 737 import xpcom … … 776 752 777 753 def getArray(self, oInterface, sAttrib): 778 return oInterface.__getattr__('get' + ComifyName(sAttrib))()754 return getattr(oInterface, 'get' + comifyName(sAttrib)); 779 755 780 756 def setArray(self, oInterface, sAttrib, aoArray): 781 return oInterface.__getattr__('set' + ComifyName(sAttrib))(aoArray)757 return setattr(oInterface, 'set' + comifyName(sAttrib), aoArray) 782 758 783 759 def initPerThread(self): … … 790 766 791 767 def createListener(self, oImplClass, dArgs): 792 d= {}793 d['BaseClass'] = oImplClass794 d['dArgs'] = dArgs795 s tr= ""796 s tr+= "import xpcom.components\n"797 s tr+= "class ListenerImpl(BaseClass):\n"798 s tr+= " _com_interfaces_ = xpcom.components.interfaces.IEventListener\n"799 s tr+= " def __init__(self): BaseClass.__init__(self, dArgs)\n"800 s tr+= "result = ListenerImpl()\n"801 exec(s tr, d, d)802 return d['result']803 804 def waitForEvents(self, timeout):768 notDocumentedDict = {} 769 notDocumentedDict['BaseClass'] = oImplClass 770 notDocumentedDict['dArgs'] = dArgs 771 sEval = "" 772 sEval += "import xpcom.components\n" 773 sEval += "class ListenerImpl(BaseClass):\n" 774 sEval += " _com_interfaces_ = xpcom.components.interfaces.IEventListener\n" 775 sEval += " def __init__(self): BaseClass.__init__(self, dArgs)\n" 776 sEval += "result = ListenerImpl()\n" 777 exec(sEval, notDocumentedDict, notDocumentedDict) # pylint: disable=exec-used 778 return notDocumentedDict['result'] 779 780 def waitForEvents(self, cMsTimeout): 805 781 import xpcom 806 return xpcom._xpcom.WaitForEvents( timeout)782 return xpcom._xpcom.WaitForEvents(cMsTimeout) 807 783 808 784 def interruptWaitEvents(self): … … 868 844 PlatformBase.__init__(self, dParams) 869 845 # Import web services stuff. Fix the sys.path the first time. 870 sWebServLib = os.path.join( VBoxSdkDir, 'bindings', 'webservice', 'python', 'lib')846 sWebServLib = os.path.join(g_sVBoxSdkDir, 'bindings', 'webservice', 'python', 'lib') 871 847 if sWebServLib not in sys.path: 872 848 sys.path.append(sWebServLib) … … 904 880 905 881 def getArray(self, oInterface, sAttrib): 906 return oInterface.__getattr__(sAttrib)882 return getattr(oInterface, sAttrib) 907 883 908 884 def setArray(self, oInterface, sAttrib, aoArray): 909 return oInterface.__setattr__(sAttrib, aoArray)910 911 def waitForEvents(self, timeout):885 return setattr(oInterface, sAttrib, aoArray) 886 887 def waitForEvents(self, _timeout): 912 888 # Webservices cannot do that yet 913 889 return 2 914 890 915 def interruptWaitEvents(self , timeout):891 def interruptWaitEvents(self): 916 892 # Webservices cannot do that yet 917 893 return False … … 924 900 925 901 def queryInterface(self, oIUnknown, sClassName): 926 d= {}927 d['oIUnknown'] = oIUnknown928 s tr= ""929 s tr+= "from VirtualBox_wrappers import " + sClassName + "\n"930 s tr+= "result = " + sClassName + "(oIUnknown.mgr, oIUnknown.handle)\n"902 notDocumentedDict = {} 903 notDocumentedDict['oIUnknown'] = oIUnknown 904 sEval = "" 905 sEval += "from VirtualBox_wrappers import " + sClassName + "\n" 906 sEval += "result = " + sClassName + "(oIUnknown.mgr, oIUnknown.handle)\n" 931 907 # wrong, need to test if class indeed implements this interface 932 exec(s tr, d, d)933 return d['result']908 exec(sEval, notDocumentedDict, notDocumentedDict) # pylint: disable=exec-used 909 return notDocumentedDict['result'] 934 910 935 911 # … … 970 946 # platforms at the same time, so this should be sufficent for most uses and 971 947 # be way simpler to use than VirtualBoxManager::oXcptClass. 972 CurXcptClass = None948 g_curXcptClass = None 973 949 974 950 … … 1029 1005 ## The exception class for the selected platform. 1030 1006 self.oXcptClass = self.platform.xcptGetBaseXcpt() 1031 global CurXcptClass1032 CurXcptClass = self.oXcptClass1007 global g_curXcptClass 1008 g_curXcptClass = self.oXcptClass 1033 1009 1034 1010 # Get the virtualbox singleton. 1035 1011 try: 1036 vbox =self.platform.getVirtualBox()1012 self.platform.getVirtualBox() 1037 1013 except NameError: 1038 1014 print("Installation problem: check that appropriate libs in place") … … 1157 1133 Returns the VirtualBox binary directory. 1158 1134 """ 1159 global VBoxBinDir 1160 return VBoxBinDir 1135 return g_sVBoxBinDir 1161 1136 1162 1137 def getSdkDir(self): … … 1164 1139 Returns the VirtualBox SDK directory. 1165 1140 """ 1166 global VBoxSdkDir 1167 return VBoxSdkDir 1141 return g_sVBoxSdkDir 1168 1142 1169 1143 def getEnumValueName(self, sEnumTypeNm, oEnumValue, fTypePrefix = False): … … 1182 1156 if len(dNamedValues) > 0: 1183 1157 1184 dValueNames = dict();1158 dValueNames = {}; 1185 1159 for sName in dNamedValues: 1186 1160 dValueNames[dNamedValues[sName]] = sName; … … 1266 1240 # Build the dictionary on demand. 1267 1241 if self._dErrorValToName is None: 1268 dErrorValToName = dict()1242 dErrorValToName = {} 1269 1243 for sKey in dir(self.statuses): 1270 1244 if sKey[0].isupper():
Note:
See TracChangeset
for help on using the changeset viewer.