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 | # This directory's content goes to the site-wide directory
|
---|
33 | #sys.path.append(VboxSdkDir+"/bindings/glue/python")
|
---|
34 |
|
---|
35 | from VirtualBox_constants import VirtualBoxReflectionInfo
|
---|
36 |
|
---|
37 | class PlatformMSCOM:
|
---|
38 | class ConstantFake:
|
---|
39 | def __init__(self, parent, name):
|
---|
40 | self.__dict__['_parent'] = parent
|
---|
41 | self.__dict__['_name'] = name
|
---|
42 | self.__dict__['_consts'] = {}
|
---|
43 | try:
|
---|
44 | self.__dict__['_depth']=parent.__dict__['_depth']+1
|
---|
45 | except:
|
---|
46 | self.__dict__['_depth']=0
|
---|
47 | if self.__dict__['_depth'] > 4:
|
---|
48 | raise AttributeError
|
---|
49 |
|
---|
50 | def __getattr__(self, attr):
|
---|
51 | import win32com
|
---|
52 | from win32com.client import constants
|
---|
53 |
|
---|
54 | if attr.startswith("__"):
|
---|
55 | raise AttributeError
|
---|
56 |
|
---|
57 | consts = self.__dict__['_consts']
|
---|
58 |
|
---|
59 | fake = consts.get(attr, None)
|
---|
60 | if fake != None:
|
---|
61 | return fake
|
---|
62 | try:
|
---|
63 | name = self.__dict__['_name']
|
---|
64 | parent = self.__dict__['_parent']
|
---|
65 | while parent != None:
|
---|
66 | if parent._name is not None:
|
---|
67 | name = parent._name+'_'+name
|
---|
68 | parent = parent._parent
|
---|
69 |
|
---|
70 | if name is not None:
|
---|
71 | name += "_" + attr
|
---|
72 | else:
|
---|
73 | name = attr
|
---|
74 | print "ask",name
|
---|
75 | return win32com.client.constants.__getattr__(name)
|
---|
76 | except AttributeError,e:
|
---|
77 | fake = PlatformMSCOM.ConstantFake(self, attr)
|
---|
78 | consts[attr] = fake
|
---|
79 | return fake
|
---|
80 |
|
---|
81 |
|
---|
82 | class InterfacesWrapper:
|
---|
83 | def __init__(self):
|
---|
84 | self.__dict__['_rootFake'] = PlatformMSCOM.ConstantFake(None, None)
|
---|
85 |
|
---|
86 | def __getattr__(self, a):
|
---|
87 | import win32com
|
---|
88 | from win32com.client import constants
|
---|
89 | if a.startswith("__"):
|
---|
90 | raise AttributeError
|
---|
91 | try:
|
---|
92 | return win32com.client.constants.__getattr__(a)
|
---|
93 | except AttributeError,e:
|
---|
94 | return self.__dict__['_rootFake'].__getattr__(a)
|
---|
95 |
|
---|
96 | def __init__(self, params):
|
---|
97 | sys.path.append(VboxSdkDir+'/bindings/mscom/python/')
|
---|
98 | from win32com import universal
|
---|
99 | from win32com.client import gencache, DispatchWithEvents, Dispatch
|
---|
100 | from win32com.client import constants, getevents
|
---|
101 | import win32com
|
---|
102 | import pythoncom
|
---|
103 | import win32api
|
---|
104 | self.constants = PlatformMSCOM.InterfacesWrapper()
|
---|
105 | #win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
|
---|
106 | #win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBox')
|
---|
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):
|
---|
139 | d = {}
|
---|
140 | d['BaseClass'] = impl
|
---|
141 | str = ""
|
---|
142 | str += "import win32com.server.util"
|
---|
143 | str += "class "+iface+"Impl(BaseClass):\n"
|
---|
144 | str += " _com_interfaces_ = ['"+iface+"']\n"
|
---|
145 | str += " _typelib_guid_ = '{46137EEC-703B-4FE5-AFD4-7C9BBBBA0259}'\n"
|
---|
146 | str += " def __init__(self): pass\n"
|
---|
147 | str += "result = win32com.server.util.wrap("+iface+"Impl())\n"
|
---|
148 | exec (str,d,d)
|
---|
149 | return d['result']
|
---|
150 |
|
---|
151 | class PlatformXPCOM:
|
---|
152 | def __init__(self, params):
|
---|
153 | sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
|
---|
154 | import xpcom.vboxxpcom
|
---|
155 | import xpcom
|
---|
156 | import xpcom.components
|
---|
157 |
|
---|
158 | def getSessionObject(self, vbox):
|
---|
159 | import xpcom.components
|
---|
160 | return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
|
---|
161 |
|
---|
162 | def getVirtualBox(self):
|
---|
163 | import xpcom.components
|
---|
164 | return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
|
---|
165 |
|
---|
166 | def getConstants(self):
|
---|
167 | import xpcom.components
|
---|
168 | return xpcom.components.interfaces
|
---|
169 |
|
---|
170 | def getType(self):
|
---|
171 | return 'XPCOM'
|
---|
172 |
|
---|
173 | def getRemote(self):
|
---|
174 | return False
|
---|
175 |
|
---|
176 | def getArray(self, obj, field):
|
---|
177 | return obj.__getattr__('get'+field.capitalize())()
|
---|
178 |
|
---|
179 | def initPerThread(self):
|
---|
180 | pass
|
---|
181 |
|
---|
182 | def deinitPerThread(self):
|
---|
183 | pass
|
---|
184 |
|
---|
185 | def createCallback(self, iface, impl):
|
---|
186 | d = {}
|
---|
187 | d['BaseClass'] = impl
|
---|
188 | str = ""
|
---|
189 | str += "import xpcom.components\n"
|
---|
190 | str += "class "+iface+"Impl(BaseClass):\n"
|
---|
191 | str += " _com_interfaces_ = xpcom.components.interfaces."+iface+"\n"
|
---|
192 | str += " def __init__(self): pass\n"
|
---|
193 | str += "result = "+iface+"Impl()\n"
|
---|
194 | #print str
|
---|
195 | exec (str,d,d)
|
---|
196 | return d['result']
|
---|
197 |
|
---|
198 |
|
---|
199 | class PlatformWEBSERVICE:
|
---|
200 | def __init__(self, params):
|
---|
201 | sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
|
---|
202 | import VirtualBox_services
|
---|
203 | import VirtualBox_wrappers
|
---|
204 | from VirtualBox_wrappers import IWebsessionManager2
|
---|
205 | if params is not None:
|
---|
206 | self.user = params.get("user", "")
|
---|
207 | self.password = params.get("password", "")
|
---|
208 | self.url = params.get("url", "")
|
---|
209 | else:
|
---|
210 | self.user = ""
|
---|
211 | self.password = ""
|
---|
212 | self.url = None
|
---|
213 | self.wsmgr = IWebsessionManager2(self.url)
|
---|
214 |
|
---|
215 | def getSessionObject(self, vbox):
|
---|
216 | return self.wsmgr.getSessionObject(vbox)
|
---|
217 |
|
---|
218 | def getVirtualBox(self):
|
---|
219 | return self.wsmgr.logon(self.user, self.password)
|
---|
220 |
|
---|
221 | def getConstants(self):
|
---|
222 | return None
|
---|
223 |
|
---|
224 | def getType(self):
|
---|
225 | return 'WEBSERVICE'
|
---|
226 |
|
---|
227 | def getRemote(self):
|
---|
228 | return True
|
---|
229 |
|
---|
230 | def getArray(self, obj, field):
|
---|
231 | return obj.__getattr__(field)
|
---|
232 |
|
---|
233 | def initPerThread(self):
|
---|
234 | pass
|
---|
235 |
|
---|
236 | def deinitPerThread(self):
|
---|
237 | pass
|
---|
238 |
|
---|
239 | def createCallback(self, iface, impl):
|
---|
240 | raise Exception("no callbacks for webservices")
|
---|
241 |
|
---|
242 | class SessionManager:
|
---|
243 | def __init__(self, mgr):
|
---|
244 | self.mgr = mgr
|
---|
245 |
|
---|
246 | def getSessionObject(self, vbox):
|
---|
247 | return self.mgr.platform.getSessionObject(vbox)
|
---|
248 |
|
---|
249 | class VirtualBoxManager:
|
---|
250 | def __init__(self, style, platparams):
|
---|
251 | if style is None:
|
---|
252 | if sys.platform == 'win32':
|
---|
253 | style = "MSCOM"
|
---|
254 | else:
|
---|
255 | style = "XPCOM"
|
---|
256 | try:
|
---|
257 | exec "self.platform = Platform"+style+"(platparams)"
|
---|
258 | self.vbox = self.platform.getVirtualBox()
|
---|
259 | self.mgr = SessionManager(self)
|
---|
260 | self.constants = VirtualBoxReflectionInfo()
|
---|
261 | self.type = self.platform.getType()
|
---|
262 | self.remote = self.platform.getRemote()
|
---|
263 | except Exception,e:
|
---|
264 | print "init exception: ",e
|
---|
265 | traceback.print_exc()
|
---|
266 | raise e
|
---|
267 |
|
---|
268 | def getArray(self, obj, field):
|
---|
269 | return self.platform.getArray(obj, field)
|
---|
270 |
|
---|
271 | def getVirtualBox(self):
|
---|
272 | return self.platform.getVirtualBox()
|
---|
273 |
|
---|
274 | def __del__(self):
|
---|
275 | if hasattr(self, "vbox"):
|
---|
276 | del self.vbox
|
---|
277 |
|
---|
278 | def initPerThread(self):
|
---|
279 | self.platform.initPerThread()
|
---|
280 |
|
---|
281 | def openMachineSession(self, machineId):
|
---|
282 | session = self.mgr.getSessionObject(self.vbox)
|
---|
283 | self.vbox.openSession(session, machineId)
|
---|
284 | return session
|
---|
285 |
|
---|
286 | def closeMachineSession(self, session):
|
---|
287 | session.close()
|
---|
288 |
|
---|
289 | def deinitPerThread(self):
|
---|
290 | self.platform.deinitPerThread()
|
---|
291 |
|
---|
292 | def createCallback(self, iface, impl):
|
---|
293 | return self.platform.createCallback(iface, impl)
|
---|