#!/usr/bin/python # # pull-lp-source -- pull a source package from Launchpad # Basic usage: pull-lp-source [] # # Copyright (C) 2008 Iain Lane # # BackportFromLP class taken from prevu tool, which is: # Copyright (C) 2006 John Dong # # ################################################################## # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # See file /usr/share/common-licenses/GPL for more details. # # ################################################################## import os import re import subprocess import sys import urllib2 from optparse import OptionParser class BackportFromLP: def __getitem__(self, name): return getattr(self, name) def __init__(self, package, target_release): self.package = package self.target_release = target_release self.__prepare_sources() def __prepare_sources(self): # Scrape the source package from Launchpad :) contents = urllib2.urlopen('https://launchpad.net/ubuntu/%(target_release)s/+source/%(package)s' % self).read() links = re.findall('a href=\"(.*\.dsc)\"', contents) if len(links) == 1 and \ subprocess.call(['dget', '-xu', 'http://launchpad.net%s' % links[0]]) == 0: print '\nSuccess!' else: raise ValueError, '\nFailed to fetch and extract the source. ' +\ 'Ensure that the package specified is a valid source ' +\ 'package name and that Launchpad is not down.' default_release = 'intrepid' if __name__ == '__main__': usage = "Usage: %prog [release]" optParser = OptionParser(usage) (options, args) = optParser.parse_args() if not args: print >> sys.stderr, "Need arguments." optParser.print_help() sys.exit(1) package = args[0] if len(args) == 2: # Custom distribution specified. release = args[1] else: release = os.getenv('DIST') or default_release # Correct-ish args, can proceed. # Check release by checking if Launchpad page exists try: urllib2.urlopen("https://launchpad.net/ubuntu/%s" % release) except urllib2.HTTPError: print >> sys.stderr, "The '%s' release does not appear to exist on " \ "Launchpad." % release sys.exit(1) # Check package exists. try: sourcePage = urllib2.urlopen("https://launchpad.net/ubuntu/%s/+source/%s" % \ (release, package)).read() m = re.search('"/ubuntu/%s/\+source/%s/(\d[^"]+)"' % (release, \ package.replace('+', '\+')), sourcePage) if not m: print >> sys.stderr, "The '%s' package does not appear to exist in " \ "the '%s' release." % (package, release) sys.exit(1) except urllib2.HTTPError, e: # Raised on 404 if e.code == 404: print >> sys.stderr, "The '%s' package does not appear to exist in " \ "the '%s' release." % (package, release) else: # Other code, probably LP malfunction print >> sys.stderr, "Error when checking Launchpad for package: %s " % e sys.exit(1) except urllib2.URLError, e: # Other error (NXDOMAIN, ...) (_, reason) = e.reason print >> sys.stderr, "Error when checking Launchpad for package: %s " % reason sys.exit(1) # All good - start downloading... try: print 'Attempting to get %s from release %s...' % \ (package, release.capitalize()) BackportFromLP(package, release) except ValueError, e: print 'Error when downloading package %s from release %s: %s.' % \ (package, release, e)