]> piware.de Git - bin.git/blob - buildd.py
postinst-setup: do not trash shadow group
[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 cookiefile = os.path.expanduser('~/.lpcookie')
21 if not os.path.exists(cookiefile):
22     try:
23         cookiefile = glob.glob(os.path.expanduser('~/.mozilla/*/*/cookies.txt'))[0]
24     except IndexError:
25         print >> sys.stderr, 'Could not find Firefox cookie file'
26         sys.exit(1)
27
28 # build HTML opener with cookie file
29 cj = cookielib.MozillaCookieJar()
30 cj.load(cookiefile)
31 urlopener = urllib2.build_opener()
32 urlopener.add_handler(urllib2.HTTPCookieProcessor(cj))
33
34 # find out the version in given release
35 try:
36         page = urlopener.open('https://launchpad.net/ubuntu/+source/' + package).read()
37 except urllib2.HTTPError:
38     print >> sys.stderr, 'This source does not appear to exist in Ubuntu'
39     sys.exit(1)
40
41 m = re.search('"/ubuntu/%s/\+source/%s/(\d[^"]+)"' % (release, package.replace('+', '\+')), page)
42 if not m:
43     print >> sys.stderr, 'Cannot find this source package in this release'
44     sys.exit(1)
45 version = m.group(1)
46 print 'Source version:', version
47
48 # parse out build URLs, states, and arches
49 buildstats = {}
50 page = urlopener.open('https://launchpad.net/ubuntu/+source/%s/%s' % (package, version))
51 url = page.geturl()
52 page = page.read()
53 for m in re.finditer('"/ubuntu/\+source/%s/%s(/\+build/\d+)"[^\n]+\n\s*(\w+).*?<span>(\w+)</span>.*?</a>\s*([^\n]+)\n' %
54     (package.replace('+', '\+'), version.replace('+', '\+')), page, re.S):
55     if m.group(2) == release:
56         print '%s: %s' % (m.group(3), m.group(4))
57         buildstats[url + m.group(1)] = [m.group(3).strip(), m.group(4).strip()]
58
59 # operations
60 if op == 'status':
61     sys.exit(0)
62
63 for build, (arch, status) in buildstats.iteritems():
64     if op == 'rescore':
65         if status == 'Needs building':
66             print 'rescoring', build, '(%s)' % arch
67             urlopener.open(build+'/+rescore', urlencode(
68                 {'SCORE': '5000', 'RESCORE': '1'}))
69     elif op == 'retry':
70         if status in ('Failed to build', 'Chroot problem', 'Failed to upload'):
71             print 'retrying', build, '(%s)' % arch
72             urlopener.open(build+'/+retry', urlencode(
73                 {'RETRY': '1'}))
74
75     else:
76         print >> sys.stderr, 'Invalid operation'
77         sys.exit(1)