Examples

Python 3.x

#!/usr/bin/python3
'''
A simple example of how to use the yumdaemon client API for python 3 

This example show how to get available updates (incl. summary & size) and
how to search for packages where 'yum' is in the name.
'''
from yumdaemon3 import YumDaemonClient, AccessDeniedError, YumLockedError, YumDaemonError, YumTransactionError

class MyClient(YumDaemonClient):

    def __init(self):
        YumDaemonClient.__init__(self)
        
    def do_something(self):
        try:
            self.Lock()
            print("=" * 70)
            print("Getting Updates")
            print("=" * 70)
            result = self.GetPackageObjects('updates',['summary','size'])
            for (pkg_id,summary,size) in result:
                print("%s\n\tsummary : %s\n\tsize : %s" % (self._fullname(pkg_id),summary,size))
            print("=" * 70)
            print("Search : yum ")
            print("=" * 70)
            result = self.Search(["name"],["yum"], True)
            for id in result:
                print(" --> %s" % self._fullname(id))
        except AccessDeniedError as err:
            print("Access Denied : \n\t"+str(err))
        except YumLockedError as err:
            print("Yum Locked : \n\t"+str(err))
        except YumTransactionError as err:
            print("Yum Transaction Error : \n\t"+str(err))
        except YumDaemonError as err:    
            print("Error in Yum Backend : \n\t"+str(err))
            print(err)
        finally:
            # always try to Unlock (ignore errors)
            try:
                self.Unlock()
            except:
                pass
            
    def _fullname(self,id):
        ''' Package fullname  '''
        (n, e, v, r, a, repo_id)  = str(id).split(',')
        if e and e != '0':
            return "%s-%s:%s-%s.%s (%s)" % (n, e, v, r, a, repo_id)
        else:
            return "%s-%s-%s.%s (%s)" % (n, v, r, a, repo_id)

                    
    def on_UpdateProgress(self,name,frac,fread,ftime):
        pass

    def on_TransactionEvent(self,event):
        pass

    def on_RPMProgress(self, package, action, te_current, te_total, ts_current, ts_total):
        pass

if __name__ == "__main__":
    
    cli = MyClient()
    cli.do_something()
#!/usr/bin/python3
'''
A simple example of how to use the yumdaemon client API for python 3 

The example show how to install & remove the '0xFFFF' package

'''
from yumdaemon3 import YumDaemonClient, AccessDeniedError, YumLockedError, YumDaemonError, YumTransactionError

class MyClient(YumDaemonClient):

    def __init(self):
        YumDaemonClient.__init__(self)
        
    def do_something(self):
        try:
            self.Lock()
            print("=" * 70)
            print("Install : 0xFFFF")
            print("=" * 70)
            rc, result = self.Install('0xFFFF')
            if rc==2: # OK
                print ("Dependency resolution completed ok")
                self._show_transaction_result(result)
                result = self.RunTransaction()
                print(result)
            elif rc == 0: # Nothing to do (package now found or already installed)
                print("Noting to do")
            else: # Error in Dependency resolution
                print ("Dependency resolution failed")
                print(result)
            print("=" * 70)
            print("Remove : 0xFFFF")
            print("=" * 70)
            rc, result = self.Remove('0xFFFF')
            if rc==2:
                print ("Dependency resolution completed ok")
                self._show_transaction_result(result)
                result = self.RunTransaction()
                print(result)
            elif rc == 0:
                print("Noting to do")
            else:
                print ("Dependency resolution failed")
                print(result)
        except AccessDeniedError as err:
            print("Access Denied : \n\t"+str(err))
        except YumLockedError as err:
            print("Yum Locked : \n\t"+str(err))
        except YumTransactionError as err:
            print("Yum Transaction Error : \n\t"+str(err))
        except YumDaemonError as err:    
            print("Error in Yum Backend : \n\t"+str(err))
            print(err)
        finally:
            # always try to Unlock (ignore errors)
            try:
                self.Unlock()
            except:
                pass
            
    def _fullname(self,id):
        ''' Package fullname  '''
        (n, e, v, r, a, repo_id)  = str(id).split(',')
        if e and e != '0':
            return "%s-%s:%s-%s.%s (%s)" % (n, e, v, r, a, repo_id)
        else:
            return "%s-%s-%s.%s (%s)" % (n, v, r, a, repo_id)

    def _show_transaction_result(self, output):
        for action, pkgs in output:
            print( "  %s" % action)
            for pkg_list in pkgs:
                id, size, obs_list = pkg_list  # (pkg_id, size, list with id's obsoleted by this pkg)
                print ("    --> %-50s : %s" % (self._fullname(id),size))

                    
    def on_UpdateProgress(self,name,frac,fread,ftime):
        print("   --> Downloading : %s (%i %%) " % (name, int(frac*100)))

    def on_TransactionEvent(self,event):
        print("Event: ", event)

    def on_RPMProgress(self, package, action, te_current, te_total, ts_current, ts_total):
        if action == 'erase':
            print("   --> %s : %s :" % (action,package))
        else:
            print("   --> %s : %s : (%.2f / %.2f)" % (action,self._fullname(package),te_current, te_total))

if __name__ == "__main__":
    
    cli = MyClient()
    cli.do_something()

Vala

[DBus (name = "org.baseurl.Yum.Interface")]
interface Yum : Object {
    public abstract bool lock() throws IOError;
    public abstract bool unlock() throws IOError;
    public abstract int get_version () throws IOError;
    public abstract string[] get_packages(string pkg_narrow) throws IOError;
    public abstract string[] get_packages_by_name(string pattern, bool newest_only) throws IOError;
}

