]> git.decadent.org.uk Git - dak.git/blob - dak/override_disparity.py
Merge remote-tracking branch 'nthykier/auto-decruft'
[dak.git] / dak / override_disparity.py
1 #!/usr/bin/env python
2
3 """
4 Generate a list of override disparities
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2010 Luca Falavigna <dktrkranz@debian.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 ################################################################################
26
27 # <adsb> Yay bugzilla *sigh*
28 # <phil> :)
29 # <Ganneff> quick, replace the bts with it
30 # * jcristau replaces dak with soyuz
31 # <adsb> and expects Ganneff to look after it?
32 # <jcristau> nah, elmo can do that
33 # * jcristau hides
34
35 ################################################################################
36
37 import os
38 import sys
39 import apt_pkg
40 import yaml
41
42 from daklib.config import Config
43 from daklib.dbconn import *
44 from daklib import utils
45
46 ################################################################################
47
48 def usage (exit_code=0):
49     print """Generate a list of override disparities
50
51        Usage:
52        dak override-disparity [-f <file>] [ -p <package> ] [ -s <suite> ]
53
54 Options:
55
56   -h, --help                show this help and exit
57   -f, --file                store output into given file
58   -p, --package             limit check on given package only
59   -s, --suite               choose suite to look for (default: unstable)"""
60
61     sys.exit(exit_code)
62
63 def main():
64     cnf = Config()
65     Arguments = [('h','help','Override-Disparity::Options::Help'),
66                  ('f','file','Override-Disparity::Options::File','HasArg'),
67                  ('s','suite','Override-Disparity::Options::Suite','HasArg'),
68                  ('p','package','Override-Disparity::Options::Package','HasArg')]
69
70     for i in ['help', 'package']:
71         if not cnf.has_key('Override-Disparity::Options::%s' % (i)):
72             cnf['Override-Disparity::Options::%s' % (i)] = ''
73     if not cnf.has_key('Override-Disparity::Options::Suite'):
74         cnf['Override-Disparity::Options::Suite'] = 'unstable'
75
76     apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
77     Options = cnf.subtree('Override-Disparity::Options')
78
79     if Options['help']:
80         usage()
81
82     depends = {}
83     session = DBConn().session()
84     suite_name = Options['suite']
85     suite = get_suite(suite_name, session)
86     if suite is None:
87         utils.fubar("Unknown suite '{0}'".format(suite_name))
88     components = get_component_names(session)
89     arches = set([x.arch_string for x in get_suite_architectures(suite_name)])
90     arches -= set(['source', 'all'])
91     for arch in arches:
92         for component in components:
93             Packages = utils.get_packages_from_ftp(suite.archive.path, suite_name, component, arch)
94             while Packages.step():
95                 package = Packages.section.find('Package')
96                 dep_list = Packages.section.find('Depends')
97                 if Options['package'] and package != Options['package']:
98                     continue
99                 if dep_list:
100                     for d in apt_pkg.parse_depends(dep_list):
101                         for i in d:
102                             if not depends.has_key(package):
103                                 depends[package] = set()
104                             depends[package].add(i[0])
105
106     priorities = {}
107     query = """SELECT DISTINCT o.package, p.level, p.priority, m.name
108                FROM override o
109                JOIN suite s ON s.id = o.suite
110                JOIN priority p ON p.id = o.priority
111                JOIN binaries b ON b.package = o.package
112                JOIN maintainer m ON m.id = b.maintainer
113                JOIN bin_associations ba ON ba.bin = b.id
114                WHERE s.suite_name = '%s'
115                AND ba.suite = s.id
116                AND p.level <> 0""" % suite_name
117     packages = session.execute(query)
118
119     out = {}
120     if Options.has_key('file'):
121         outfile = file(os.path.expanduser(Options['file']), 'w')
122     else:
123         outfile = sys.stdout
124     for p in packages:
125         priorities[p[0]] = [p[1], p[2], p[3], True]
126     for d in sorted(depends.keys()):
127         for p in depends[d]:
128             if priorities.has_key(d) and priorities.has_key(p):
129                 if priorities[d][0] < priorities[p][0]:
130                      if priorities[d][3]:
131                          if not out.has_key(d):
132                              out[d] = {}
133                          out[d]['priority'] = priorities[d][1]
134                          out[d]['maintainer'] = unicode(priorities[d][2], 'utf-8')
135                          out[d]['priority'] = priorities[d][1]
136                          priorities[d][3] = False
137                      if not out[d].has_key('dependency'):
138                          out[d]['dependency'] = {}
139                      out[d]['dependency'][p] = priorities[p][1]
140     yaml.safe_dump(out, outfile, default_flow_style=False)
141     if Options.has_key('file'):
142         outfile.close()
143
144 if __name__ == '__main__':
145     main()