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.virtualbox.org. 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 | #
|
---|
16 | import sys,os
|
---|
17 | import traceback
|
---|
18 |
|
---|
19 | VboxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
|
---|
20 | VboxSdkDir = os.environ.get("VBOX_SDK_PATH", None)
|
---|
21 |
|
---|
22 | if VboxBinDir is None:
|
---|
23 | # Will be set by the installer
|
---|
24 | VboxBinDir = "%VBOX_INSTALL_PATH%"
|
---|
25 |
|
---|
26 | if VboxSdkDir is None:
|
---|
27 | VboxSdkDir = VboxBinDir+"/sdk"
|
---|
28 |
|
---|
29 | os.environ["VBOX_PROGRAM_PATH"] = VboxBinDir
|
---|
30 | os.environ["VBOX_SDK_PATH"] = VboxSdkDir
|
---|
31 | sys.path.append(VboxBinDir)
|
---|
32 |
|
---|
33 | from VirtualBox_constants import VirtualBoxReflectionInfo
|
---|
34 |
|
---|
35 | class 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 |
|
---|
147 | def waitForEvents(self, timeout):
|
---|
148 | # not really supported yet
|
---|
149 | pass
|
---|
150 |
|
---|
151 | def deinit(self):
|
---|
152 | import pythoncom
|
---|
153 | pythoncom.CoUninitialize()
|
---|
154 | pass
|
---|
155 |
|
---|
156 | class PlatformXPCOM:
|
---|
157 | def __init__(self, params):
|
---|
158 | sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
|
---|
159 | import xpcom.vboxxpcom
|
---|
160 | import xpcom
|
---|
161 | import xpcom.components
|
---|
162 |
|
---|
163 | def getSessionObject(self, vbox):
|
---|
164 | import xpcom.components
|
---|
165 | return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
|
---|
166 |
|
---|
167 | def getVirtualBox(self):
|
---|
168 | import xpcom.components
|
---|
169 | return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
|
---|
170 |
|
---|
171 | def getConstants(self):
|
---|
172 | import xpcom.components
|
---|
173 | return xpcom.components.interfaces
|
---|
174 |
|
---|
175 | def getType(self):
|
---|
176 | return 'XPCOM'
|
---|
177 |
|
---|
178 | def getRemote(self):
|
---|
179 | return False
|
---|
180 |
|
---|
181 | def getArray(self, obj, field):
|
---|
182 | return obj.__getattr__('get'+field.capitalize())()
|
---|
183 |
|
---|
184 | def initPerThread(self):
|
---|
185 | pass
|
---|
186 |
|
---|
187 | def deinitPerThread(self):
|
---|
188 | pass
|
---|
189 |
|
---|
190 | def createCallback(self, iface, impl, arg):
|
---|
191 | d = {}
|
---|
192 | d['BaseClass'] = impl
|
---|
193 | d['arg'] = arg
|
---|
194 | str = ""
|
---|
195 | str += "import xpcom.components\n"
|
---|
196 | str += "class "+iface+"Impl(BaseClass):\n"
|
---|
197 | str += " _com_interfaces_ = xpcom.components.interfaces."+iface+"\n"
|
---|
198 | str += " def __init__(self): BaseClass.__init__(self, arg)\n"
|
---|
199 | str += "result = "+iface+"Impl()\n"
|
---|
200 | exec (str,d,d)
|
---|
201 | return d['result']
|
---|
202 |
|
---|
203 | def waitForEvents(self, timeout):
|
---|
204 | import xpcom
|
---|
205 | xpcom._xpcom.WaitForEvents(timeout)
|
---|
206 |
|
---|
207 | def deinit(self):
|
---|
208 | import xpcom
|
---|
209 | xpcom._xpcom.DeinitCOM()
|
---|
210 |
|
---|
211 | class PlatformWEBSERVICE:
|
---|
212 | def __init__(self, params):
|
---|
213 | sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
|
---|
214 | import VirtualBox_services
|
---|
215 | import VirtualBox_wrappers
|
---|
216 | from VirtualBox_wrappers import IWebsessionManager2
|
---|
217 | if params is not None:
|
---|
218 | self.user = params.get("user", "")
|
---|
219 | self.password = params.get("password", "")
|
---|
220 | self.url = params.get("url", "")
|
---|
221 | else:
|
---|
222 | self.user = ""
|
---|
223 | self.password = ""
|
---|
224 | self.url = None
|
---|
225 | self.wsmgr = IWebsessionManager2(self.url)
|
---|
226 |
|
---|
227 | def getSessionObject(self, vbox):
|
---|
228 | return self.wsmgr.getSessionObject(vbox)
|
---|
229 |
|
---|
230 | def getVirtualBox(self):
|
---|
231 | return self.wsmgr.logon(self.user, self.password)
|
---|
232 |
|
---|
233 | def getConstants(self):
|
---|
234 | return None
|
---|
235 |
|
---|
236 | def getType(self):
|
---|
237 | return 'WEBSERVICE'
|
---|
238 |
|
---|
239 | def getRemote(self):
|
---|
240 | return True
|
---|
241 |
|
---|
242 | def getArray(self, obj, field):
|
---|
243 | return obj.__getattr__(field)
|
---|
244 |
|
---|
245 | def initPerThread(self):
|
---|
246 | pass
|
---|
247 |
|
---|
248 | def deinitPerThread(self):
|
---|
249 | pass
|
---|
250 |
|
---|
251 | def createCallback(self, iface, impl, arg):
|
---|
252 | raise Exception("no callbacks for webservices")
|
---|
253 |
|
---|
254 | def waitForEvents(self, timeout):
|
---|
255 | # Webservices cannot do that
|
---|
256 | pass
|
---|
257 |
|
---|
258 | def deinit(self):
|
---|
259 | # should we do something about it?
|
---|
260 | pass
|
---|
261 |
|
---|
262 | class SessionManager:
|
---|
263 | def __init__(self, mgr):
|
---|
264 | self.mgr = mgr
|
---|
265 |
|
---|
266 | def getSessionObject(self, vbox):
|
---|
267 | return self.mgr.platform.getSessionObject(vbox)
|
---|
268 |
|
---|
269 | class VirtualBoxManager:
|
---|
270 | def __init__(self, style, platparams):
|
---|
271 | if style is None:
|
---|
272 | if sys.platform == 'win32':
|
---|
273 | style = "MSCOM"
|
---|
274 | else:
|
---|
275 | style = "XPCOM"
|
---|
276 | try:
|
---|
277 | exec "self.platform = Platform"+style+"(platparams)"
|
---|
278 | self.vbox = self.platform.getVirtualBox()
|
---|
279 | self.mgr = SessionManager(self)
|
---|
280 | self.constants = VirtualBoxReflectionInfo()
|
---|
281 | self.type = self.platform.getType()
|
---|
282 | self.remote = self.platform.getRemote()
|
---|
283 | except Exception,e:
|
---|
284 | print "init exception: ",e
|
---|
285 | traceback.print_exc()
|
---|
286 | raise e
|
---|
287 |
|
---|
288 | def getArray(self, obj, field):
|
---|
289 | return self.platform.getArray(obj, field)
|
---|
290 |
|
---|
291 | def getVirtualBox(self):
|
---|
292 | return self.platform.getVirtualBox()
|
---|
293 |
|
---|
294 | def __del__(self):
|
---|
295 | deinit(self)
|
---|
296 |
|
---|
297 | def deinit(self):
|
---|
298 | if hasattr(self, "vbox"):
|
---|
299 | del self.vbox
|
---|
300 | if hasattr(self, "platform"):
|
---|
301 | self.platform.deinit()
|
---|
302 |
|
---|
303 | def initPerThread(self):
|
---|
304 | self.platform.initPerThread()
|
---|
305 |
|
---|
306 | def openMachineSession(self, machineId):
|
---|
307 | session = self.mgr.getSessionObject(self.vbox)
|
---|
308 | self.vbox.openSession(session, machineId)
|
---|
309 | return session
|
---|
310 |
|
---|
311 | def closeMachineSession(self, session):
|
---|
312 | session.close()
|
---|
313 |
|
---|
314 | def deinitPerThread(self):
|
---|
315 | self.platform.deinitPerThread()
|
---|
316 |
|
---|
317 | def createCallback(self, iface, impl, arg):
|
---|
318 | return self.platform.createCallback(iface, impl, arg)
|
---|
319 |
|
---|
320 | def waitForEvents(self, timeout):
|
---|
321 | return self.platform.waitForEvents(timeout)
|
---|