]> piware.de Git - bin.git/blob - buildd.py
update postinst-setup
[bin.git] / buildd.py
1 #!/usr/bin/python
2
3 # CLI for Launchpad buildd operations
4 # (C) 2007 Canonical Ltd.
5 # Author: Martin Pitt <martin.pitt@canonical.com>
6
7 import urllib2, cookielib, re, sys, glob, os.path
8 from urllib import urlencode
9
10 # parse arguments
11 try:
12     (package, release, op) = sys.argv[1:]
13 except ValueError:
14     print >> sys.stderr, '''Usage: %s <srcpackage> <release> <operation>
15     
16 operation: status | retry | rescore [priority (default 5000)]''' % sys.argv[0]
17     sys.exit(1)
18
19 # find cookie file
20 try:
21     cookiefile = glob.glob(os.path.expanduser('~/.mozilla/*/*/cookies.txt'))[0]
22 except IndexError:
23     print >> sys.stderr, 'Could not find Firefox cookie file'
24     sys.exit(1)
25
26 # build HTML opener with cookie file
27 cj = cookielib.MozillaCookieJar()
28 cj.load(cookiefile)
29 urlopener = urllib2.build_opener()
30 urlopener.add_handler(urllib2.HTTPCookieProcessor(cj))
31
32 # find out the version in given release
33 try:
34         page = urlopener.open('https://launchpad.net/ubuntu/+source/' + package).read()
35 except urllib2.HTTPError:
36     print >> sys.stderr, 'This source does not appear to exist in Ubuntu'
37     sys.exit(1)
38
39 m = re.search('"/ubuntu/%s/\+source/%s/(\d[^"]+)"' % (release, package.replace('+', '\+')), page)
40 if not m:
41     print >> sys.stderr, 'Cannot find this source package in this release'
42     sys.exit(1)
43 version = m.group(1)
44 print 'Source version:', version
45
46 # parse out build URLs, states, and arches
47 buildstats = {}
48 page = urlopener.open('https://launchpad.net/ubuntu/+source/%s/%s' % (package, version))
49 url = page.geturl()
50 page = page.read()
51 for m in re.finditer('"/ubuntu/\+source/%s/%s(/\+build/\d+)"[^\n]+\n\s*(\w+).*?<span>(\w+)</span>.*?</a>\s*([^\n]+)\n' %
52     (package.replace('+', '\+'), version.replace('+', '\+')), page, re.S):
53     if m.group(2) == release:
54         print '%s: %s' % (m.group(3), m.group(4))
55         buildstats[url + m.group(1)] = [m.group(3).strip(), m.group(4).strip()]
56
57 # operations
58 if op == 'status':
59     sys.exit(0)
60
61 for build, (arch, status) in buildstats.iteritems():
62     if op == 'rescore':
63         if status == 'Needs building':
64             print 'rescoring', build, '(%s)' % arch
65             urlopener.open(build+'/+rescore', urlencode(
66                 {'SCORE': '5000', 'RESCORE': '1'}))
67     elif op == 'retry':
68         if status in ('Failed to build', 'Chroot problem', 'Failed to upload'):
69             print 'retrying', build, '(%s)' % arch
70             urlopener.open(build+'/+retry', urlencode(
71                 {'RETRY': '1'}))
72
73     else:
74         print >> sys.stderr, 'Invalid operation'
75         sys.exit(1)