VirtualBox

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

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

Python: ship and install bindings with binary install on Linux

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 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
30os.environ["VBOX_SDK_PATH"] = VboxSdkDir
31sys.path.append(VboxBinDir)
32# This directory's content goes to the site-wide directory
33#sys.path.append(VboxSdkDir+"/bindings/glue/python")
34
35from VirtualBox_constants import VirtualBoxReflectionInfo
36
37class 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.server.register
102 import win32com
103 import pythoncom
104 import win32api
105 self.constants = PlatformMSCOM.InterfacesWrapper()
106 #win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
107 #win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBox')
108
109 def getSessionObject(self, vbox):
110 import win32com
111 from win32com.client import Dispatch
112 return win32com.client.Dispatch("VirtualBox.Session")
113
114 def getVirtualBox(self):
115 import win32com
116 from win32com.client import Dispatch
117 return win32com.client.Dispatch("VirtualBox.VirtualBox")
118
119 def getConstants(self):
120 return self.constants
121
122 def getType(self):
123 return 'MSCOM'
124
125 def getRemote(self):
126 return False
127
128 def getArray(self, obj, field):
129 return obj.__getattr__(field)
130
131 def initPerThread(self):
132 import pythoncom
133 pythoncom.CoInitializeEx(0)
134
135 def deinitPerThread(self):
136 import pythoncom
137 pythoncom.CoUninitialize()
138
139
140class PlatformXPCOM:
141 def __init__(self, params):
142 sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
143 import xpcom.vboxxpcom
144 import xpcom
145 import xpcom.components
146
147 def getSessionObject(self, vbox):
148 import xpcom.components
149 return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
150
151 def getVirtualBox(self):
152 import xpcom.components
153 return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
154
155 def getConstants(self):
156 import xpcom.components
157 return xpcom.components.interfaces
158
159 def getType(self):
160 return 'XPCOM'
161
162 def getRemote(self):
163 return False
164
165 def getArray(self, obj, field):
166 return obj.__getattr__('get'+field.capitalize())()
167
168 def initPerThread(self):
169 pass
170
171 def deinitPerThread(self):
172 pass
173
174class PlatformWEBSERVICE:
175 def __init__(self, params):
176 sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
177 import VirtualBox_services
178 import VirtualBox_wrappers
179 from VirtualBox_wrappers import IWebsessionManager2
180 if params is not None:
181 self.user = params.get("user", "")
182 self.password = params.get("password", "")
183 self.url = params.get("url", "")
184 else:
185 self.user = ""
186 self.password = ""
187 self.url = None
188 self.wsmgr = IWebsessionManager2(self.url)
189
190 def getSessionObject(self, vbox):
191 return self.wsmgr.getSessionObject(vbox)
192
193 def getVirtualBox(self):
194 return self.wsmgr.logon(self.user, self.password)
195
196 def getConstants(self):
197 return None
198
199 def getType(self):
200 return 'WEBSERVICE'
201
202 def getRemote(self):
203 return True
204
205 def getArray(self, obj, field):
206 return obj.__getattr__(field)
207
208 def initPerThread(self):
209 pass
210
211 def deinitPerThread(self):
212 pass
213
214class SessionManager:
215 def __init__(self, mgr):
216 self.mgr = mgr
217
218 def getSessionObject(self, vbox):
219 return self.mgr.platform.getSessionObject(vbox)
220
221class VirtualBoxManager:
222 def __init__(self, style, platparams):
223 if style is None:
224 if sys.platform == 'win32':
225 style = "MSCOM"
226 else:
227 style = "XPCOM"
228 try:
229 exec "self.platform = Platform"+style+"(platparams)"
230 self.vbox = self.platform.getVirtualBox()
231 self.mgr = SessionManager(self)
232 self.constants = VirtualBoxReflectionInfo()
233 self.type = self.platform.getType()
234 self.remote = self.platform.getRemote()
235 except Exception,e:
236 print "init exception: ",e
237 traceback.print_exc()
238 raise e
239
240 def getArray(self, obj, field):
241 return self.platform.getArray(obj, field)
242
243 def getVirtualBox(self):
244 return self.platform.getVirtualBox()
245
246 def __del__(self):
247 if hasattr(self, "vbox"):
248 del self.vbox
249
250 def initPerThread(self):
251 self.platform.initPerThread()
252
253 def openMachineSession(self, machineId):
254 session = self.mgr.getSessionObject(self.vbox)
255 self.vbox.openSession(session, machineId)
256 return session
257
258 def closeMachineSession(self, session):
259 session.close()
260
261 def deinitPerThread(self):
262 self.platform.deinitPerThread()
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