Skip to content

Instantly share code, notes, and snippets.

@cyli
Created April 11, 2012 21:11
Show Gist options
  • Save cyli/2362616 to your computer and use it in GitHub Desktop.
Save cyli/2362616 to your computer and use it in GitHub Desktop.
Download twisted package .dsc's from twisted's launchpad repo
#!/usr/bin/env python
"""
Downloads dsc files associated with a particular version of Twisted for a
particular Ubuntu release.
"""
import re
import subprocess
import sys
import urlparse
from launchpadlib.launchpad import Launchpad
from twisted.python.usage import Options
def get_twisted_dev_archive(launchpad):
"""
Given a launchpad object that gives access to launchpad data (can be
an anonymous login or using an actual user's credentials), retrieves the
archive object associated with the twisted dev team.
@param launchpad: launchpad access object
@type launchpad: what is returned by L{Launchpad.login_anonymously} or
L{Launchpad.login_with}
@return: a C{Archive} object
"""
twisted_dev = launchpad.people['twisted-dev']
return twisted_dev.archive
# ----- These functions copied from https://help.launchpad.net/API/Examples
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, package_filter=None, version_filter=None):
"""
Given an archive, returns all the dsc files (as an iterator) for the
packages in that archive
@param archive: the archive to search
@type archive: a C{Archive} object
@param package_filter: the version to filter by - if None, does not
filter by package
@type package_filter: C{regexp}
@param version_filter: the version to filter by - if None, does not
filter by version
@type version_filter: C{regexp}
@return: iterable of all the dsc files in the 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)
if ((not version_filter or version_filter.search(version)) and
(not package_filter or
package_filter.search(i.source_package_name))):
yield "%s~%s/+archive/+files/%s_%s.dsc" % (
webroot, archive.owner.name, i.source_package_name, version)
# -----
def dget_dscs(dscs):
"""
If dget is available, downloads (not extract) all the dsc files.
@param dscs: iterable containing all the dscs to download
@type dscs: C{iterable}
@return: None when all the dscs have downloaded
"""
for dsc in dscs:
print "Downloading %s" % (dsc,)
subprocess.check_call(['dget', '-du', dsc])
# -----
class DSCOptions(Options):
"""
Options to run this script with
"""
optParameters = [
["package_name", "p", None, "Specify a package name regex"],
["version_number", "v", None, "Specify a package version regex"]
]
optFlags = [
["download", "d", ("Download files with dget (no extraction). "
"If this option is not provided, will just "
"list the packages.")]
]
if __name__ == "__main__":
"""
TODO: usage, parse args
"""
options = DSCOptions()
options.parseOptions()
launchpad = Launchpad.login_anonymously('get info', 'production')
archive = get_twisted_dev_archive(launchpad)
kwargs = {}
if options['package_name']:
kwargs['package_filter'] = re.compile(str(options['package_name']))
if options['version_number']:
kwargs['version_filter'] = re.compile(str(options['version_number']))
dscs = get_dsc(archive, **kwargs)
if options['download'] != None:
dget_dscs(dscs)
else:
for dsc in dscs:
print dsc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment