1 | #!/usr/bin/env python
|
---|
2 |
|
---|
3 | import sys,os
|
---|
4 | import traceback
|
---|
5 |
|
---|
6 | VboxBinDir = os.environ.get("VBOX_PROGRAM_PATH", None)
|
---|
7 | VboxSdkDir = os.environ.get("VBOX_SDK_PATH", None)
|
---|
8 |
|
---|
9 | if VboxBinDir is None:
|
---|
10 | # @todo: To be set by installer
|
---|
11 | VboxBinDir = "/home/nike/work/ws/out/linux.amd64/debug/bin/"
|
---|
12 |
|
---|
13 | if VboxSdkDir is None:
|
---|
14 | VboxSdkDir = VboxBinDir+"/sdk"
|
---|
15 |
|
---|
16 | os.environ["VBOX_PROGRAM_PATH"] = VboxBinDir
|
---|
17 | os.environ["VBOX_SDK_PATH"] = VboxSdkDir
|
---|
18 | sys.path.append(VboxBinDir)
|
---|
19 | sys.path.append(VboxSdkDir+"/bindings/glue/python")
|
---|
20 |
|
---|
21 | class PlatformMSCOM:
|
---|
22 | class ConstantFake:
|
---|
23 | def __init__(self, parent, name):
|
---|
24 | self.__dict__['_parent'] = parent
|
---|
25 | self.__dict__['_name'] = name
|
---|
26 | self.__dict__['_consts'] = {}
|
---|
27 | try:
|
---|
28 | self.__dict__['_depth']=parent.__dict__['_depth']+1
|
---|
29 | except:
|
---|
30 | self.__dict__['_depth']=0
|
---|
31 | if self.__dict__['_depth'] > 4:
|
---|
32 | raise AttributeError
|
---|
33 |
|
---|
34 | def __getattr__(self, attr):
|
---|
35 | if attr.startswith("__"):
|
---|
36 | raise AttributeError
|
---|
37 |
|
---|
38 | consts = self.__dict__['_consts']
|
---|
39 | fake = consts.get(attr, None)
|
---|
40 | if fake != None:
|
---|
41 | return fake
|
---|
42 | try:
|
---|
43 | name = makeFullName(self, attr)
|
---|
44 | return win32com.client.constants.__getattr__(name)
|
---|
45 | except AttributeError,e:
|
---|
46 | fake = ConstantFake(self, attr)
|
---|
47 | consts[attr] = fake
|
---|
48 | return fake
|
---|
49 |
|
---|
50 | class InterfacesWrapper:
|
---|
51 | def __init__(self):
|
---|
52 | self.__dict__['_rootFake'] = PlatformMSCOM.ConstantFake(None, None)
|
---|
53 |
|
---|
54 | def __getattr__(self, a):
|
---|
55 | if a.startswith("__"):
|
---|
56 | raise AttributeError
|
---|
57 | try:
|
---|
58 | return win32com.client.constants.__getattr__(a)
|
---|
59 | except AttributeError,e:
|
---|
60 | return self.__dict__['_rootFake'].__getattr__(a)
|
---|
61 |
|
---|
62 | def makeFullName(fake, attr):
|
---|
63 | name = fake._name
|
---|
64 | parent = fake._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 | return name
|
---|
75 |
|
---|
76 | def __init__(self, params):
|
---|
77 | sys.path.append(VboxSdkDir+'/bindings/mscom/python/')
|
---|
78 | from win32com import universal
|
---|
79 | from win32com.client import gencache, DispatchWithEvents, Dispatch
|
---|
80 | from win32com.client import constants, getevents
|
---|
81 | import win32com.server.register
|
---|
82 | import win32com
|
---|
83 | import pythoncom
|
---|
84 | import win32api
|
---|
85 | self.constants = PlatformMSCOM.InterfacesWrapper()
|
---|
86 | #win32com.client.gencache.EnsureDispatch('VirtualBox.Session')
|
---|
87 | #win32com.client.gencache.EnsureDispatch('VirtualBox.VirtualBox')
|
---|
88 |
|
---|
89 | def getSessionObject(self):
|
---|
90 | import win32com
|
---|
91 | from win32com.client import Dispatch
|
---|
92 | return win32com.client.Dispatch("VirtualBox.Session")
|
---|
93 |
|
---|
94 | def getVirtualBox(self):
|
---|
95 | import win32com
|
---|
96 | from win32com.client import Dispatch
|
---|
97 | return win32com.client.Dispatch("VirtualBox.VirtualBox")
|
---|
98 |
|
---|
99 | def getConstants(self):
|
---|
100 | return self.constants
|
---|
101 |
|
---|
102 | def getType(self):
|
---|
103 | return 'MSCOM'
|
---|
104 |
|
---|
105 | def getRemote(self):
|
---|
106 | return False
|
---|
107 |
|
---|
108 | def getArray(self, obj, field):
|
---|
109 | return obj.__getattr__(field)
|
---|
110 |
|
---|
111 |
|
---|
112 | class PlatformXPCOM:
|
---|
113 | def __init__(self, params):
|
---|
114 | sys.path.append(VboxSdkDir+'/bindings/xpcom/python/')
|
---|
115 | import xpcom.vboxxpcom
|
---|
116 | import xpcom
|
---|
117 | import xpcom.components
|
---|
118 |
|
---|
119 | def getSessionObject(self):
|
---|
120 | import xpcom.components
|
---|
121 | return xpcom.components.classes["@virtualbox.org/Session;1"].createInstance()
|
---|
122 |
|
---|
123 | def getVirtualBox(self):
|
---|
124 | import xpcom.components
|
---|
125 | return xpcom.components.classes["@virtualbox.org/VirtualBox;1"].createInstance()
|
---|
126 |
|
---|
127 | def getConstants(self):
|
---|
128 | import xpcom.components
|
---|
129 | return xpcom.components.interfaces
|
---|
130 |
|
---|
131 | def getType(self):
|
---|
132 | return 'XPCOM'
|
---|
133 |
|
---|
134 | def getRemote(self):
|
---|
135 | return False
|
---|
136 |
|
---|
137 | def getArray(self, obj, field):
|
---|
138 | return obj.__getattr__('get'+field.capitalize())()
|
---|
139 |
|
---|
140 | class PlatformWEBSERVICE:
|
---|
141 | def __init__(self, params):
|
---|
142 | sys.path.append(VboxSdkDir+'/bindings/webservice/python/lib')
|
---|
143 | import VirtualBox_services
|
---|
144 | import VirtualBox_wrappers
|
---|
145 | from VirtualBox_wrappers import IWebsessionManager
|
---|
146 | from VirtualBox_wrappers import g_port
|
---|
147 | from VirtualBox_wrappers import g_reflectionInfo
|
---|
148 | self.wsmgr = IWebsessionManager()
|
---|
149 | self.port = g_port
|
---|
150 | self.constants = g_reflectionInfo
|
---|
151 | self.user = ""
|
---|
152 | self.password = ""
|
---|
153 |
|
---|
154 | def getSessionObject(self):
|
---|
155 | return self.wsmgr.getSessionObject()
|
---|
156 |
|
---|
157 | def getVirtualBox(self):
|
---|
158 | return self.wsmgr.logon(self.user, self.password)
|
---|
159 |
|
---|
160 | def getConstants(self):
|
---|
161 | from VirtualBox_wrappers import g_reflectionInfo
|
---|
162 | return g_reflectionInfo
|
---|
163 |
|
---|
164 | def getType(self):
|
---|
165 | return 'WEBSERVICE'
|
---|
166 |
|
---|
167 | def getRemote(self):
|
---|
168 | return True
|
---|
169 |
|
---|
170 | def getArray(self, obj, field):
|
---|
171 | return obj.__getattr__(field)
|
---|
172 |
|
---|
173 |
|
---|
174 | class SessionManager:
|
---|
175 | def __init__(self, mgr):
|
---|
176 | self.mgr = mgr
|
---|
177 |
|
---|
178 | def getSessionObject(self, vbox):
|
---|
179 | return self.mgr.platform.getSessionObject()
|
---|
180 |
|
---|
181 | class VirtualBoxManager:
|
---|
182 | def __init__(self, style, platparams):
|
---|
183 | if style is None:
|
---|
184 | if sys.platform == 'win32':
|
---|
185 | style = "MSCOM"
|
---|
186 | else:
|
---|
187 | style = "XPCOM"
|
---|
188 | try:
|
---|
189 | exec "self.platform = Platform"+style+"(platparams)"
|
---|
190 | self.vbox = self.platform.getVirtualBox()
|
---|
191 | self.mgr = SessionManager(self)
|
---|
192 | self.constants = self.platform.getConstants()
|
---|
193 | self.type = self.platform.getType()
|
---|
194 | self.remote = self.platform.getRemote()
|
---|
195 | except Exception,e:
|
---|
196 | print "init exception: ",e
|
---|
197 | traceback.print_exc()
|
---|
198 | raise e
|
---|
199 |
|
---|
200 | def getArray(self, obj, field):
|
---|
201 | return self.platform.getArray(obj, field)
|
---|
202 |
|
---|
203 | def getVirtualBox(self):
|
---|
204 | return self.platform.getVirtualBox()
|
---|
205 |
|
---|
206 | def __del__(self):
|
---|
207 | if hasattr(self, "vbox"):
|
---|
208 | del self.vbox
|
---|