Diff for "API/Examples"

Not logged in - Log In / Register

Differences between revisions 1 and 10 (spanning 9 versions)
Revision 1 as of 2009-11-02 20:45:06
Size: 229
Editor: 78-105-1-164
Comment:
Revision 10 as of 2009-11-25 20:38:33
Size: 3042
Editor: 70-8-217-239
Comment: Make the first example be how to find out your launchpadlib version.
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
This page has a bunch of examples of how to use `launchpadlib` and the Python APIs. Think of it like a cookbook that you can add your favourite recipe to.

If this duplicates [[API/launchpadlib|launchpadlib]] or [[API/Uses]] too much, then please merge or edit pages as needed.

== Find out if your launchpadlib version is recent enough (>= 1.5.1) ==

{{{
#!python
import launchpadlib
print launchpadlib.__version__
}}}

'''1.5.1''' or above is the answer you're looking for; all subsequent examples assume you have at least that recent a launchpadlib.
Line 6: Line 20:

Ever wanted to have Launchpad greet you by your own name? Now you can, in the comfort of your own home.

{{{
#!python
from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
launchpad = Launchpad.login_with(
    'hello-world', EDGE_SERVICE_ROOT)
print 'Hello, %s!' % launchpad.me.display_name
}}}

The `hello-world` bit is the name of the application and EDGE_SERVICE_ROOT means connect to the edge server.

== Does a bug have a release target? ==

{{{
#!python
from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT

def has_target(bug, series):
    series_url = str(series)
    for task in bug.bug_tasks:
        if str(task).startswith(series_url):
            return True
    return False

launchpad = Launchpad.login_with(
    'hello-world', EDGE_SERVICE_ROOT)
b = launchpad.bugs[324614]
ubuntu = launchpad.distributions["ubuntu"]
jaunty = ubuntu.getSeries(name_or_version="jaunty")
has_target(b, jaunty)
### ==> should evalute to True
}}}

== Listing the current package versions in a particular distroseries ==

{{{
#!python
from launchpadlib.launchpad import Launchpad

launchpad = Launchpad.login_with(
    'hello-world', 'production')
ubuntu = launchpad.distributions["ubuntu"]
archive = ubuntu.main_archive
series = ubuntu.current_series
archive.getPublishedSources(exact_match=True, source_name="apport", distro_series=series)[0].source_package_version
### ==> should return u'0.123'
}}}

== Get dsc-files for sources in an archive ==

{{{
#!python
import re
import urlparse

### See previous examples for how to get an archive.
                
def create_webroot_url_from_self_link(self_link):
    scheme, netloc, _, _, _ = urlparse.urlsplit(self_link)
    netloc = netloc.lstrip("api.")
    return u"%s://%s/" %(scheme, netloc)
    
def get_dsc(archive):
    re_version = re.compile(r"^\d+\:")
    x = archive.getPublishedSources()
    webroot = create_webroot_url_from_self_link(archive.self_link)
    for i in x:
        version = i.source_package_version
        version = re_version.sub("", version, 1)
        yield "%s~%s/+archive/+files/%s_%s.dsc" \
            %(webroot, archive.owner.name, i.source_package_name, version)
}}}

Launchpad Help > API > Examples

This page has a bunch of examples of how to use launchpadlib and the Python APIs. Think of it like a cookbook that you can add your favourite recipe to.

If this duplicates launchpadlib or API/Uses too much, then please merge or edit pages as needed.

Find out if your launchpadlib version is recent enough (>= 1.5.1)

   1 import launchpadlib
   2 print launchpadlib.__version__

1.5.1 or above is the answer you're looking for; all subsequent examples assume you have at least that recent a launchpadlib.

Hello Launchpad!

Ever wanted to have Launchpad greet you by your own name? Now you can, in the comfort of your own home.

   1 from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
   2 launchpad = Launchpad.login_with(
   3     'hello-world', EDGE_SERVICE_ROOT)
   4 print 'Hello, %s!' % launchpad.me.display_name

The hello-world bit is the name of the application and EDGE_SERVICE_ROOT means connect to the edge server.

Does a bug have a release target?

   1 from launchpadlib.launchpad import Launchpad,  EDGE_SERVICE_ROOT
   2 
   3 def has_target(bug, series):
   4     series_url = str(series)
   5     for task in bug.bug_tasks:
   6         if str(task).startswith(series_url):
   7             return True
   8     return False
   9 
  10 launchpad = Launchpad.login_with(
  11     'hello-world',  EDGE_SERVICE_ROOT)
  12 b = launchpad.bugs[324614]
  13 ubuntu = launchpad.distributions["ubuntu"]
  14 jaunty = ubuntu.getSeries(name_or_version="jaunty")
  15 has_target(b, jaunty)
  16 ### ==> should evalute to True

Listing the current package versions in a particular distroseries

   1 from launchpadlib.launchpad import Launchpad
   2 
   3 launchpad = Launchpad.login_with(
   4     'hello-world', 'production')
   5 ubuntu = launchpad.distributions["ubuntu"]
   6 archive = ubuntu.main_archive
   7 series = ubuntu.current_series
   8 archive.getPublishedSources(exact_match=True, source_name="apport", distro_series=series)[0].source_package_version
   9 ### ==> should return u'0.123'

Get dsc-files for sources in an archive

   1 import re
   2 import urlparse
   3 
   4 ### See previous examples for how to get an archive.
   5                 
   6 def create_webroot_url_from_self_link(self_link):
   7     scheme, netloc, _, _, _ = urlparse.urlsplit(self_link)
   8     netloc = netloc.lstrip("api.")
   9     return u"%s://%s/" %(scheme, netloc)
  10     
  11 def get_dsc(archive):
  12     re_version = re.compile(r"^\d+\:")
  13     x = archive.getPublishedSources()
  14     webroot = create_webroot_url_from_self_link(archive.self_link)
  15     for i in x:
  16         version = i.source_package_version
  17         version = re_version.sub("", version, 1)
  18         yield "%s~%s/+archive/+files/%s_%s.dsc" \
  19             %(webroot, archive.owner.name, i.source_package_name, version)

API/Examples (last edited 2016-04-15 18:13:29 by cjwatson)