Using PackageKit in Python

In order to provide a sensible upstream implementation for package query/install/remove methods in Jockey, I started playing with PackageKit and recently packaged and fixed the latest upstream version 0.2.2 work reasonably well on Intrepid.

Unfortunately there are no official Python bindings yet. The raw D-BUS interface is slightly inconvenient to use, since it is fully asynchronous. This seems to be pretty redundant to me, since D-BUS already provides asynchronous method calls (if you need them) and makes writing code painful in synchronous programs.

Thus I went ahead and created a fairly easy Python class for calling the most common PackageKit functions from a Python program (source code), including some demo code.

Now the usage becomes fairly simple:

pk = PackageKitClient()

    print pk.Resolve('none', 'pmount')
    # [(False, 'pmount;0.9.17-2;amd64;Ubuntu', 'mount removable devices as normal user')]

    print pk.GetDetails('installation-guide-powerpc;20080520ubuntu1;all;Ubuntu')
    # ('unknown', 'unknown', 'This package contains the Ubuntu installation guide \
    # for the PowerPC architecture, in a variety of languages.\nA shorter reference, \
    # the installation HOWTO, is included in an appendix. ', '', 1074334)

    def cb(status, pc, spc, el, rem, c):
        print 'install pkg: %s, %i%%, cancel allowed: %s' % (status, pc, str(c))
        return True # return False to cancel
    pk.InstallPackages(['pmount;0.9.17-2;i386;Ubuntu', 'quilt;0.46-6;all;Ubuntu'], cb)

As usual in Python, errors are represented as exceptions.

This just leaves a few nitpicks now, such as PackageKit not being able to determine the package license with Apt, but by and large this provides the basic building blocks now.