VirtualBox

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

Last change on this file since 20693 was 20693, checked in by vboxsync, 16 years ago

Python: Windows waiting for event, additional shell command

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1#
2# Copyright (C) 2009 Sun Microsystems, Inc.
3#
4# This file is part of VirtualBox Open Source Edition (OSE), as
5# available from http://www.215389.xyz. This file is free software;
6# you can redistribute it and/or modify it under the terms of the GNU
7# General Public License (GPL) as published by the Free Software
8# Foundation, in version 2 as it comes in the "COPYING" file of the
9# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
10# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
11#
12# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
13# Clara, CA 95054 USA or visit http://www.sun.com if you need
14# additional information or have any questions.
15#
16import sys,os
17import traceback
18
19VboxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
20VboxSdkDir = os.environ.get("VBOX_SDK_PATH", None)
21
22if VboxBinDir is None:
23 # Will be set by the installer
24 VboxBinDir = "%VBOX_INSTALL_PATH%"
25
26if VboxSdkDir is None:
27 VboxSdkDir = VboxBinDir+"/sdk"
28
29os.environ["VBOX_PROGRAM_PATH"] = VboxBinDir
30os.environ["VBOX_SDK_PATH"] = VboxSdkDir
31sys.path.append(VboxBinDir)
32
33from VirtualBox_constants import VirtualBoxReflectionInfo
34
35class PlatformMSCOM:
36 # Class to fake access to constants in style of foo.bar.boo
37 class ConstantFake:
38 def __init__(self, parent, name):
39 self.__dict__['_parent'] = parent
40 self.__dict__['_name'] = name
41 self.__dict__['_consts'] = {}
42 try:
43 self.__dict__['_depth']=parent.__dict__['_depth']+1
44 except:
45 self.__dict__['_depth']=0
46 if self.__dict__['_depth'] > 4:
47 raise AttributeError
48
49 def __getattr__(self, attr):
50 import win32com
51 from win32com.client import constants
52
53 if attr.startswith("__"):
54 raise AttributeError
55
56 consts = self.__dict__['_consts']
57
58 fake = consts.get(attr, None)
59 if fake != None:
60 return fake
61 try:
62 name = self.__dict__['_name']
63 parent = self.__dict__['_parent']
64 while parent != None:
65 if parent._name is not None:
66 name = parent._name+'_'+name
67 parent = parent._parent
68
69 if name is not None:
70 name += "_" + attr
71 else:
72 name = attr
73 return win32com.client.constants.__getattr__(name)
74 except AttributeError,e:
75 fake = PlatformMSCOM.ConstantFake(self, attr)
76 consts[attr] = fake
77 return fake
78
79
80 class InterfacesWrapper:
81 def __init__(self):
82 self.__dict__['_rootFake'] = PlatformMSCOM.ConstantFake(None, None)
83
84 def __getattr__(self, a):
85 import win32com
86 from win32com.client import constants
87 if a.startswith("__"):
88 raise AttributeError
89 try:
90 return win32com.client.constants.__getattr__(a)
91 except AttributeError,e:
92 return self.__dict__['_rootFake'].__getattr__(a)
93
94 VBOX_TLB_GUID = '{46137EEC-703B-4FE5-AFD4-7C9BBBBA0259}'
95 VBOX_TLB_LCID = 0
96 VBOX_TLB_MAJOR = 1
97 VBOX_TLB_MINOR = 0
98
99 def __init__(self, params):
100 from win32com import universal
101 from win32com.client import gencache, DispatchWithEvents, Dispatch
102 from win32com.client import constants, getevents
103 import win32com
104 import pythoncom
105 import win32api
106 self.constants = PlatformMSCOM.InterfacesWrapper()
107
108 def getSessionObject(self, vbox):
109 import win32com
110 from win32com.client import Dispatch
111 return win32com.client.Dispatch("VirtualBox.Session")
112
113 def getVirtualBox(self):
114 import win32com
115 from win32com.client import Dispatch
116 return win32com.client.Dispatch("VirtualBox.VirtualBox")
117
118 def getConstants(self):
119 return self.constants
120
121 def getType(self):
122 return 'MSCOM'
123
124 def getRemote(self):
125 return False
126
127 def getArray(self, obj, field):
128 return obj.__getattr__(field)
129
130 def initPerThread(self):
131 import pythoncom
132 pythoncom.CoInitializeEx(0)
133
134 def deinitPerThread(self):
135 import pythoncom
136 pythoncom.CoUninitialize()
137
138 def createCallback(self, iface, impl, arg):
139 d = {}
140 d['BaseClass'] = impl
141 d['arg'] = arg
142 d['tlb_guid'] = PlatformMSCOM.VBOX_TLB_GUID
143 str = ""
144 str += "import win32com.server.util\n"
145 #str += "from win32com import universal\n"
146 #str += "import pythoncom\n"
147 #str += "universal.RegisterInterfaces(tlb_guid, 0, 1, 0, ['"+iface+"'])\n"
148
149 str += "class "+iface+"Impl(BaseClass):\n"
150 str += " _com_interfaces_ = ['"+iface+"']\n"
151 str += " _typelib_guid_ = tlb_guid\n"
152 str += " _typelib_version_ = 1, 0\n"
153 #str += " _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER\n"
154 #str += " _reg_clsid_ = '{F21202A2-959A-4149-B1C3-68B9013F3335}'\n"
155 #str += " _reg_progid_ = 'VirtualBox."+iface+"Impl'\n"
156 #str += " _reg_desc_ = 'Generated callback implementation class'\n"
157 #str += " _reg_policy_spec_ = 'win32com.server.policy.EventHandlerPolicy'\n"
158
159 str += " def __init__(self): BaseClass.__init__(self, arg)\n"
160 str += "result = win32com.server.util.wrap("+iface+"Impl())\n"
161 exec (str,d,d)
162 return d['result']
163
164 def waitForEvents(self, timeout):
165 from win32file import CloseHandle
166 from win32con import DUPLICATE_SAME_ACCESS
167 from win32api import GetCurrentThread,DuplicateHandle,GetCurrentProcess
168 from win32event import MsgWaitForMultipleObjects, \
169 QS_ALLINPUT, WAIT_TIMEOUT, WAIT_OBJECT_0
170 from pythoncom import PumpWaitingMessages
171
172 pid = GetCurrentProcess()
173 handle = DuplicateHandle(pid, GetCurrentThread(), pid, 0, 0, DUPLICATE_SAME_ACCESS)
174
175 handles = []
176 handles.append(handle)
177
178 rc = MsgWaitForMultipleObjects(handles, 0, timeout, QS_ALLINPUT)
179 if rc >= WAIT_OBJECT_0 and rc < WAIT_OBJECT_0+len(handles):
180 # is it possible?
181 print "how come?"
182 pass
183 elif rc==WAIT_OBJECT_0 + len(handles):
184 # Waiting messages
185 PumpWaitingMessages()
186 else:
187 pass
188 CloseHandle(handle)
189
190 def deinit(self):
191 import pythoncom
192 pythoncom.CoUninitialize()
193 pass
194
195class PlatformXPCOM:
196 def __init__(self, params):
197 sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
198 import xpcom.vboxxpcom
199 import xpcom
200 import xpcom.components
201
202 def getSessionObject(self, vbox):
203 import xpcom.components
204 return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
205
206 def getVirtualBox(self):
207 import xpcom.components
208 return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
209
210 def getConstants(self):
211 import xpcom.components
212 return xpcom.components.interfaces
213
214 def getType(self):
215 return 'XPCOM'
216
217 def getRemote(self):
218 return False
219
220 def getArray(self, obj, field):
221 return obj.__getattr__('get'+field.capitalize())()
222
223 def initPerThread(self):
224 pass
225
226 def deinitPerThread(self):
227 pass
228
229 def createCallback(self, iface, impl, arg):
230 d = {}
231 d['BaseClass'] = impl
232 d['arg'] = arg
233 str = ""
234 str += "import xpcom.components\n"
235 str += "class "+iface+"Impl(BaseClass):\n"
236 str += " _com_interfaces_ = xpcom.components.interfaces."+iface+"\n"
237 str += " def __init__(self): BaseClass.__init__(self, arg)\n"
238 str += "result = "+iface+"Impl()\n"
239 exec (str,d,d)
240 return d['result']
241
242 def waitForEvents(self, timeout):
243 import xpcom
244 xpcom._xpcom.WaitForEvents(timeout)
245
246 def deinit(self):
247 import xpcom
248 xpcom._xpcom.DeinitCOM()
249
250class PlatformWEBSERVICE:
251 def __init__(self, params):
252 sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
253 import VirtualBox_services
254 import VirtualBox_wrappers
255 from VirtualBox_wrappers import IWebsessionManager2
256 if params is not None:
257 self.user = params.get("user", "")
258 self.password = params.get("password", "")
259 self.url = params.get("url", "")
260 else:
261 self.user = ""
262 self.password = ""
263 self.url = None
264 self.wsmgr = IWebsessionManager2(self.url)
265
266 def getSessionObject(self, vbox):
267 return self.wsmgr.getSessionObject(vbox)
268
269 def getVirtualBox(self):
270 return self.wsmgr.logon(self.user, self.password)
271
272 def getConstants(self):
273 return None
274
275 def getType(self):
276 return 'WEBSERVICE'
277
278 def getRemote(self):
279 return True
280
281 def getArray(self, obj, field):
282 return obj.__getattr__(field)
283
284 def initPerThread(self):
285 pass
286
287 def deinitPerThread(self):
288 pass
289
290 def createCallback(self, iface, impl, arg):
291 raise Exception("no callbacks for webservices")
292
293 def waitForEvents(self, timeout):
294 # Webservices cannot do that
295 pass
296
297 def deinit(self):
298 # should we do something about it?
299 pass
300
301class SessionManager:
302 def __init__(self, mgr):
303 self.mgr = mgr
304
305 def getSessionObject(self, vbox):
306 return self.mgr.platform.getSessionObject(vbox)
307
308class VirtualBoxManager:
309 def __init__(self, style, platparams):
310 if style is None:
311 if sys.platform == 'win32':
312 style = "MSCOM"
313 else:
314 style = "XPCOM"
315 try:
316 exec "self.platform = Platform"+style+"(platparams)"
317 self.vbox = self.platform.getVirtualBox()
318 self.mgr = SessionManager(self)
319 self.constants = VirtualBoxReflectionInfo()
320 self.type = self.platform.getType()
321 self.remote = self.platform.getRemote()
322 except Exception,e:
323 print "init exception: ",e
324 traceback.print_exc()
325 raise e
326
327 def getArray(self, obj, field):
328 return self.platform.getArray(obj, field)
329
330 def getVirtualBox(self):
331 return self.platform.getVirtualBox()
332
333 def __del__(self):
334 deinit(self)
335
336 def deinit(self):
337 if hasattr(self, "vbox"):
338 del self.vbox
339 if hasattr(self, "platform"):
340 self.platform.deinit()
341
342 def initPerThread(self):
343 self.platform.initPerThread()
344
345 def openMachineSession(self, machineId):
346 session = self.mgr.getSessionObject(self.vbox)
347 self.vbox.openSession(session, machineId)
348 return session
349
350 def closeMachineSession(self, session):
351 session.close()
352
353 def deinitPerThread(self):
354 self.platform.deinitPerThread()
355
356 def createCallback(self, iface, impl, arg):
357 return self.platform.createCallback(iface, impl, arg)
358
359 def waitForEvents(self, timeout):
360 return self.platform.waitForEvents(timeout)
Note: See TracBrowser for help on using the repository browser.

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