]> git.decadent.org.uk Git - dak.git/blob - rene
fix incorrectly inverted lockfile check
[dak.git] / rene
1 #!/usr/bin/env python
2
3 # Check for obsolete binary packages
4 # Copyright (C) 2000, 2001, 2002  James Troup <james@nocrew.org>
5 # $Id: rene,v 1.17 2003-01-20 19:13:21 troup Exp $
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 ################################################################################
22
23 # ``If you're claiming that's a "problem" that needs to be "fixed",
24 #   you might as well write some letters to God about how unfair entropy
25 #   is while you're at it.'' -- 20020802143104.GA5628@azure.humbug.org.au
26
27 ## TODO:  fix NBS looping for version, implement Dubious NBS, fix up output of duplicate source package stuff, improve experimental ?, add support for non-US ?, add overrides, avoid ANAIS for duplicated packages
28
29 ################################################################################
30
31 import commands, pg, os, string, sys, tempfile, time;
32 import utils, db_access;
33 import apt_pkg;
34
35 ################################################################################
36
37 Cnf = None;
38 projectB = None;
39 suite_id = None;
40
41 ################################################################################
42
43 def usage(exit_code=0):
44     print """Usage: rene
45 Check for obsolete or duplicated packages.
46
47   -h, --help                show this help and exit."""
48     sys.exit(exit_code)
49
50 ################################################################################
51
52 def add_nbs(nbs_d, source, version, package):
53     if not nbs_d.has_key(source):
54         nbs_d[source] = {};
55     if not nbs_d[source].has_key(version):
56         nbs_d[source][version] = {};
57     nbs_d[source][version][package] = "";
58
59 ################################################################################
60
61 def main ():
62     global Cnf, projectB, suite_id;
63
64     Cnf = utils.get_conf();
65
66     Arguments = [('h',"help","Rene::Options::Help")];
67     for i in [ "help" ]:
68         if not Cnf.has_key("Rene::Options::%s" % (i)):
69             Cnf["Rene::Options::%s" % (i)] = "";
70
71     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv);
72
73     Options = Cnf.SubTree("Rene::Options")
74     if Options["Help"]:
75         usage();
76
77     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
78     db_access.init(Cnf, projectB);
79
80     bin_pkgs = {};
81     src_pkgs = {};
82     source_binaries = {};
83     bins_in_suite = {};
84     nbs = {};
85     source_versions = {};
86
87     anais_output = "";
88     duplicate_bins = {};
89
90     suite = "unstable";
91     suite_id = db_access.get_suite_id(suite);
92
93     bin_not_built = {};
94
95     # Initalize a large hash table of all binary packages
96     before = time.time();
97     sys.stderr.write("[Getting a list of binary packages in %s..." % (suite));
98     q = projectB.query("SELECT distinct b.package FROM binaries b, bin_associations ba WHERE ba.suite = 5 AND ba.bin = b.id");
99     ql = q.getresult();
100     sys.stderr.write("done. (%d seconds)]\n" % (int(time.time()-before)));
101     for i in ql:
102         bins_in_suite[i[0]] = "";
103
104     components = Cnf.ValueList("Suite::%s::Components" % (suite));
105     for component in components:
106         filename = "%s/dists/%s/%s/source/Sources.gz" % (Cnf["Dir::Root"], suite, component);
107         # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
108         temp_filename = tempfile.mktemp();
109         fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
110         os.close(fd);
111         (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename));
112         if (result != 0):
113             sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output));
114             sys.exit(result);
115         sources = utils.open_file(temp_filename);
116         Sources = apt_pkg.ParseTagFile(sources);
117         while Sources.Step():
118             source = Sources.Section.Find('Package');
119             source_version = Sources.Section.Find('Version');
120             architecture = Sources.Section.Find('Architecture');
121             binaries = Sources.Section.Find('Binary');
122             binaries_list = map(string.strip, binaries.split(','));
123
124             # Check for binaries not built on any architecture.
125             for binary in binaries_list:
126                 if not bins_in_suite.has_key(binary):
127                     if not bin_not_built.has_key(source):
128                         bin_not_built[source] = {};
129                     bin_not_built[source][binary] = "";
130
131             # Check for packages built on architectures they shouldn't be.
132             if architecture != "any" and architecture != "all":
133                 architectures = {};
134                 for arch in architecture.split():
135                     architectures[arch.strip()] = "";
136                 for binary in binaries_list:
137                     q = projectB.query("SELECT a.arch_string, b.version FROM binaries b, bin_associations ba, architecture a WHERE ba.suite = %s AND ba.bin = b.id AND b.architecture = a.id AND b.package = '%s'" % (suite_id, binary));
138                     ql = q.getresult();
139                     versions = [];
140                     for i in ql:
141                         arch = i[0];
142                         version = i[1];
143                         if architectures.has_key(arch):
144                             versions.append(version);
145                     versions.sort(apt_pkg.VersionCompare);
146                     if versions:
147                         latest_version = versions.pop()
148                     else:
149                         latest_version = None;
150                     # Check for 'invalid' architectures
151                     versions_d = {}
152                     for i in ql:
153                         arch = i[0];
154                         version = i[1];
155                         if not architectures.has_key(arch):
156                             if not versions_d.has_key(version):
157                                 versions_d[version] = [];
158                             versions_d[version].append(arch)
159
160                     if versions_d != {}:
161                         anais_output += "\n (*) %s_%s [%s]: %s\n" % (binary, latest_version, source, architecture);
162                         versions = versions_d.keys();
163                         versions.sort(apt_pkg.VersionCompare);
164                         for version in versions:
165                             arches = versions_d[version];
166                             arches.sort();
167                             anais_output += "    o %s: %s\n" % (version, ", ".join(arches));
168
169             # Check for duplicated packages and build indices for checking "no source" later
170             source_index = component + '/' + source;
171             if src_pkgs.has_key(source):
172                 print " %s is a duplicated source package (%s and %s)" % (source, source_index, src_pkgs[source]);
173             src_pkgs[source] = source_index;
174             for binary in binaries_list:
175                 if bin_pkgs.has_key(binary):
176                     key = "%s~%s" % (source, bin_pkgs[binary]);
177                     if not duplicate_bins.has_key(key):
178                         duplicate_bins[key] = [];
179                     duplicate_bins[key].append(binary);
180                 bin_pkgs[binary] = source;
181             source_binaries[source] = binaries;
182             source_versions[source] = source_version;
183
184         sources.close();
185         os.unlink(temp_filename);
186
187     for component in components:
188         architectures = filter(utils.real_arch, Cnf.ValueList("Suite::%s::Architectures" % (suite)));
189         for architecture in architectures:
190             filename = "%s/dists/%s/%s/binary-%s/Packages" % (Cnf["Dir::Root"], suite, component, architecture);
191             packages = utils.open_file(filename);
192             Packages = apt_pkg.ParseTagFile(packages);
193             while Packages.Step():
194                 package = Packages.Section.Find('Package');
195                 source = Packages.Section.Find('Source', "");
196                 version = Packages.Section.Find('Version');
197                 if source == "":
198                     source = package;
199                 if source.find("(") != -1:
200                     m = utils.re_extract_src_version.match(source);
201                     source = m.group(1);
202                     version = m.group(2);
203                 if not bin_pkgs.has_key(package):
204                     if not nbs.has_key(source):
205                         nbs[source] = {};
206                     if not nbs[source].has_key(package):
207                         nbs[source][package] = {};
208                     nbs[source][package][version] = "";
209             packages.close();
210
211     dubious_nbs = {};
212     real_nbs = {};
213     for source in nbs.keys():
214         for package in nbs[source].keys():
215             versions = nbs[source][package].keys();
216             versions.sort(apt_pkg.VersionCompare);
217             latest_version = versions.pop();
218             source_version = source_versions.get(source,"0");
219             if apt_pkg.VersionCompare(latest_version, source_version) == 0:
220                 add_nbs(dubious_nbs, source, latest_version, package);
221             else:
222                 add_nbs(real_nbs, source, latest_version, package);
223
224     # Check for packages in experimental obsoleted by versions in unstable
225     suite_id = db_access.get_suite_id("unstable");
226     q = projectB.query("""
227 SELECT s.source, s.version AS experimental, s2.version AS unstable
228   FROM src_associations sa, source s, source s2, src_associations sa2
229   WHERE sa.suite = 1 AND sa2.suite = %d AND sa.source = s.id
230    AND sa2.source = s2.id AND s.source = s2.source
231    AND versioncmp(s.version, s2.version) < 0""" % (suite_id));
232     ql = q.getresult();
233     if ql:
234         nviu_to_remove = [];
235         print "Newer version in unstable";
236         print "-------------------------";
237         print ;
238         for i in ql:
239             (source, experimental_version, unstable_version) = i;
240             print " o %s (%s, %s)" % (source, experimental_version, unstable_version);
241             nviu_to_remove.extend(source);
242         print
243         print "Suggested command:"
244         print " melanie -m \"[rene] NVIU\" -s experimental %s" % (" ".join(nviu_to_remove));
245         print
246
247     print "Not Built from Source";
248     print "---------------------";
249     print ;
250
251     nbs_to_remove = [];
252     nbs_keys = real_nbs.keys();
253     nbs_keys.sort();
254     for source in nbs_keys:
255         binaries = source_binaries.get(source, "(source does not exist)")
256         print " * %s_%s builds: %s" % (source,
257                                        source_versions.get(source, "??"),
258                                        source_binaries.get(source, "(source does not exist)"));
259         print "      but no longer builds:"
260         versions = real_nbs[source].keys();
261         versions.sort(apt_pkg.VersionCompare);
262         for version in versions:
263             packages = real_nbs[source][version].keys();
264             packages.sort();
265             for pkg in packages:
266                 # *cough* FIXME
267                 if pkg.find("pcmcia") == -1:
268                     nbs_to_remove.append(pkg);
269             print "        o %s: %s" % (version, ", ".join(packages));
270
271         print ;
272
273     print "Suggested command:"
274     print " melanie -m \"[rene] NBS\" -b %s" % (" ".join(nbs_to_remove));
275     print
276
277     print "="*75
278     print
279
280     print "Unbuilt binary packages";
281     print "-----------------------";
282     print
283     keys = bin_not_built.keys();
284     keys.sort();
285     for source in keys:
286         binaries = bin_not_built[source].keys();
287         binaries.sort();
288         print " o %s: %s" % (source, ", ".join(binaries));
289     print ;
290
291     print "Built from multiple source packages";
292     print "-----------------------------------";
293     print ;
294     keys = duplicate_bins.keys();
295     keys.sort();
296     for key in keys:
297         (source_a, source_b) = key.split("~");
298         print " o %s & %s => %s" % (source_a, source_b, ", ".join(duplicate_bins[key]));
299     print ;
300
301     print "Architecture Not Allowed In Source";
302     print "----------------------------------";
303     print anais_output;
304     print ;
305
306     print "Dubious NBS";
307     print "-----------";
308     print ;
309
310     dubious_nbs_keys = dubious_nbs.keys();
311     dubious_nbs_keys.sort();
312     for source in dubious_nbs_keys:
313         binaries = source_binaries.get(source, "(source does not exist)")
314         print " * %s_%s builds: %s" % (source,
315                                        source_versions.get(source, "??"),
316                                        source_binaries.get(source, "(source does not exist)"));
317         print "      won't admit to building:"
318         versions = dubious_nbs[source].keys();
319         versions.sort(apt_pkg.VersionCompare);
320         for version in versions:
321             packages = dubious_nbs[source][version].keys();
322             packages.sort();
323             print "        o %s: %s" % (version, ", ".join(packages));
324
325         print ;
326
327
328 ################################################################################
329
330 if __name__ == '__main__':
331     main()