2006-02-28 12:17:00 +00:00
#
2009-09-16 14:03:53 +01:00
# Manually written part of python bindings for libvirt
2006-02-28 12:17:00 +00:00
#
2007-11-30 11:10:53 +00:00
# On cygwin, the DLL is called cygvirtmod.dll
try :
import libvirtmod
2009-05-19 13:03:03 +00:00
except ImportError , lib_e :
try :
import cygvirtmod as libvirtmod
except ImportError , cyg_e :
if str ( cyg_e ) . count ( " No module named " ) :
raise lib_e
2007-11-30 11:10:53 +00:00
2006-02-28 12:17:00 +00:00
import types
2006-11-07 23:18:56 +00:00
# The root of all libvirt errors.
2006-02-28 12:17:00 +00:00
class libvirtError ( Exception ) :
2008-08-22 10:50:18 +00:00
def __init__ ( self , defmsg , conn = None , dom = None , net = None , pool = None , vol = None ) :
2006-02-28 12:17:00 +00:00
2009-01-20 22:10:52 +00:00
# Never call virConnGetLastError().
# virGetLastError() is now thread local
err = virGetLastError ( )
2008-08-22 10:50:18 +00:00
if err is None :
msg = defmsg
2006-11-07 23:18:56 +00:00
else :
2008-08-22 10:50:18 +00:00
msg = err [ 2 ]
Exception . __init__ ( self , msg )
self . err = err
2006-11-07 23:18:56 +00:00
def get_error_code ( self ) :
if self . err is None :
return None
return self . err [ 0 ]
def get_error_domain ( self ) :
if self . err is None :
return None
return self . err [ 1 ]
def get_error_message ( self ) :
if self . err is None :
return None
return self . err [ 2 ]
def get_error_level ( self ) :
if self . err is None :
return None
return self . err [ 3 ]
def get_str1 ( self ) :
if self . err is None :
return None
return self . err [ 4 ]
def get_str2 ( self ) :
if self . err is None :
return None
return self . err [ 5 ]
def get_str3 ( self ) :
if self . err is None :
return None
return self . err [ 6 ]
def get_int1 ( self ) :
if self . err is None :
return None
return self . err [ 7 ]
def get_int2 ( self ) :
if self . err is None :
return None
return self . err [ 8 ]
2006-02-28 12:17:00 +00:00
#
# register the libvirt global error handler
#
def registerErrorHandler ( f , ctx ) :
""" Register a Python written function to for error reporting.
The function is called back as f ( ctx , error ) , with error
2008-03-14 11:08:03 +00:00
being a list of information about the error being raised .
2006-02-28 12:17:00 +00:00
Returns 1 in case of success . """
return libvirtmod . virRegisterErrorHandler ( f , ctx )
2007-12-05 19:09:23 +00:00
def openAuth ( uri , auth , flags ) :
ret = libvirtmod . virConnectOpenAuth ( uri , auth , flags )
if ret is None : raise libvirtError ( ' virConnectOpenAuth() failed ' )
return virConnect ( _obj = ret )
2007-05-29 14:58:27 +00:00
#
# Return library version.
#
def getVersion ( name = None ) :
""" If no name parameter is passed (or name is None) then the
version of the libvirt library is returned as an integer .
If a name is passed and it refers to a driver linked to the
libvirt library , then this returns a tuple of ( library version ,
driver version ) .
If the name passed refers to a non - existent driver , then you
will get the exception ' no support for hypervisor ' .
Versions numbers are integers : 1000000 * major + 1000 * minor + release . """
if name is None :
ret = libvirtmod . virGetVersion ( ) ;
else :
ret = libvirtmod . virGetVersion ( name ) ;
if ret is None : raise libvirtError ( " virGetVersion() failed " )
return ret
2008-10-31 10:13:45 +00:00
#
# Invoke an EventHandle callback
#
2011-06-15 20:14:45 -04:00
def _eventInvokeHandleCallback ( watch , fd , event , opaque , opaquecompat = None ) :
2008-10-31 10:13:45 +00:00
"""
Invoke the Event Impl Handle Callback in C
"""
2011-06-15 20:02:27 -04:00
# libvirt 0.9.2 and earlier required custom event loops to know
# that opaque=(cb, original_opaque) and pass the values individually
# to this wrapper. This should handle the back compat case, and make
# future invocations match the virEventHandleCallback prototype
if opaquecompat :
callback = opaque
opaque = opaquecompat
else :
callback = opaque [ 0 ]
opaque = opaque [ 1 ]
2008-11-24 19:28:12 +00:00
libvirtmod . virEventInvokeHandleCallback ( watch , fd , event , callback , opaque ) ;
2008-10-31 10:13:45 +00:00
#
# Invoke an EventTimeout callback
#
2011-06-15 20:14:45 -04:00
def _eventInvokeTimeoutCallback ( timer , opaque , opaquecompat = None ) :
2008-10-31 10:13:45 +00:00
"""
Invoke the Event Impl Timeout Callback in C
"""
2011-06-15 20:02:27 -04:00
# libvirt 0.9.2 and earlier required custom event loops to know
# that opaque=(cb, original_opaque) and pass the values individually
# to this wrapper. This should handle the back compat case, and make
# future invocations match the virEventTimeoutCallback prototype
if opaquecompat :
callback = opaque
opaque = opaquecompat
else :
callback = opaque [ 0 ]
opaque = opaque [ 1 ]
2008-10-31 10:13:45 +00:00
libvirtmod . virEventInvokeTimeoutCallback ( timer , callback , opaque ) ;
2011-06-15 19:35:44 -04:00
def _dispatchEventHandleCallback ( watch , fd , events , cbData ) :
cb = cbData [ " cb " ]
opaque = cbData [ " opaque " ]
cb ( watch , fd , events , opaque )
return 0
def _dispatchEventTimeoutCallback ( timer , cbData ) :
cb = cbData [ " cb " ]
opaque = cbData [ " opaque " ]
cb ( timer , opaque )
return 0
def virEventAddHandle ( fd , events , cb , opaque ) :
"""
register a callback for monitoring file handle events
@fd : file handle to monitor for events
@events : bitset of events to watch from virEventHandleType constants
@cb : callback to invoke when an event occurs
@opaque : user data to pass to callback
Example callback prototype is :
def cb ( watch , # int id of the handle
fd , # int file descriptor the event occured on
events , # int bitmap of events that have occured
opaque ) : # opaque data passed to eventAddHandle
"""
cbData = { " cb " : cb , " opaque " : opaque }
ret = libvirtmod . virEventAddHandle ( fd , events , cbData )
if ret == - 1 : raise libvirtError ( ' virEventAddHandle() failed ' )
return ret
def virEventAddTimeout ( timeout , cb , opaque ) :
"""
register a callback for a timer event
@timeout : time between events in milliseconds
@cb : callback to invoke when an event occurs
@opaque : user data to pass to callback
Setting timeout to - 1 will disable the timer . Setting the timeout
to zero will cause it to fire on every event loop iteration .
Example callback prototype is :
def cb ( timer , # int id of the timer
opaque ) : # opaque data passed to eventAddTimeout
"""
cbData = { " cb " : cb , " opaque " : opaque }
ret = libvirtmod . virEventAddTimeout ( timeout , cbData )
if ret == - 1 : raise libvirtError ( ' virEventAddTimeout() failed ' )
return ret