2 # Manually written part of python bindings for libvirt
5 # On cygwin, the DLL is called cygvirtmod.dll
11 lib_e
= sys
.exc_info()[1]
13 import cygvirtmod
as libvirtmod
15 cyg_e
= sys
.exc_info()[1]
16 if str(cyg_e
).count("No module named"):
20 # The root of all libvirt errors.
21 class libvirtError(Exception):
22 def __init__(self
, defmsg
, conn
=None, dom
=None, net
=None, pool
=None, vol
=None):
24 # Never call virConnGetLastError().
25 # virGetLastError() is now thread local
26 err
= libvirtmod
.virGetLastError()
32 Exception.__init
__(self
, msg
)
36 def get_error_code(self
):
41 def get_error_domain(self
):
46 def get_error_message(self
):
51 def get_error_level(self
):
82 # register the libvirt global error handler
84 def registerErrorHandler(f
, ctx
):
85 """Register a Python function for error reporting.
86 The function is called back as f(ctx, error), with error
87 being a list of information about the error being raised.
88 Returns 1 in case of success."""
89 return libvirtmod
.virRegisterErrorHandler(f
,ctx
)
91 def openAuth(uri
, auth
, flags
=0):
92 ret
= libvirtmod
.virConnectOpenAuth(uri
, auth
, flags
)
93 if ret
is None:raise libvirtError('virConnectOpenAuth() failed')
94 return virConnect(_obj
=ret
)
98 # Return library version.
100 def getVersion (name
= None):
101 """If no name parameter is passed (or name is None) then the
102 version of the libvirt library is returned as an integer.
104 If a name is passed and it refers to a driver linked to the
105 libvirt library, then this returns a tuple of (library version,
108 If the name passed refers to a non-existent driver, then you
109 will get the exception 'no support for hypervisor'.
111 Versions numbers are integers: 1000000*major + 1000*minor + release."""
113 ret
= libvirtmod
.virGetVersion ()
115 ret
= libvirtmod
.virGetVersion (name
)
116 if ret
is None: raise libvirtError ("virGetVersion() failed")
121 # Invoke an EventHandle callback
123 def _eventInvokeHandleCallback(watch
, fd
, event
, opaque
, opaquecompat
=None):
125 Invoke the Event Impl Handle Callback in C
127 # libvirt 0.9.2 and earlier required custom event loops to know
128 # that opaque=(cb, original_opaque) and pass the values individually
129 # to this wrapper. This should handle the back compat case, and make
130 # future invocations match the virEventHandleCallback prototype
133 opaque
= opaquecompat
138 libvirtmod
.virEventInvokeHandleCallback(watch
, fd
, event
, callback
, opaque
)
141 # Invoke an EventTimeout callback
143 def _eventInvokeTimeoutCallback(timer
, opaque
, opaquecompat
=None):
145 Invoke the Event Impl Timeout Callback in C
147 # libvirt 0.9.2 and earlier required custom event loops to know
148 # that opaque=(cb, original_opaque) and pass the values individually
149 # to this wrapper. This should handle the back compat case, and make
150 # future invocations match the virEventTimeoutCallback prototype
153 opaque
= opaquecompat
158 libvirtmod
.virEventInvokeTimeoutCallback(timer
, callback
, opaque
)
160 def _dispatchEventHandleCallback(watch
, fd
, events
, cbData
):
162 opaque
= cbData
["opaque"]
164 cb(watch
, fd
, events
, opaque
)
167 def _dispatchEventTimeoutCallback(timer
, cbData
):
169 opaque
= cbData
["opaque"]
174 def virEventAddHandle(fd
, events
, cb
, opaque
):
176 register a callback for monitoring file handle events
178 @fd: file handle to monitor for events
179 @events: bitset of events to watch from virEventHandleType constants
180 @cb: callback to invoke when an event occurs
181 @opaque: user data to pass to callback
183 Example callback prototype is:
184 def cb(watch, # int id of the handle
185 fd, # int file descriptor the event occurred on
186 events, # int bitmap of events that have occurred
187 opaque): # opaque data passed to eventAddHandle
189 cbData
= {"cb" : cb
, "opaque" : opaque
}
190 ret
= libvirtmod
.virEventAddHandle(fd
, events
, cbData
)
191 if ret
== -1: raise libvirtError ('virEventAddHandle() failed')
194 def virEventAddTimeout(timeout
, cb
, opaque
):
196 register a callback for a timer event
198 @timeout: time between events in milliseconds
199 @cb: callback to invoke when an event occurs
200 @opaque: user data to pass to callback
202 Setting timeout to -1 will disable the timer. Setting the timeout
203 to zero will cause it to fire on every event loop iteration.
205 Example callback prototype is:
206 def cb(timer, # int id of the timer
207 opaque): # opaque data passed to eventAddTimeout
209 cbData
= {"cb" : cb
, "opaque" : opaque
}
210 ret
= libvirtmod
.virEventAddTimeout(timeout
, cbData
)
211 if ret
== -1: raise libvirtError ('virEventAddTimeout() failed')
216 # a caller for the ff callbacks for custom event loop implementations
219 def virEventInvokeFreeCallback(opaque
):
221 Execute callback which frees the opaque buffer
223 @opaque: the opaque object passed to addHandle or addTimeout
225 WARNING: This function should not be called from any call by libvirt's
226 core. It will most probably cause deadlock in C-level libvirt code.
227 Instead it should be scheduled and called from implementation's stack.
229 See https://libvirt.org/html/libvirt-libvirt-event.html#virEventAddHandleFunc
230 for more information.
232 This function is not dependent on any event loop implementation.
235 libvirtmod
.virEventInvokeFreeCallback(opaque
[2], opaque
[1])