VirtualBox

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

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

Main: implmented waitForEvents(aTimeout) API for XPCOM targets, added command to VBox shell using this API

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 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 def __init__(self, params):
95 from win32com import universal
96 from win32com.client import gencache, DispatchWithEvents, Dispatch
97 from win32com.client import constants, getevents
98 import win32com
99 import pythoncom
100 import win32api
101 self.constants = PlatformMSCOM.InterfacesWrapper()
102
103 def getSessionObject(self, vbox):
104 import win32com
105 from win32com.client import Dispatch
106 return win32com.client.Dispatch("VirtualBox.Session")
107
108 def getVirtualBox(self):
109 import win32com
110 from win32com.client import Dispatch
111 return win32com.client.Dispatch("VirtualBox.VirtualBox")
112
113 def getConstants(self):
114 return self.constants
115
116 def getType(self):
117 return 'MSCOM'
118
119 def getRemote(self):
120 return False
121
122 def getArray(self, obj, field):
123 return obj.__getattr__(field)
124
125 def initPerThread(self):
126 import pythoncom
127 pythoncom.CoInitializeEx(0)
128
129 def deinitPerThread(self):
130 import pythoncom
131 pythoncom.CoUninitialize()
132
133 def createCallback(self, iface, impl, arg):
134 d = {}
135 d['BaseClass'] = impl
136 d['arg'] = arg
137 str = ""
138 str += "import win32com.server.util"
139 str += "class "+iface+"Impl(BaseClass):\n"
140 str += " _com_interfaces_ = ['"+iface+"']\n"
141 str += " _typelib_guid_ = '{46137EEC-703B-4FE5-AFD4-7C9BBBBA0259}'\n"
142 str += " def __init__(self): BaseClass.__init__(self, arg)\n"
143 str += "result = win32com.server.util.wrap("+iface+"Impl())\n"
144 exec (str,d,d)
145 return d['result']
146
147class PlatformXPCOM:
148 def __init__(self, params):
149 sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
150 import xpcom.vboxxpcom
151 import xpcom
152 import xpcom.components
153
154 def getSessionObject(self, vbox):
155 import xpcom.components
156 return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
157
158 def getVirtualBox(self):
159 import xpcom.components
160 return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
161
162 def getConstants(self):
163 import xpcom.components
164 return xpcom.components.interfaces
165
166 def getType(self):
167 return 'XPCOM'
168
169 def getRemote(self):
170 return False
171
172 def getArray(self, obj, field):
173 return obj.__getattr__('get'+field.capitalize())()
174
175 def initPerThread(self):
176 pass
177
178 def deinitPerThread(self):
179 pass
180
181 def createCallback(self, iface, impl, arg):
182 d = {}
183 d['BaseClass'] = impl
184 d['arg'] = arg
185 str = ""
186 str += "import xpcom.components\n"
187 str += "class "+iface+"Impl(BaseClass):\n"
188 str += " _com_interfaces_ = xpcom.components.interfaces."+iface+"\n"
189 str += " def __init__(self): BaseClass.__init__(self, arg)\n"
190 str += "result = "+iface+"Impl()\n"
191 exec (str,d,d)
192 return d['result']
193
194
195class PlatformWEBSERVICE:
196 def __init__(self, params):
197 sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
198 import VirtualBox_services
199 import VirtualBox_wrappers
200 from VirtualBox_wrappers import IWebsessionManager2
201 if params is not None:
202 self.user = params.get("user", "")
203 self.password = params.get("password", "")
204 self.url = params.get("url", "")
205 else:
206 self.user = ""
207 self.password = ""
208 self.url = None
209 self.wsmgr = IWebsessionManager2(self.url)
210
211 def getSessionObject(self, vbox):
212 return self.wsmgr.getSessionObject(vbox)
213
214 def getVirtualBox(self):
215 return self.wsmgr.logon(self.user, self.password)
216
217 def getConstants(self):
218 return None
219
220 def getType(self):
221 return 'WEBSERVICE'
222
223 def getRemote(self):
224 return True
225
226 def getArray(self, obj, field):
227 return obj.__getattr__(field)
228
229 def initPerThread(self):
230 pass
231
232 def deinitPerThread(self):
233 pass
234
235 def createCallback(self, iface, impl, arg):
236 raise Exception("no callbacks for webservices")
237
238class SessionManager:
239 def __init__(self, mgr):
240 self.mgr = mgr
241
242 def getSessionObject(self, vbox):
243 return self.mgr.platform.getSessionObject(vbox)
244
245class VirtualBoxManager:
246 def __init__(self, style, platparams):
247 if style is None:
248 if sys.platform == 'win32':
249 style = "MSCOM"
250 else:
251 style = "XPCOM"
252 try:
253 exec "self.platform = Platform"+style+"(platparams)"
254 self.vbox = self.platform.getVirtualBox()
255 self.mgr = SessionManager(self)
256 self.constants = VirtualBoxReflectionInfo()
257 self.type = self.platform.getType()
258 self.remote = self.platform.getRemote()
259 except Exception,e:
260 print "init exception: ",e
261 traceback.print_exc()
262 raise e
263
264 def getArray(self, obj, field):
265 return self.platform.getArray(obj, field)
266
267 def getVirtualBox(self):
268 return self.platform.getVirtualBox()
269
270 def __del__(self):
271 if hasattr(self, "vbox"):
272 del self.vbox
273
274 def initPerThread(self):
275 self.platform.initPerThread()
276
277 def openMachineSession(self, machineId):
278 session = self.mgr.getSessionObject(self.vbox)
279 self.vbox.openSession(session, machineId)
280 return session
281
282 def closeMachineSession(self, session):
283 session.close()
284
285 def deinitPerThread(self):
286 self.platform.deinitPerThread()
287
288 def createCallback(self, iface, impl, arg):
289 return self.platform.createCallback(iface, impl, arg)
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