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