Scripting around your Launchpad project

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Iurii Kovalenko,

Iurii Kovalenko,

Lift Off!

Launchpad is a great way to get bleeding-edge and niche software for your Ubuntu, but it also provides a complete API, which allows you to write scripts to automate your access. We look into the Python 3 bindings for the Launchpad API.

Launchpad (LP) is well known to most people involved with Ubuntu development, because it's the main place for almost all Ubuntu core involvement. It's a very popular web service for driving and maintaining software projects – both open and closed source. It offers bug-tracking functionality, version control through Bazaar (bzr) or Git, translations, Debian package builders, private package archives, and many other features useful when working in a distributed environment. But, it's not just a useful tool for web users; Launchpad also offers a very rich API allowing scripts to access the same functionality available for humans through the web browser: the Launchpad API. In this article, I will concentrate on the most popular and convenient bindings: the Python launchpadlib .

Figure 1: Output and usage of the Launchpad bug script.

Quick Setup

Ubuntu currently ships two separate Python bindings for Launchpad: one for Python 2.x and one for Python 3.x. In all the examples here, I will use the latter, as there is no real reason not to switch to 3.x whenever it's possible. To get all the required bits in place, you need to install the python3-launchpadlib (or python-launchpadlib for 2.x) package along with all its dependencies [1].

sudo apt-get install python3-launchpadlib

No additional steps are needed, as all the required packages are in the main archives. The same can be achieved by using the pip installer.

pip3 install launchpadlib

for non-Ubuntu distributions.

First Application

I'll start with a very simple Python 3 example. Listing 1 shows a small script whose purpose is to write out the main title of a selected bug number. In Launchpad, each bug is assigned an unique bug number that identifies the bug in a global scope.

Listing 1

01 #!/usr/bin/python3
03 import sys
04 from launchpadlib.launchpad import Launchpad
06 def main():
07         if len(sys.argv) < 2:
08                 print('Usage: BUG_NUMBER')
09                 exit(1)
11         number = sys.argv[1]
12         if not number.isdigit():
13                 print('BUG_NUMBER needs to be an integer.')
14                 exit(1)
16         lp = Launchpad.login_anonymously(
17                 consumer_name='bug-details-test', service_root='production',
18                 version='devel')
20         number = int(sys.argv[1])
21         try:
22                 print(lp.bugs[number].title)
23         except KeyError:
24                 print('Bug #{} does not exist.'.format(number))
26 if __name__ == '__main__':
27         main()

I'll look at the example in detail – you can skip the first argument check bits as those are unrelated to launchpadlib usage. First, obviously, you need to establish a connection to Launchpad itself. This can be done in two ways – anonymously or with selected credentials. The latter is required if your scripts need write privileges to modify something on Launchpad, like adding bugs, requesting package rebuilds, etc. Anonymous users only get read access, which is sufficient for the example case here.

To log in and get a Launchpad object to work, the login_anonymously() method is used. It generally takes three arguments: consumer_name , which can be any string defining our purpose (only used for statistics, can be anything); service_root , defining which LP instance should be used – the production one or staging; and, finally, version , which is the version of the API to be used. Here, I use the devel version, as the last stable version (which was 1.0 at this writing) is heavily outdated and not recommended.

The login method returns a Launchpad object that you can use for communication with the Launchpad API. The number of calls it provides is massive, but for now I only want to use it for fetching Launchpad bug information. The LP object I created has a property called "bugs," which is a collection storing all registered bug reports accessible through their unique numbers. Next, I try to access the "bug" object for the selected bug number from the bugs collection. If such a bug does not exist, Launchpadlib will throw a KeyError. A bug object has multiple interesting properties, one of which is "title," which I print out to the user before exit. Figure 2 shows a sample usage of the first script.

Figure 2: Launchpad prompt for authorization access.

The good part about the Launchpad API is also the documentation, which is always kept up to date [2]. All the properties and calls listed there are available both in the Python bindings as in the raw HTTP API.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $0.99
(incl. VAT)

Buy Ubuntu User

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content