--- /dev/null
+#!/usr/bin/python
+
+# CLI for Launchpad buildd operations
+# (C) 2007 Canonical Ltd.
+# Author: Martin Pitt <martin.pitt@canonical.com>
+
+import urllib2, cookielib, re, sys, glob, os.path
+from urllib import urlencode
+
+# parse arguments
+try:
+ (package, release, op) = sys.argv[1:]
+except ValueError:
+ print >> sys.stderr, '''Usage: %s <srcpackage> <release> <operation>
+
+operation: status | retry | rescore [priority (default 5000)]''' % sys.argv[0]
+ sys.exit(1)
+
+# find cookie file
+try:
+ cookiefile = glob.glob(os.path.expanduser('~/.mozilla/firefox/*/cookies.txt'))[0]
+except IndexError:
+ print >> sys.stderr, 'Could not find Firefox cookie file'
+ sys.exit(1)
+
+# build HTML opener with cookie file
+cj = cookielib.MozillaCookieJar()
+cj.load(cookiefile)
+urlopener = urllib2.build_opener()
+urlopener.add_handler(urllib2.HTTPCookieProcessor(cj))
+
+# find out the version in given release
+page = urlopener.open('https://launchpad.net/ubuntu/+source/' + package).read()
+m = re.search('"/ubuntu/%s/\+source/%s/(\d[^"]+)"' % (release, package), page)
+if not m:
+ print >> sys.stderr, 'Cannot find this package'
+ sys.exit(1)
+version = m.group(1)
+print 'Source version:', version
+
+# parse out build URLs, states, and arches
+buildstats = {}
+page = urlopener.open('https://launchpad.net/ubuntu/+source/%s/%s' % (package, version))
+url = page.geturl()
+page = page.read()
+for m in re.finditer('"/ubuntu/\+source/%s/%s(/\+build/\d+)"[^\n]+\n\s*(\w+).*?<span>(\w+)</span>.*?</a>\s*([^\n]+)\n' %
+ (package, version), page, re.S):
+ if m.group(2) == release:
+ print '%s: %s' % (m.group(3), m.group(4))
+ buildstats[url + m.group(1)] = [m.group(3).strip(), m.group(4).strip()]
+
+# operations
+if op == 'status':
+ sys.exit(0)
+
+for build, (arch, status) in buildstats.iteritems():
+ if op == 'rescore':
+ if status == 'Needs building':
+ print 'rescoring', build, '(%s)' % arch
+ urlopener.open(build+'/+rescore', urlencode(
+ {'SCORE': '5000', 'RESCORE': '1'}))
+ elif op == 'retry':
+ if status == 'Failed to build':
+ print 'retrying', build, '(%s)' % arch
+ urlopener.open(build+'/+retry', urlencode(
+ {'RETRY': '1'}))
+
+ else:
+ print >> sys.stderr, 'Invalid operation'
+ sys.exit(1)