Scripting around your Launchpad project

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Write Access

As mentioned earlier, you can also acquire Launchpad read-write access in your Python scripts. The only difference in the connection setup is how you get the Launchpad connection object.

lp = Launchpad.login_with(
        consumer_name='test',
service_root='production',
        version='devel')

When using login_with() instead of login_anonymously() , the provided arguments are almost exactly the same as before. The difference is that once the script is run by the user in an interactive environment, Launchpad will first try to authorize the connection by using your login credentials. This usually results in a new web browser tab opening up with an authorization request (Figure 2). The user can then choose to allow the computer and its script to access Launchpad for a selected period of time (or indefinitely).

If the script is to be run remotely, the login_with() method must be executed once on an interactive system with the credentials_file keyword argument filled to generate an authorization token to the selected file. That file is then your gateway to Launchpad login, so keep it safe from unauthorized access. Afterward, all operations on this script will be executed with your Launchpad user credentials.

Once launchpadlib is authorized, your scripts have now basically the same powers as you would have when accessing Launchpad through a web browser, allowing you to, for example, write bug comments, modify their contents, copy packages between PPAs, create merge proposals, and more. As an example, I'll slightly modify the bug printing script to a bug commenting application. After adding an additional command-line argument and using login_with() instead of login_anonymously , I just need to modify the part between the try-except statement like this:

bug = lp.bugs[number]
bug.newMessage(content=text)

After running the script with arguments – bug number and message – launchpadlib will create a new comment using the authorized user identity. As mentioned earlier, you're not limited to bug operations only, but this was the easiest thing to show as an example.

More Than Bugs

Almost anything that can be done manually on the Launchpad web site also can be realized through the API. My earlier examples concentrated on bug manipulation, but other tasks can be done as easily. A very convenient feature is the fact that most LP URLs from the web browser – such as bugs, PPAs, builds, users, teams, branches – can be easily loaded and used in your Python launchpadlib script with only a slight modification. The Launchpad object offers a load() method for this purpose.

This method takes a web service API URL and loads it, returning its corresponding Python object. For instance, if you're interested in the details of the unity8 package in the Ubuntu archives, the standard web URL for that is: https://launchpad.net/ubuntu/+source/unity8. To get the API URL for the same object, you prepend the URL with "api" and add the API version string right after the main domain part. You can then load it with the following code:

source = lp.load('https://api.launchpad.net/devel/ubuntu/+source/unity8')

The source object is now available for further usage, allowing you to search all bugs assigned to this source package or manage package bug subscriptions. When trying to open the URL in a web browser, an XML file from the web API will be shown – this is the raw representation that can be used when creating any other language bindings.

Another useful example from the Launchpad API is getting information about published source packages. Suppose you want to see all the unity8 versions published in the Xenial (16.04) series:

distro = lp.distributions['ubuntu']
xenial = distro.getSeries(name_or_version='xenial')
srcs = distro.main_archive.getPublishedSources(\
  source_name='unity8', distro_series=xenial, order_by_date=True, exact_match=True)
for src in srcs:
        print(src.source_package_version)

First, you fetch the Ubuntu distribution object, using it to get a series object for Xenial and then accessing its main_archive member for archive-related queries. The getPublishedSources call lists all source_package_publishing_history objects for source packages that fit the selected query. Here, you ask for all unity8 packages from the Xenial series, ordered by date (descending), and requesting an exact match – without this, LP API will also return all similarly named results. You then iterate over the returned collection and print out the version number for each package published in the archive.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content