VirtualBox

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

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

Python: installer draft

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 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.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#
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
30#os.environ["VBOX_SDK_PATH"] = VboxSdkDir
31sys.path.append(VboxBinDir)
32#sys.path.append(VboxSdkDir+"/bindings/glue/python")
33
34from VirtualBox_constants import VirtualBoxReflectionInfo
35
36class PlatformMSCOM:
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 print "ask",name
74 return win32com.client.constants.__getattr__(name)
75 except AttributeError,e:
76 fake = PlatformMSCOM.ConstantFake(self, attr)
77 consts[attr] = fake
78 return fake
79
80
81 class InterfacesWrapper:
82 def __init__(self):
83 self.__dict__['_rootFake'] = PlatformMSCOM.ConstantFake(None, None)
84
85 def __getattr__(self, a):
86 import win32com
87 from win32com.client import constants
88 if a.startswith("__"):
89 raise AttributeError
90 try:
91 return win32com.client.constants.__getattr__(a)
92 except AttributeError,e:
93 return self.__dict__['_rootFake'].__getattr__(a)
94
95 def __init__(self, params):
96 sys.path.append(VboxSdkDir+'/bindings/mscom/python/')
97 from win32com import universal
98 from win32com.client import gencache, DispatchWithEvents, Dispatch
99 from win32com.client import constants, getevents
100 import win32com.server.register
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
139class PlatformXPCOM:
140 def __init__(self, params):
141 sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
142 import xpcom.vboxxpcom
143 import xpcom
144 import xpcom.components
145
146 def getSessionObject(self, vbox):
147 import xpcom.components
148 return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
149
150 def getVirtualBox(self):
151 import xpcom.components
152 return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
153
154 def getConstants(self):
155 import xpcom.components
156 return xpcom.components.interfaces
157
158 def getType(self):
159 return 'XPCOM'
160
161 def getRemote(self):
162 return False
163
164 def getArray(self, obj, field):
165 return obj.__getattr__('get'+field.capitalize())()
166
167 def initPerThread(self):
168 pass
169
170 def deinitPerThread(self):
171 pass
172
173class PlatformWEBSERVICE:
174 def __init__(self, params):
175 sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
176 import VirtualBox_services
177 import VirtualBox_wrappers
178 from VirtualBox_wrappers import IWebsessionManager2
179 if params is not None:
180 self.user = params.get("user", "")
181 self.password = params.get("password", "")
182 self.url = params.get("url", "")
183 else:
184 self.user = ""
185 self.password = ""
186 self.url = None
187 self.wsmgr = IWebsessionManager2(self.url)
188
189 def getSessionObject(self, vbox):
190 return self.wsmgr.getSessionObject(vbox)
191
192 def getVirtualBox(self):
193 return self.wsmgr.logon(self.user, self.password)
194
195 def getConstants(self):
196 return None
197
198 def getType(self):
199 return 'WEBSERVICE'
200
201 def getRemote(self):
202 return True
203
204 def getArray(self, obj, field):
205 return obj.__getattr__(field)
206
207 def initPerThread(self):
208 pass
209
210 def deinitPerThread(self):
211 pass
212
213class SessionManager:
214 def __init__(self, mgr):
215 self.mgr = mgr
216
217 def getSessionObject(self, vbox):
218 return self.mgr.platform.getSessionObject(vbox)
219
220class VirtualBoxManager:
221 def __init__(self, style, platparams):
222 if style is None:
223 if sys.platform == 'win32':
224 style = "MSCOM"
225 else:
226 style = "XPCOM"
227 try:
228 exec "self.platform = Platform"+style+"(platparams)"
229 self.vbox = self.platform.getVirtualBox()
230 self.mgr = SessionManager(self)
231 self.constants = VirtualBoxReflectionInfo()
232 self.type = self.platform.getType()
233 self.remote = self.platform.getRemote()
234 except Exception,e:
235 print "init exception: ",e
236 traceback.print_exc()
237 raise e
238
239 def getArray(self, obj, field):
240 return self.platform.getArray(obj, field)
241
242 def getVirtualBox(self):
243 return self.platform.getVirtualBox()
244
245 def __del__(self):
246 if hasattr(self, "vbox"):
247 del self.vbox
248
249 def initPerThread(self):
250 self.platform.initPerThread()
251
252 def deinitPerThread(self):
253 self.platform.deinitPerThread()
Note: See TracBrowser for help on using the repository browser.

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