Diff for "API/Examples"

Not logged in - Log In / Register

Differences between revisions 12 and 13
Revision 12 as of 2009-11-26 01:45:48
Size: 4398
Editor: ppp112-44
Comment:
Revision 13 as of 2009-11-30 04:51:33
Size: 4578
Editor: 173-109-241-128
Comment: Don't use the "#!python" thing in preformatted areas.
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
#!python
import launchpadlib
print launchpadlib.__version__
   import launchpadlib
   print launchpadlib.__version__
Line 24: Line 23:
#!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
   from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
   launchpad = Launchpad.login_with(
       'hello-world', EDGE_SERVICE_ROOT)
   print 'Hello, %s!' % launchpad.me.display_name
Line 36: Line 34:
#!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
   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
Line 58: Line 55:
#!python
from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT

launchpad = Launchpad.login_with(
    'hello-world', EDGE_SERVICE_ROOT)
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'
   from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
       launchpad = Launchpad.login_with(
    'hello-world', EDGE_SERVICE_ROOT)
   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'
Line 73: Line 69:
#!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)
   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)
Line 100: Line 95:
#!python
def create_session():
    lplib_cachedir = os.path.expanduser("~/.cache/launchpadlib/")
    hydrazine_cachedir = os.path.expanduser("~/.cache/hydrazine/")
    rrd_dir = os.path.expanduser("~/.cache/hydrazine/rrd")
    for d in [lplib_cachedir, hydrazine_cachedir, rrd_dir]:
        if not os.path.isdir(d):
            os.makedirs(d, mode=0700)


hydrazine_credentials_filename = os.path.join(hydrazine_cachedir,
        'credentials')
    if os.path.exists(hydrazine_credentials_filename):
        credentials = Credentials()
        credentials.load(file(
            os.path.expanduser("~/.cache/hydrazine/credentials"),
            "r"))
        trace('loaded existing credentials')
        return Launchpad(credentials, service_root,
            lplib_cachedir)
        # TODO: handle the case of having credentials that have expired etc
    else:
        launchpad = Launchpad.get_token_and_login(
            'Hydrazine',
            service_root,
            lplib_cachedir)
        trace('saving credentials...')
        launchpad.credentials.save(file(
            hydrazine_credentials_filename,
            "w"))
        return launchpad
   def create_session():
    lplib_cachedir = os.path.expanduser("~/.cache/launchpadlib/")
       hydrazine_cachedir = os.path.expanduser("~/.cache/hydrazine/")
       rrd_dir = os.path.expanduser("~/.cache/hydrazine/rrd")
       for d in [lplib_cachedir, hydrazine_cachedir, rrd_dir]:
           if not os.path.isdir(d):
    os.makedirs(d, mode=0700)
            hydrazine_credentials_filename = os.path.join(hydrazine_cachedir,
           'credentials')
       if os.path.exists(hydrazine_credentials_filename):
           credentials = Credentials()
           credentials.load(file(
    os.path.expanduser("~/.cache/hydrazine/credentials"),
               "r"))
           trace('loaded existing credentials')
           return Launchpad(credentials, service_root,
               lplib_cachedir)
    # TODO: handle the case of having credentials that have expired etc
       else:
           launchpad = Launchpad.get_token_and_login(
    'Hydrazine',
               service_root,
               lplib_cachedir)
           trace('saving credentials...')
           launchpad.credentials.save(file(
               hydrazine_credentials_filename,
    "w"))
           return launchpad

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)

   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.

Hello Launchpad!

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

   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?

   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

   from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
   
   launchpad = Launchpad.login_with(
       'hello-world', EDGE_SERVICE_ROOT)
   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

   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)

Cache Launchpad credentials per application

From <https://launchpad.net/hydrazine> - use your own application name

   def create_session():
       lplib_cachedir = os.path.expanduser("~/.cache/launchpadlib/")
       hydrazine_cachedir = os.path.expanduser("~/.cache/hydrazine/")
       rrd_dir = os.path.expanduser("~/.cache/hydrazine/rrd")
       for d in [lplib_cachedir, hydrazine_cachedir, rrd_dir]:
           if not os.path.isdir(d):
               os.makedirs(d, mode=0700)
   
   
       hydrazine_credentials_filename = os.path.join(hydrazine_cachedir,
           'credentials')
       if os.path.exists(hydrazine_credentials_filename):
           credentials = Credentials()
           credentials.load(file(
               os.path.expanduser("~/.cache/hydrazine/credentials"),
               "r"))
           trace('loaded existing credentials')
           return Launchpad(credentials, service_root,
               lplib_cachedir)
           # TODO: handle the case of having credentials that have expired etc
       else:
           launchpad = Launchpad.get_token_and_login(
               'Hydrazine',
               service_root,
               lplib_cachedir)
           trace('saving credentials...')
           launchpad.credentials.save(file(
               hydrazine_credentials_filename,
               "w"))
           return launchpad

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