From 70fc6fa3c74feda48b5b396d60445eca789c999a Mon Sep 17 00:00:00 2001 From: Jonathan Patrick Davies Date: Wed, 27 Aug 2008 10:22:08 +0100 Subject: [PATCH] * common.py: New functions: checkReleaseExists() and checkSourceExists(). * buildd and pull-lp-source: Adapt code to use new functions above. --- buildd | 20 +++++--------------- common.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ debian/changelog | 2 ++ pull-lp-source | 47 ++++++++-------------------------------------- 4 files changed, 64 insertions(+), 54 deletions(-) diff --git a/buildd b/buildd index 2e892d4..74f4d57 100755 --- a/buildd +++ b/buildd @@ -91,22 +91,12 @@ if os.environ.has_key('https_proxy'): launchpadCookie = common.prepareLaunchpadCookie() urlopener = common.setupLaunchpadUrlOpener(launchpadCookie) +# Check the release exists. +# Check release by checking if Launchpad page exists +common.checkReleaseExists(release) + # Find out the version in given release. -try: - page = urlopener.open('https://launchpad.net/ubuntu/+source/' + package).read() -except urllib2.HTTPError: - print >> sys.stderr, "The source package (%s) does not appear to exist " \ - "in Ubuntu." % package - sys.exit(1) - -m = re.search('"/ubuntu/%s/\+source/%s/(\d[^"]+)"' % (release, - package.replace('+', '\+')), page) -if not m: - print >> sys.stderr, "Unable to find this source package (%s) in this " \ - "release (%s)." % (package, release.capitalize()) - sys.exit(1) - -version = m.group(1) +(page, version) = common.checkSourceExists(package, release) # Output details. print "The source version for '%s' in %s is at %s." % (package, diff --git a/common.py b/common.py index 3c9cbfa..5f8ff5d 100644 --- a/common.py +++ b/common.py @@ -27,6 +27,7 @@ import cookielib import glob import os.path +import re import sys import urllib2 @@ -61,6 +62,54 @@ def readlist(filename, uniq=True): return items +def checkReleaseExists(release): + """ Check that an Ubuntu release exists by opening + https://launchpad.net/ubuntu/releaseName page on Launchpad. + + If an error is returned; the release does not exist. """ + 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) + +def checkSourceExists(package, release): + """ Check that a package exists by opening its + https://launchpad.net/ubuntu/+source/package page. + + Return the page and version in release. """ + try: + page = urllib2.urlopen('https://launchpad.net/ubuntu/+source/' + package).read() + + m = re.search('"/ubuntu/%s/\+source/%s/(\d[^"]+)"' % (release, + package.replace('+', '\+')), page) + if not m: + print >> sys.stderr, "Unable to find this source package (%s) in " \ + "this release (%s)." % (package, release.capitalize()) + sys.exit(1) + + except urllib2.HTTPError, error: # Raised on 404. + if error.code == 404: + print >> sys.stderr, "The source package (%s) does not appear to " \ + "exist in Ubuntu." % package + else: # Other error code, probably Launchpad malfunction. + print >> sys.stderr, "Error when checking Launchpad for package: " \ + "%s." % error + + sys.exit(1) # Exit. Error encountered. + + except urllib2.URLError, error: # Other error (NXDOMAIN, ...) + (_, reason) = error.reason + print >> sys.stderr, "Error when checking Launchpad for package: %s." % \ + reason + sys.exit(1) + + # Get package version. + version = m.group(1) + + return page, version + def prepareLaunchpadCookie(): """ Search for a cookie file in the places as defined by try_globs. We shall use this cookie for authentication with Launchpad. """ diff --git a/debian/changelog b/debian/changelog index e40d2ee..2ebd6fa 100644 --- a/debian/changelog +++ b/debian/changelog @@ -3,6 +3,8 @@ ubuntu-dev-tools (0.42ubuntu1) intrepid; urgency=low [Jonathan Patrick Davies] * requestsync: Exit when connecting to Launchpad fails. * doc/requestsync.1: Document new -d flag. + * common.py: New functions: checkReleaseExists() and checkSourceExists(). + * buildd and pull-lp-source: Adapt code to use new functions above. [ Jelmer Vernooij ] * requestsync: Add -d option to allow overriding the Debian distro to sync diff --git a/pull-lp-source b/pull-lp-source index abf338c..2482585 100755 --- a/pull-lp-source +++ b/pull-lp-source @@ -32,6 +32,10 @@ import sys import urllib2 from optparse import OptionParser +# Ubuntu-dev-tools modules. +sys.path.append("/usr/share/ubuntu-dev-tools") +import common + class BackportFromLP: def __getitem__(self, name): @@ -67,54 +71,19 @@ if __name__ == '__main__': optParser.print_help() sys.exit(1) - package = args[0] + package = str(args[0]).lower() if len(args) == 2: # Custom distribution specified. - release = args[1] + release = str(args[1]).lower() 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) + common.checkReleaseExists(release) # 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) + common.checkSourceExists(package, release) # All good - start downloading... try: