Koji Python API – Part 1

Recently I ran into a bunch of issues with the koji python API. Starting out was the hardest part since I did not know how to use the koji api and it was extremely difficult to find resources relating to it. Finally I found a name of a person maintaining koji, Mike Bonnet, and search around for some of his scripts(Mike Bonnet’s Script) which were really useful. Using his scripts and some other resources I was able to understand quite a bit about the koji python api and make this post.

First I will talk about some of the things I learned while searching around and reading scripts.

How to Query Koji Using Python:

Start by importing koji

import koji

Next you will need to create a new session object. Make sure you point it toward the correct koji server. For example you could put it the default koji server

session = koji.ClientSession('http://koji.fedoraproject.org/kojihub')

or you could send it to the arm koji server.

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

Once you have created the session the next steps are to find the task id you would like to use.
Note:(Querying only by task id was an issue at first for me at first, as I wanted to query through package names and versions of builds. But I could not find a way to do it.)

task_id = 123456

Then you can query that task id for info on the task.

task = session.getTaskInfo(task_id, request=True)

At this point you can now get the state of the package, each state has it’s own value from 0 – 5. These states can be viewed with:

koji.TASK_STATES['FREE']           # = 0
koji.TASK_STATES['OPEN']           # = 1
koji.TASK_STATES['CLOSED']         # = 2
koji.TASK_STATES['CANCELED']       # = 3
koji.TASK_STATES['ASSIGNED']       # = 4
koji.TASK_STATES['FAILED']         # = 5

Now you can compare your state to any of these states:

state = task['state']

if state == koji.TASK_STATES['FREE'] or state == koji.TASK_STATES['FREE']:
    print "Job has not finished yet"

elif state == koji.TASK_STATES['CLOSE']:
    print "Job completed successfully"

elif state == koji.TASK_STATES['FAILED']:
    print "Job failed"

elif state == koji.TASK_STATES['CANCELED']
    print "Job canceled"

The next part will allow you to check information on the child tasks you have selected. Note that you may specify specific information to narrow down the information it gives you. The below code for example will give you the child tasks no matter what state they are in. You must keep the options ‘parent’ and ‘decode’. without ‘parent’ it will not work, decode simply removes a lot of tags that make it nearly impossible to read the output.

tasks = session.listTasks(opts={'parent': task_id, 'method': 'buildArch', 'decode': True})

But you could also narrow it down by putting in the state. This will allow you to choose to show only child tasks that completed successfully

tasks = session.listTasks(opts={'parent': task_id, 'method': 'buildArch', 'state': [koji.TASK_STATES['CLOSED']], 'decode': True})

or only ones that failed.. etc

tasks = session.listTasks(opts={'parent': task_id, 'method': 'buildArch', 'state': [koji.TASK_STATES['FAILED']], 'decode': True})

About oatleywillisa

Computer Networking Student
This entry was posted in SBR600. 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