Koji Python API – Part 2

How to query packages in koji with the package name?

In this post I will be talking about my most recent release 0.3 and explaining my most recent script chkpkg-koji.py. I’m mostly writing this because of the lack of documentation for using koji python API, or my lack of finding it. I would like to ask anyone who reads this, if you have some better methods of doing any of these tasks, or new cool things, let me know.

Most of this was stolen or taken, in some way, from the koji client written by:
Dennis Gregorovic
Mike McLean
Cristian Balint

Since this is the part 2 of my explanation of koji python API, I will skip over some things. First lets do the first step when working with the API.

import koji
server = "http://koji.fedoraproject.org/kojihub/"
session = koji.ClientSession(server)

This part can be changed, if you’d like to connect to a different koji server just change the server url. For example, to check arm.

server = "http://arm.koji.fedoraproject.org/kojihub/"
session = koji.ClientSession(server)

This is the part that changes from my previous post, instead of querying the package based on task_id, we will do it a different way. A task_id is useless when you are trying to check a package package based on it’s name… and you don’t know the task_id. Here is a work around I found to get this to work, this is probably not the best or easiest way to do this, but it is the best I was able to find(and only?).

Start by querying the build history of the package on koji:

packagename = "units"
tables = session.queryHistory(package=package_name)

This will give a list of all package build versions and releases for all fedora builds. The most important part about this step, is getting a small field of information called the build_id. To get the most recent package build simply sort out the latest version, latest release, and choose the tag_name(f17, f18, f19). This will narrow down your selection to a single, most recent build of a package.

This next part of code is where it all happens, starting with parsing the output of the queryHistory call which was used earlier. When using session.queryHistory, it outputs a giant table that contains even more tables and lists inside. This is a complete mess, but very easy to parse through, just use 2 for loops to separate them all into their own separate tables and it becomes easy to manage each table. I used the pre_release variable to determine which release of the package was the most recent, maybe other people will find better way of doing this. At this point you can now do a little switch to convert your build_id, which will no longer be very important, to a task_id, which everyone loves to use.

for table in tables:
            build = tables[table]
            pre_release = 0
            for x in build:
                    if int(x['release'][0]) < pre_release:
                    elif str(x['version']) == "2.00" and x['tag.name'] == "f18":
                        pre_release = int(x['release'][0])
                        build_info = session.getBuild(x['build_id'])
                        task_id = build_info['task_id']
                        task = session.getTaskInfo(task_id, request=True)

Using the build_id, use build_info = session.getBuild(build_id), which will give you more information on the build. Inside this build_info is the task_id, so you just need to pull it out. task_id = build_info[‘task_id’]. Now we have converted a package_name into a task_id, and can continue working with this task_id just like in part 1 starting with the: task = session.getTaskInfo(task_id, request=True). You can continue one and check all sorts of information using the build_id and task_id such as if the build failed or was successful or some other hidden fields inside the tables.

task_state = task['state']
                        task_request = task['request']
                        task_method = task['method']
                        print "\nbuild_id: " + str(x['build_id'])
                        print "task_id: " + str(task_id)
                        print "version: " + str(x['version'])
                        print "release: " + str(x['release'])
                        print "tag_name: " + str(x['tag.name'])
                        # If FREE or OPEN is true then the package has not finished building on koji
                        if task_state in (koji.TASK_STATES['FREE'], koji.TASK_STATES['OPEN']):
                            print "Task: " + str(task_id) + "\nStatus: Not completed"
                            build_status = "in-progress"
                        # If CLOSED is true then the package finished building successfully
                        elif task_state == koji.TASK_STATES['CLOSED']:
                            print "Task: " + str(task_id) + "\nStatus: Successful"
                            build_status = "successful"
                        # If CANCELED is true then the package was cancelled before it was completed
                        elif task_state == koji.TASK_STATES['CANCELED']:
                            print "Task: " + str(task_id) + "\nStatus: Cancelled"
                            build_status = "cancelled"
                        # If FAILED is true then the package failed the build before it was completed
                        elif task_state == koji.TASK_STATES['FAILED']:
                            print "Task: " + str(task_id) + "\nStatus: Failed"
                            build_status = "failed"
                            print "Task: " + str(task_id) + "\nStatus: Other"
                            build_status = "other"

About oatleywillisa

Computer Networking Student
This entry was posted in SBR600 and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s