int main () {
    string [] packages;
    try {
        Yum yum = Bus.get_proxy_sync (BusType.SYSTEM,
                                        "org.baseurl.Yum",
                                        "/");

        int  version = yum.get_version();
        stdout.printf ("Yum Daemon version %i\n", version);
        if (yum.lock()) {
            packages = yum.get_packages("updates");
            foreach (string pkg in packages) {            
                stdout.printf ("package:  %s\n", pkg);            
            }
            packages = yum.get_packages_by_name("yum*",true);
            foreach (string pkg in packages) {            
                stdout.printf ("package:  %s\n", pkg);            
            }
            yum.unlock();
        }        
//        var loop = new MainLoop ();
//        loop.run ();

    } catch (IOError e) {
        stderr.printf ("%s\n", e.message);
        return 1;
    }

    return 0;
}

C

#include 	<glib.h>
#include	<glib-object.h>
#include	<dbus/dbus.h>
#include	<dbus/dbus-glib.h>

#define 	YUM_SERVICE_NAME   "org.baseurl.Yum"
#define 	YUM_INTERFACE	"org.baseurl.Yum.Interface"
#define 	YUM_OBJECT_PATH	"/"

gboolean yum_dbus_init();
gboolean yum_dbus_translate(const gchar*, gboolean, gchar*);

static DBusGConnection *conn = NULL;
static DBusGProxy *proxy = NULL;
static gboolean *version = NULL;
static gboolean *locked = NULL;
char **package_list;
char **package_list_ptr;


gboolean yum_dbus_init()
{
	GError *error = NULL;
	
	conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
	
	if (conn == NULL) {
		g_warning("Error %s\n", error->message);
        g_error_free(error);
		return FALSE;
	}
	else
	{
		g_debug("conn object is %d\n", conn);
	}

	proxy = dbus_g_proxy_new_for_name_owner(conn,
						YUM_SERVICE_NAME,
						YUM_OBJECT_PATH,
						YUM_INTERFACE,
					        &error);
    dbus_g_proxy_set_default_timeout(proxy, 60000);		
	g_debug("proxy %d\n", proxy);
									
	if (proxy == NULL || error != NULL)
	{
		g_warning("Cannot connect to the yum service : %s\n", error->message);
		g_error_free(error);
		return FALSE;
	}
	else
	{
		return TRUE;
	}
	
}

gboolean yum_dbus_get_version()
{
		
	GError *error = NULL;
	dbus_g_proxy_call(proxy, "GetVersion", &error, 
					G_TYPE_INVALID,
					G_TYPE_INT,
					&version,
					G_TYPE_INVALID);
									
	
	if (error) 
        {
    		g_warning("Error : %s\n", error->message);
	        g_error_free(error);
            return FALSE;
        }
	else
        {
	        return TRUE;
        }
}

gboolean yum_dbus_lock()
{
		
	GError *error = NULL;
	dbus_g_proxy_call(proxy, "Lock", &error, 
					G_TYPE_INVALID,
					G_TYPE_BOOLEAN,
					&locked,
					G_TYPE_INVALID);
									
	
	if (error) 
        {
    		g_warning("Error : %s\n", error->message);
	        g_error_free(error);
            return FALSE;
        }
	else
        {
            if (locked) {
                g_print("Yum is now locked\n");
                return TRUE;
            } else {
                g_print("Yum is locked by another application\n");
                return FALSE;
            }
        }
}

gboolean yum_dbus_unlock()
{
		
	GError *error = NULL;
    gboolean *unlocked = NULL;    

	dbus_g_proxy_call(proxy, "Unlock", &error, 
					G_TYPE_INVALID,
					G_TYPE_BOOLEAN,
					&unlocked,
					G_TYPE_INVALID);
									
	
	if (error) 
        {
    		g_warning("Error : %s\n", error->message);
	        g_error_free(error);
            return FALSE;
        }
	else
        {
            if (unlocked) {
                g_print("Yum is unlocked\n");
                locked = FALSE;
                return TRUE;
            } else {
      	        return FALSE;
            }
        }
}

gboolean yum_dbus_get_packages_by_name(gchar* pattern, gboolean use_newest)
{
		
	GError *error = NULL;

    g_print("Getting packages matching : %s \n", pattern);
	dbus_g_proxy_call(proxy, "GetPackagesByName", &error, 
					G_TYPE_STRING, pattern,
					G_TYPE_BOOLEAN, use_newest, 
					G_TYPE_INVALID,
					G_TYPE_STRV, &package_list,
					G_TYPE_INVALID);
									
	
	if (error) 
        {
    		g_warning("Error : %s\n", error->message);
	        g_error_free(error);
            yum_dbus_unlock();
            return FALSE;
        }
	else
        {
            g_print("Got packages\n");
            return TRUE;
        }
}

int main(int argc, char** argv)
{
	g_type_init();	
	
	if (yum_dbus_init() == FALSE)
		g_error("yum_dbus_init, unable to connect yum DBUS service");
	else
	{
		yum_dbus_get_version();	
		g_print("version is : %i\n", version);
        if (yum_dbus_lock() == TRUE) {
    		g_print("Ready for some action\n");    
            if (yum_dbus_get_packages_by_name("yum*", TRUE)) {
                g_print("Packages:\n");
                g_print("==========================:\n");
                for (package_list_ptr = package_list; *package_list_ptr; package_list_ptr++)
                {
                  g_print ("  %s\n", *package_list_ptr);
                }
                g_strfreev (package_list);
            }
            yum_dbus_unlock();
        }
		g_object_unref(proxy);
	}	
	return 0;
}

Table Of Contents

Previous topic

yumdaemon DBus client API for Python 2.x

This Page