Examples

Python 2.x &3.x

'''
A simple example of how to use the yumdaemon client API for python 2 or 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 yumdaemon 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.GetPackageWithAttributes('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, False)
            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)


if __name__ == "__main__":

    cli = MyClient()
    cli.do_something()
'''
A simple example of how to use the yumdaemon client API for python 2 or 3

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

'''
from yumdaemon 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))

if __name__ == "__main__":

    cli = MyClient()
    cli.do_something()
'''
A simple example of how to use the yumdaemon client API for python 2 or 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 yumdaemon import YumDaemonReadOnlyClient, AccessDeniedError, YumLockedError, YumDaemonError

class MyClient(YumDaemonReadOnlyClient):

    def __init(self):
        YumDaemonReadOnlyClient.__init__(self)

    def do_something(self):
        try:
            self.Lock()
            print("=" * 70)
            print("Getting Updates")
            print("=" * 70)
            result = self.GetPackageWithAttributes('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, False)
            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 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)


if __name__ == "__main__":

    cli = MyClient()
    cli.do_something()

Vala

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

MainLoop main_loop;

async void run_system () {
    string [] packages;
    try {
        YumSystem yum = yield Bus.get_proxy (BusType.SYSTEM,
                                        "org.baseurl.YumSystem",
                                        "/");

        int version = yum.get_version();
        stdout.printf ("================================================================\n");
        stdout.printf ("Testing yumdaemon system service\n");
        stdout.printf ("================================================================\n");
        stdout.printf ("System YumDaemon version %i\n", version);
        bool lock = yum.lock();
        if (lock) {
            packages = yield yum.get_packages("updates");
            foreach (string pkg in packages) {            
                stdout.printf ("package:  %s\n", pkg);            
            }
            packages = yield yum.get_packages_by_name("yum*",true);
            foreach (string pkg in packages) {            
                stdout.printf ("package:  %s\n", pkg);            
            }
            yum.unlock();
        }        
    } catch (IOError e) {
        stderr.printf ("%s\n", e.message);
    }
    main_loop.quit ();
}

async void run_session () {
    string [] packages;
    try {
        YumSession yum = yield Bus.get_proxy (BusType.SESSION,
                                        "org.baseurl.YumSession",
                                        "/");

        int version = yum.get_version();
        stdout.printf ("================================================================\n");
        stdout.printf ("Testing yumdaemon system service\n");
        stdout.printf ("================================================================\n");
        stdout.printf ("Session YumDaemon version %i\n", version);
        bool lock = yum.lock();
        if (lock) {
            packages = yield yum.get_packages("updates");
            foreach (string pkg in packages) {            
                stdout.printf ("package:  %s\n", pkg);            
            }
            packages = yield yum.get_packages_by_name("yum*",true);
            foreach (string pkg in packages) {            
                stdout.printf ("package:  %s\n", pkg);            
            }
            yum.unlock();
        }        
    } catch (IOError e) {
        stderr.printf ("%s\n", e.message);
    }
    main_loop.quit ();
}

int main () {
    run_session();
    main_loop = new MainLoop (null, false);
    main_loop.run ();
    run_system();
    main_loop = new MainLoop (null, false);
    main_loop.run ();
    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.YumSession"
#define 	YUM_INTERFACE	"org.baseurl.YumSession"
#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_SESSION, &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(conn,
						YUM_SERVICE_NAME,
						YUM_OBJECT_PATH,
						YUM_INTERFACE);
    /*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

Client API for Python 2.x and 3.x

This Page