]> git.decadent.org.uk Git - dak.git/blob - dak/cruft_report.py
Add Not-For-Us checking to cruft_report
[dak.git] / dak / cruft_report.py
1 #!/usr/bin/env python
2
3 # Check for obsolete binary packages
4 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006  James Troup <james@nocrew.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 # ``If you're claiming that's a "problem" that needs to be "fixed",
23 #   you might as well write some letters to God about how unfair entropy
24 #   is while you're at it.'' -- 20020802143104.GA5628@azure.humbug.org.au
25
26 ## TODO:  fix NBS looping for version, implement Dubious NBS, fix up output of duplicate source package stuff, improve experimental ?, add overrides, avoid ANAIS for duplicated packages
27
28 ################################################################################
29
30 import commands, pg, os, sys, time, re
31 import apt_pkg
32 from daklib import database
33 from daklib import utils
34
35 ################################################################################
36
37 Cnf = None
38 projectB = None
39 suite = "unstable" # Default
40 suite_id = None
41 no_longer_in_suite = {}; # Really should be static to add_nbs, but I'm lazy
42
43 source_binaries = {}
44 source_versions = {}
45
46 ################################################################################
47
48 def usage(exit_code=0):
49     print """Usage: dak cruft-report
50 Check for obsolete or duplicated packages.
51
52   -h, --help                show this help and exit.
53   -m, --mode=MODE           chose the MODE to run in (full or daily).
54   -s, --suite=SUITE         check suite SUITE.
55   -w, --wanna-build-dump    where to find the copies of http://buildd.debian.org/stats/*.txt"""
56     sys.exit(exit_code)
57
58 ################################################################################
59
60 def add_nbs(nbs_d, source, version, package):
61     # Ensure the package is still in the suite (someone may have already removed it)
62     if no_longer_in_suite.has_key(package):
63         return
64     else:
65         q = projectB.query("SELECT b.id FROM binaries b, bin_associations ba WHERE ba.bin = b.id AND ba.suite = %s AND b.package = '%s' LIMIT 1" % (suite_id, package))
66         if not q.getresult():
67             no_longer_in_suite[package] = ""
68             return
69
70     nbs_d.setdefault(source, {})
71     nbs_d[source].setdefault(version, {})
72     nbs_d[source][version][package] = ""
73
74 ################################################################################
75
76 # Check for packages built on architectures they shouldn't be.
77 def do_anais(architecture, binaries_list, source):
78     if architecture == "any" or architecture == "all":
79         return ""
80
81     anais_output = ""
82     architectures = {}
83     for arch in architecture.split():
84         architectures[arch.strip()] = ""
85     for binary in binaries_list:
86         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))
87         ql = q.getresult()
88         versions = []
89         for i in ql:
90             arch = i[0]
91             version = i[1]
92             if architectures.has_key(arch):
93                 versions.append(version)
94         versions.sort(apt_pkg.VersionCompare)
95         if versions:
96             latest_version = versions.pop()
97         else:
98             latest_version = None
99         # Check for 'invalid' architectures
100         versions_d = {}
101         for i in ql:
102             arch = i[0]
103             version = i[1]
104             if not architectures.has_key(arch):
105                 versions_d.setdefault(version, [])
106                 versions_d[version].append(arch)
107
108         if versions_d != {}:
109             anais_output += "\n (*) %s_%s [%s]: %s\n" % (binary, latest_version, source, architecture)
110             versions = versions_d.keys()
111             versions.sort(apt_pkg.VersionCompare)
112             for version in versions:
113                 arches = versions_d[version]
114                 arches.sort()
115                 anais_output += "    o %s: %s\n" % (version, ", ".join(arches))
116     return anais_output
117
118
119 ################################################################################
120
121 # Check for out-of-date binaries on architectures that do not want to build that
122 # package any more, and have them listed as Not-For-Us
123 def do_nfu(nfu_packages):
124     output = ""
125     
126     a2p = {}
127
128     for architecture in nfu_packages:
129         a2p[architecture] = []
130         for (package,bver,sver) in nfu_packages[architecture]:
131             output += "  * [%s] does not want %s (binary %s, source %s)\n" % (architecture, package, bver, sver)
132             a2p[architecture].append(package)
133
134
135     if output:
136         print "Obsolete by Not-For-Us"
137         print "----------------------"
138         print
139         print output
140
141         print "Suggested commands:"
142         for architecture in a2p:
143             if a2p[architecture]:
144                 print (" dak rm -m \"[auto-cruft] NFU\" -s %s -a %s -b %s" % 
145                     (suite, architecture, " ".join(a2p[architecture])))
146         print
147
148 def parse_nfu(architecture):
149     # utils/hpodder_1.1.5.0: Not-For-Us [optional:out-of-date]
150     r = re.compile("^\w+/([^_]+)_.*: Not-For-Us")
151
152     ret = set()
153     
154     filename = "%s/%s-all.txt" % (Cnf["Cruft-Report::Options::Wanna-Build-Dump"], architecture)
155
156     # Not all architectures have a wanna-build dump, for example armel at the time of writing
157     if os.path.exists(filename):
158         f = utils.open_file(filename)
159         for line in f:
160             if line[0] == ' ':
161                 continue
162
163             m = r.match(line)
164             if m:
165                 ret.add(m.group(1))
166
167         f.close()
168     return ret
169
170 ################################################################################
171
172 def do_nviu():
173     experimental_id = database.get_suite_id("experimental")
174     if experimental_id == -1:
175         return
176     # Check for packages in experimental obsoleted by versions in unstable
177     q = projectB.query("""
178 SELECT s.source, s.version AS experimental, s2.version AS unstable
179   FROM src_associations sa, source s, source s2, src_associations sa2
180   WHERE sa.suite = %s AND sa2.suite = %d AND sa.source = s.id
181    AND sa2.source = s2.id AND s.source = s2.source
182    AND versioncmp(s.version, s2.version) < 0""" % (experimental_id,
183                                                    database.get_suite_id("unstable")))
184     ql = q.getresult()
185     if ql:
186         nviu_to_remove = []
187         print "Newer version in unstable"
188         print "-------------------------"
189         print
190         for i in ql:
191             (source, experimental_version, unstable_version) = i
192             print " o %s (%s, %s)" % (source, experimental_version, unstable_version)
193             nviu_to_remove.append(source)
194         print
195         print "Suggested command:"
196         print " dak rm -m \"[auto-cruft] NVIU\" -s experimental %s" % (" ".join(nviu_to_remove))
197         print
198
199 ################################################################################
200
201 def do_nbs(real_nbs):
202     output = "Not Built from Source\n"
203     output += "---------------------\n\n"
204
205     nbs_to_remove = []
206     nbs_keys = real_nbs.keys()
207     nbs_keys.sort()
208     for source in nbs_keys:
209         output += " * %s_%s builds: %s\n" % (source,
210                                        source_versions.get(source, "??"),
211                                        source_binaries.get(source, "(source does not exist)"))
212         output += "      but no longer builds:\n"
213         versions = real_nbs[source].keys()
214         versions.sort(apt_pkg.VersionCompare)
215         for version in versions:
216             packages = real_nbs[source][version].keys()
217             packages.sort()
218             for pkg in packages:
219                 nbs_to_remove.append(pkg)
220             output += "        o %s: %s\n" % (version, ", ".join(packages))
221
222         output += "\n"
223
224     if nbs_to_remove:
225         print output
226
227         print "Suggested command:"
228         print " dak rm -m \"[auto-cruft] NBS\" -s %s -b %s" % (suite, " ".join(nbs_to_remove))
229         print
230
231 ################################################################################
232
233 def do_dubious_nbs(dubious_nbs):
234     print "Dubious NBS"
235     print "-----------"
236     print
237
238     dubious_nbs_keys = dubious_nbs.keys()
239     dubious_nbs_keys.sort()
240     for source in dubious_nbs_keys:
241         print " * %s_%s builds: %s" % (source,
242                                        source_versions.get(source, "??"),
243                                        source_binaries.get(source, "(source does not exist)"))
244         print "      won't admit to building:"
245         versions = dubious_nbs[source].keys()
246         versions.sort(apt_pkg.VersionCompare)
247         for version in versions:
248             packages = dubious_nbs[source][version].keys()
249             packages.sort()
250             print "        o %s: %s" % (version, ", ".join(packages))
251
252         print
253
254 ################################################################################
255
256 def do_obsolete_source(duplicate_bins, bin2source):
257     obsolete = {}
258     for key in duplicate_bins.keys():
259         (source_a, source_b) = key.split('_')
260         for source in [ source_a, source_b ]:
261             if not obsolete.has_key(source):
262                 if not source_binaries.has_key(source):
263                     # Source has already been removed
264                     continue
265                 else:
266                     obsolete[source] = [ i.strip() for i in source_binaries[source].split(',') ]
267             for binary in duplicate_bins[key]:
268                 if bin2source.has_key(binary) and bin2source[binary]["source"] == source:
269                     continue
270                 if binary in obsolete[source]:
271                     obsolete[source].remove(binary)
272
273     to_remove = []
274     output = "Obsolete source package\n"
275     output += "-----------------------\n\n"
276     obsolete_keys = obsolete.keys()
277     obsolete_keys.sort()
278     for source in obsolete_keys:
279         if not obsolete[source]:
280             to_remove.append(source)
281             output += " * %s (%s)\n" % (source, source_versions[source])
282             for binary in [ i.strip() for i in source_binaries[source].split(',') ]:
283                 if bin2source.has_key(binary):
284                     output += "    o %s (%s) is built by %s.\n" \
285                           % (binary, bin2source[binary]["version"],
286                              bin2source[binary]["source"])
287                 else:
288                     output += "    o %s is not built.\n" % binary
289             output += "\n"
290
291     if to_remove:
292         print output
293
294         print "Suggested command:"
295         print " dak rm -S -p -m \"[auto-cruft] obsolete source package\" %s" % (" ".join(to_remove))
296         print
297
298 def get_suite_binaries():
299     # Initalize a large hash table of all binary packages
300     binaries = {}
301     before = time.time()
302
303     sys.stderr.write("[Getting a list of binary packages in %s..." % (suite))
304     q = projectB.query("SELECT distinct b.package FROM binaries b, bin_associations ba WHERE ba.suite = %s AND ba.bin = b.id" % (suite_id))
305     ql = q.getresult()
306     sys.stderr.write("done. (%d seconds)]\n" % (int(time.time()-before)))
307     for i in ql:
308         binaries[i[0]] = ""
309
310     return binaries
311
312 ################################################################################
313
314 def main ():
315     global Cnf, projectB, suite, suite_id, source_binaries, source_versions
316
317     Cnf = utils.get_conf()
318
319     Arguments = [('h',"help","Cruft-Report::Options::Help"),
320                  ('m',"mode","Cruft-Report::Options::Mode", "HasArg"),
321                  ('s',"suite","Cruft-Report::Options::Suite","HasArg"),
322                  ('w',"wanna-build-dump","Cruft-Report::Options::Wanna-Build-Dump","HasArg")]
323     for i in [ "help" ]:
324         if not Cnf.has_key("Cruft-Report::Options::%s" % (i)):
325             Cnf["Cruft-Report::Options::%s" % (i)] = ""
326     Cnf["Cruft-Report::Options::Suite"] = Cnf["Dinstall::DefaultSuite"]
327
328     if not Cnf.has_key("Cruft-Report::Options::Mode"):
329         Cnf["Cruft-Report::Options::Mode"] = "daily"
330
331     if not Cnf.has_key("Cruft-Report::Options::Wanna-Build-Dump"):
332         Cnf["Cruft-Report::Options::Wanna-Build-Dump"] = "./wanna-build-dump"
333
334     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
335
336     Options = Cnf.SubTree("Cruft-Report::Options")
337     if Options["Help"]:
338         usage()
339
340     # Set up checks based on mode
341     if Options["Mode"] == "daily":
342         checks = [ "nbs", "nviu", "obsolete source" ]
343     elif Options["Mode"] == "full":
344         checks = [ "nbs", "nviu", "obsolete source", "nfu", "dubious nbs", "bnb", "bms", "anais" ]
345     else:
346         utils.warn("%s is not a recognised mode - only 'full' or 'daily' are understood." % (Options["Mode"]))
347         usage(1)
348
349     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
350     database.init(Cnf, projectB)
351
352     bin_pkgs = {}
353     src_pkgs = {}
354     bin2source = {}
355     bins_in_suite = {}
356     nbs = {}
357     source_versions = {}
358
359     anais_output = ""
360     duplicate_bins = {}
361
362     nfu_packages = {}
363
364     suite = Options["Suite"]
365     suite_id = database.get_suite_id(suite)
366
367     bin_not_built = {}
368
369     if "bnb" in checks:
370         bins_in_suite = get_suite_binaries()
371
372     # Checks based on the Sources files
373     components = Cnf.ValueList("Suite::%s::Components" % (suite))
374     for component in components:
375         filename = "%s/dists/%s/%s/source/Sources.gz" % (Cnf["Dir::Root"], suite, component)
376         # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
377         temp_filename = utils.temp_filename()
378         (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
379         if (result != 0):
380             sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
381             sys.exit(result)
382         sources = utils.open_file(temp_filename)
383         Sources = apt_pkg.ParseTagFile(sources)
384         while Sources.Step():
385             source = Sources.Section.Find('Package')
386             source_version = Sources.Section.Find('Version')
387             architecture = Sources.Section.Find('Architecture')
388             binaries = Sources.Section.Find('Binary')
389             binaries_list = [ i.strip() for i in  binaries.split(',') ]
390
391             if "bnb" in checks:
392                 # Check for binaries not built on any architecture.
393                 for binary in binaries_list:
394                     if not bins_in_suite.has_key(binary):
395                         bin_not_built.setdefault(source, {})
396                         bin_not_built[source][binary] = ""
397
398             if "anais" in checks:
399                 anais_output += do_anais(architecture, binaries_list, source)
400
401             # Check for duplicated packages and build indices for checking "no source" later
402             source_index = component + '/' + source
403             if src_pkgs.has_key(source):
404                 print " %s is a duplicated source package (%s and %s)" % (source, source_index, src_pkgs[source])
405             src_pkgs[source] = source_index
406             for binary in binaries_list:
407                 if bin_pkgs.has_key(binary):
408                     key_list = [ source, bin_pkgs[binary] ]
409                     key_list.sort()
410                     key = '_'.join(key_list)
411                     duplicate_bins.setdefault(key, [])
412                     duplicate_bins[key].append(binary)
413                 bin_pkgs[binary] = source
414             source_binaries[source] = binaries
415             source_versions[source] = source_version
416
417         sources.close()
418         os.unlink(temp_filename)
419
420     # Checks based on the Packages files
421     check_components = components[:]
422     if suite != "experimental":
423         check_components.append('main/debian-installer');
424     for component in check_components:
425         architectures = filter(utils.real_arch, Cnf.ValueList("Suite::%s::Architectures" % (suite)))
426         for architecture in architectures:
427             filename = "%s/dists/%s/%s/binary-%s/Packages.gz" % (Cnf["Dir::Root"], suite, component, architecture)
428             # apt_pkg.ParseTagFile needs a real file handle
429             temp_filename = utils.temp_filename()
430             (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
431             if (result != 0):
432                 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
433                 sys.exit(result)
434
435             nfu_packages.setdefault(architecture,[])
436             nfu_entries = parse_nfu(architecture)
437
438             packages = utils.open_file(temp_filename)
439             Packages = apt_pkg.ParseTagFile(packages)
440             while Packages.Step():
441                 package = Packages.Section.Find('Package')
442                 source = Packages.Section.Find('Source', "")
443                 version = Packages.Section.Find('Version')
444                 if source == "":
445                     source = package
446                 if bin2source.has_key(package) and \
447                        apt_pkg.VersionCompare(version, bin2source[package]["version"]) > 0:
448                     bin2source[package]["version"] = version
449                     bin2source[package]["source"] = source
450                 else:
451                     bin2source[package] = {}
452                     bin2source[package]["version"] = version
453                     bin2source[package]["source"] = source
454                 if source.find("(") != -1:
455                     m = utils.re_extract_src_version.match(source)
456                     source = m.group(1)
457                     version = m.group(2)
458                 if not bin_pkgs.has_key(package):
459                     nbs.setdefault(source,{})
460                     nbs[source].setdefault(package, {})
461                     nbs[source][package][version] = ""
462                 else:
463                     previous_source = bin_pkgs[package]
464                     if previous_source != source:
465                         key_list = [ source, previous_source ]
466                         key_list.sort()
467                         key = '_'.join(key_list)
468                         duplicate_bins.setdefault(key, [])
469                         if package not in duplicate_bins[key]:
470                             duplicate_bins[key].append(package)
471                     if package in nfu_entries and \
472                         version != source_versions[source]: # only suggest to remove out-of-date packages
473                         nfu_packages[architecture].append((package,version,source_versions[source]))
474                     
475             packages.close()
476             os.unlink(temp_filename)
477
478     if "obsolete source" in checks:
479         do_obsolete_source(duplicate_bins, bin2source)
480
481     # Distinguish dubious (version numbers match) and 'real' NBS (they don't)
482     dubious_nbs = {}
483     real_nbs = {}
484     for source in nbs.keys():
485         for package in nbs[source].keys():
486             versions = nbs[source][package].keys()
487             versions.sort(apt_pkg.VersionCompare)
488             latest_version = versions.pop()
489             source_version = source_versions.get(source,"0")
490             if apt_pkg.VersionCompare(latest_version, source_version) == 0:
491                 add_nbs(dubious_nbs, source, latest_version, package)
492             else:
493                 add_nbs(real_nbs, source, latest_version, package)
494
495     if "nviu" in checks:
496         do_nviu()
497
498     if "nbs" in checks:
499         do_nbs(real_nbs)
500
501     ###
502
503     if Options["Mode"] == "full":
504         print "="*75
505         print
506
507     if "nfu" in checks:
508         do_nfu(nfu_packages)
509
510     if "bnb" in checks:
511         print "Unbuilt binary packages"
512         print "-----------------------"
513         print
514         keys = bin_not_built.keys()
515         keys.sort()
516         for source in keys:
517             binaries = bin_not_built[source].keys()
518             binaries.sort()
519             print " o %s: %s" % (source, ", ".join(binaries))
520         print
521
522     if "bms" in checks:
523         print "Built from multiple source packages"
524         print "-----------------------------------"
525         print
526         keys = duplicate_bins.keys()
527         keys.sort()
528         for key in keys:
529             (source_a, source_b) = key.split("_")
530             print " o %s & %s => %s" % (source_a, source_b, ", ".join(duplicate_bins[key]))
531         print
532
533     if "anais" in checks:
534         print "Architecture Not Allowed In Source"
535         print "----------------------------------"
536         print anais_output
537         print
538
539     if "dubious nbs" in checks:
540         do_dubious_nbs(dubious_nbs)
541
542
543 ################################################################################
544
545 if __name__ == '__main__':
546     main()