3 """ Check for obsolete binary packages """
4 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006 James Troup <james@nocrew.org>
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.
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.
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
20 ################################################################################
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
26 ## TODO: fix NBS looping for version, implement Dubious NBS, fix up output of
27 ## duplicate source package stuff, improve experimental ?, add overrides,
28 ## avoid ANAIS for duplicated packages
30 ################################################################################
32 import commands, os, sys, re
35 from daklib.config import Config
36 from daklib.dbconn import *
37 from daklib import utils
38 from daklib.regexes import re_extract_src_version
40 ################################################################################
42 no_longer_in_suite = {}; # Really should be static to add_nbs, but I'm lazy
47 ################################################################################
49 def usage(exit_code=0):
50 print """Usage: dak cruft-report
51 Check for obsolete or duplicated packages.
53 -h, --help show this help and exit.
54 -m, --mode=MODE chose the MODE to run in (full or daily).
55 -s, --suite=SUITE check suite SUITE.
56 -w, --wanna-build-dump where to find the copies of http://buildd.debian.org/stats/*.txt"""
59 ################################################################################
61 def add_nbs(nbs_d, source, version, package, suite_id, session):
62 # Ensure the package is still in the suite (someone may have already removed it)
63 if no_longer_in_suite.has_key(package):
66 q = session.execute("""SELECT b.id FROM binaries b, bin_associations ba
67 WHERE ba.bin = b.id AND ba.suite = :suite_id
68 AND b.package = :package LIMIT 1""", {'suite_id': suite_id,
71 no_longer_in_suite[package] = ""
74 nbs_d.setdefault(source, {})
75 nbs_d[source].setdefault(version, {})
76 nbs_d[source][version][package] = ""
78 ################################################################################
80 # Check for packages built on architectures they shouldn't be.
81 def do_anais(architecture, binaries_list, source, session):
82 if architecture == "any" or architecture == "all":
87 for arch in architecture.split():
88 architectures[arch.strip()] = ""
89 for binary in binaries_list:
90 q = session.execute("""SELECT a.arch_string, b.version
91 FROM binaries b, bin_associations ba, architecture a
92 WHERE ba.suite = :suiteid AND ba.bin = b.id
93 AND b.architecture = a.id AND b.package = :package""",
94 {'suiteid': suite_id, 'package': binary})
100 if architectures.has_key(arch):
101 versions.append(version)
102 versions.sort(apt_pkg.VersionCompare)
104 latest_version = versions.pop()
106 latest_version = None
107 # Check for 'invalid' architectures
112 if not architectures.has_key(arch):
113 versions_d.setdefault(version, [])
114 versions_d[version].append(arch)
117 anais_output += "\n (*) %s_%s [%s]: %s\n" % (binary, latest_version, source, architecture)
118 versions = versions_d.keys()
119 versions.sort(apt_pkg.VersionCompare)
120 for version in versions:
121 arches = versions_d[version]
123 anais_output += " o %s: %s\n" % (version, ", ".join(arches))
127 ################################################################################
129 # Check for out-of-date binaries on architectures that do not want to build that
130 # package any more, and have them listed as Not-For-Us
131 def do_nfu(nfu_packages):
136 for architecture in nfu_packages:
137 a2p[architecture] = []
138 for (package,bver,sver) in nfu_packages[architecture]:
139 output += " * [%s] does not want %s (binary %s, source %s)\n" % (architecture, package, bver, sver)
140 a2p[architecture].append(package)
144 print "Obsolete by Not-For-Us"
145 print "----------------------"
149 print "Suggested commands:"
150 for architecture in a2p:
151 if a2p[architecture]:
152 print (" dak rm -m \"[auto-cruft] NFU\" -s %s -a %s -b %s" %
153 (suite, architecture, " ".join(a2p[architecture])))
156 def parse_nfu(architecture):
158 # utils/hpodder_1.1.5.0: Not-For-Us [optional:out-of-date]
159 r = re.compile("^\w+/([^_]+)_.*: Not-For-Us")
163 filename = "%s/%s-all.txt" % (cnf["Cruft-Report::Options::Wanna-Build-Dump"], architecture)
165 # Not all architectures may have a wanna-build dump, so we want to ignore missin
167 if os.path.exists(filename):
168 f = utils.open_file(filename)
179 utils.warn("No wanna-build dump file for architecture %s" % architecture)
182 ################################################################################
184 def do_newer_version(lowersuite_name, highersuite_name, code, session):
185 lowersuite = get_suite(lowersuite_name, session)
189 highersuite = get_suite(highersuite_name, session)
193 # Check for packages in $highersuite obsoleted by versions in $lowersuite
194 q = session.execute("""
195 SELECT s.source, s.version AS lower, s2.version AS higher
196 FROM src_associations sa, source s, source s2, src_associations sa2
197 WHERE sa.suite = :highersuite_id AND sa2.suite = :lowersuite_id AND sa.source = s.id
198 AND sa2.source = s2.id AND s.source = s2.source
199 AND s.version < s2.version""", {'lowersuite_id': lowersuite.suite_id,
200 'highersuite_id': highersuite.suite_id})
204 print "Newer version in %s" % lowersuite.suite_name
205 print "-----------------" + "-" * len(lowersuite.suite_name)
208 (source, higher_version, lower_version) = i
209 print " o %s (%s, %s)" % (source, higher_version, lower_version)
210 nv_to_remove.append(source)
212 print "Suggested command:"
213 print " dak rm -m \"[auto-cruft] %s\" -s %s %s" % (code, highersuite.suite_name,
214 " ".join(nv_to_remove))
217 ################################################################################
219 def do_nbs(real_nbs):
220 output = "Not Built from Source\n"
221 output += "---------------------\n\n"
224 nbs_keys = real_nbs.keys()
226 for source in nbs_keys:
227 output += " * %s_%s builds: %s\n" % (source,
228 source_versions.get(source, "??"),
229 source_binaries.get(source, "(source does not exist)"))
230 output += " but no longer builds:\n"
231 versions = real_nbs[source].keys()
232 versions.sort(apt_pkg.VersionCompare)
234 for version in versions:
235 packages = real_nbs[source][version].keys()
237 all_packages.extend(packages)
238 output += " o %s: %s\n" % (version, ", ".join(packages))
241 cmd_output += " dak rm -m \"[auto-cruft] NBS (was built by %s)\" -s %s -b %s\n\n" % (source, suite.suite_name, " ".join(all_packages))
247 print "Suggested commands:\n"
250 ################################################################################
252 def do_dubious_nbs(dubious_nbs):
257 dubious_nbs_keys = dubious_nbs.keys()
258 dubious_nbs_keys.sort()
259 for source in dubious_nbs_keys:
260 print " * %s_%s builds: %s" % (source,
261 source_versions.get(source, "??"),
262 source_binaries.get(source, "(source does not exist)"))
263 print " won't admit to building:"
264 versions = dubious_nbs[source].keys()
265 versions.sort(apt_pkg.VersionCompare)
266 for version in versions:
267 packages = dubious_nbs[source][version].keys()
269 print " o %s: %s" % (version, ", ".join(packages))
273 ################################################################################
275 def do_obsolete_source(duplicate_bins, bin2source):
277 for key in duplicate_bins.keys():
278 (source_a, source_b) = key.split('_')
279 for source in [ source_a, source_b ]:
280 if not obsolete.has_key(source):
281 if not source_binaries.has_key(source):
282 # Source has already been removed
285 obsolete[source] = [ i.strip() for i in source_binaries[source].split(',') ]
286 for binary in duplicate_bins[key]:
287 if bin2source.has_key(binary) and bin2source[binary]["source"] == source:
289 if binary in obsolete[source]:
290 obsolete[source].remove(binary)
293 output = "Obsolete source package\n"
294 output += "-----------------------\n\n"
295 obsolete_keys = obsolete.keys()
297 for source in obsolete_keys:
298 if not obsolete[source]:
299 to_remove.append(source)
300 output += " * %s (%s)\n" % (source, source_versions[source])
301 for binary in [ i.strip() for i in source_binaries[source].split(',') ]:
302 if bin2source.has_key(binary):
303 output += " o %s (%s) is built by %s.\n" \
304 % (binary, bin2source[binary]["version"],
305 bin2source[binary]["source"])
307 output += " o %s is not built.\n" % binary
313 print "Suggested command:"
314 print " dak rm -S -p -m \"[auto-cruft] obsolete source package\" %s" % (" ".join(to_remove))
317 def get_suite_binaries(suite, session):
318 # Initalize a large hash table of all binary packages
321 print "Getting a list of binary packages in %s..." % suite.suite_name
322 q = session.execute("""SELECT distinct b.package
323 FROM binaries b, bin_associations ba
324 WHERE ba.suite = :suiteid AND ba.bin = b.id""",
325 {'suiteid': suite.suite_id})
326 for i in q.fetchall():
331 ################################################################################
334 global suite, suite_id, source_binaries, source_versions
338 Arguments = [('h',"help","Cruft-Report::Options::Help"),
339 ('m',"mode","Cruft-Report::Options::Mode", "HasArg"),
340 ('s',"suite","Cruft-Report::Options::Suite","HasArg"),
341 ('w',"wanna-build-dump","Cruft-Report::Options::Wanna-Build-Dump","HasArg")]
343 if not cnf.has_key("Cruft-Report::Options::%s" % (i)):
344 cnf["Cruft-Report::Options::%s" % (i)] = ""
345 cnf["Cruft-Report::Options::Suite"] = cnf["Dinstall::DefaultSuite"]
347 if not cnf.has_key("Cruft-Report::Options::Mode"):
348 cnf["Cruft-Report::Options::Mode"] = "daily"
350 if not cnf.has_key("Cruft-Report::Options::Wanna-Build-Dump"):
351 cnf["Cruft-Report::Options::Wanna-Build-Dump"] = "/srv/ftp.debian.org/scripts/nfu"
353 apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
355 Options = cnf.SubTree("Cruft-Report::Options")
359 # Set up checks based on mode
360 if Options["Mode"] == "daily":
361 checks = [ "nbs", "nviu", "nvit", "obsolete source" ]
362 elif Options["Mode"] == "full":
363 checks = [ "nbs", "nviu", "nvit", "obsolete source", "nfu", "dubious nbs", "bnb", "bms", "anais" ]
365 utils.warn("%s is not a recognised mode - only 'full' or 'daily' are understood." % (Options["Mode"]))
368 session = DBConn().session()
382 suite = get_suite(Options["Suite"].lower(), session)
383 suite_id = suite.suite_id
384 suite_name = suite.suite_name.lower()
389 bins_in_suite = get_suite_binaries(suite_name, session)
391 # Checks based on the Sources files
392 components = cnf.ValueList("Suite::%s::Components" % (suite_name))
393 for component in components:
394 filename = "%s/dists/%s/%s/source/Sources.gz" % (cnf["Dir::Root"], suite_name, component)
395 # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
396 (fd, temp_filename) = utils.temp_filename()
397 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
399 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
401 sources = utils.open_file(temp_filename)
402 Sources = apt_pkg.ParseTagFile(sources)
403 while Sources.Step():
404 source = Sources.Section.Find('Package')
405 source_version = Sources.Section.Find('Version')
406 architecture = Sources.Section.Find('Architecture')
407 binaries = Sources.Section.Find('Binary')
408 binaries_list = [ i.strip() for i in binaries.split(',') ]
411 # Check for binaries not built on any architecture.
412 for binary in binaries_list:
413 if not bins_in_suite.has_key(binary):
414 bin_not_built.setdefault(source, {})
415 bin_not_built[source][binary] = ""
417 if "anais" in checks:
418 anais_output += do_anais(architecture, binaries_list, source, session)
420 # Check for duplicated packages and build indices for checking "no source" later
421 source_index = component + '/' + source
422 if src_pkgs.has_key(source):
423 print " %s is a duplicated source package (%s and %s)" % (source, source_index, src_pkgs[source])
424 src_pkgs[source] = source_index
425 for binary in binaries_list:
426 if bin_pkgs.has_key(binary):
427 key_list = [ source, bin_pkgs[binary] ]
429 key = '_'.join(key_list)
430 duplicate_bins.setdefault(key, [])
431 duplicate_bins[key].append(binary)
432 bin_pkgs[binary] = source
433 source_binaries[source] = binaries
434 source_versions[source] = source_version
437 os.unlink(temp_filename)
439 # Checks based on the Packages files
440 check_components = components[:]
441 if suite_name != "experimental":
442 check_components.append('main/debian-installer');
444 for component in check_components:
445 architectures = [ a.arch_string for a in get_suite_architectures(suite_name,
446 skipsrc=True, skipall=True,
448 for architecture in architectures:
449 if component == 'main/debian-installer' and re.match("kfreebsd", architecture):
451 filename = "%s/dists/%s/%s/binary-%s/Packages.gz" % (cnf["Dir::Root"], suite_name, component, architecture)
452 # apt_pkg.ParseTagFile needs a real file handle
453 (fd, temp_filename) = utils.temp_filename()
454 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
456 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
460 nfu_packages.setdefault(architecture,[])
461 nfu_entries = parse_nfu(architecture)
463 packages = utils.open_file(temp_filename)
464 Packages = apt_pkg.ParseTagFile(packages)
465 while Packages.Step():
466 package = Packages.Section.Find('Package')
467 source = Packages.Section.Find('Source', "")
468 version = Packages.Section.Find('Version')
471 if bin2source.has_key(package) and \
472 apt_pkg.VersionCompare(version, bin2source[package]["version"]) > 0:
473 bin2source[package]["version"] = version
474 bin2source[package]["source"] = source
476 bin2source[package] = {}
477 bin2source[package]["version"] = version
478 bin2source[package]["source"] = source
479 if source.find("(") != -1:
480 m = re_extract_src_version.match(source)
483 if not bin_pkgs.has_key(package):
484 nbs.setdefault(source,{})
485 nbs[source].setdefault(package, {})
486 nbs[source][package][version] = ""
488 previous_source = bin_pkgs[package]
489 if previous_source != source:
490 key_list = [ source, previous_source ]
492 key = '_'.join(key_list)
493 duplicate_bins.setdefault(key, [])
494 if package not in duplicate_bins[key]:
495 duplicate_bins[key].append(package)
497 if package in nfu_entries and \
498 version != source_versions[source]: # only suggest to remove out-of-date packages
499 nfu_packages[architecture].append((package,version,source_versions[source]))
502 os.unlink(temp_filename)
504 if "obsolete source" in checks:
505 do_obsolete_source(duplicate_bins, bin2source)
507 # Distinguish dubious (version numbers match) and 'real' NBS (they don't)
510 for source in nbs.keys():
511 for package in nbs[source].keys():
512 versions = nbs[source][package].keys()
513 versions.sort(apt_pkg.VersionCompare)
514 latest_version = versions.pop()
515 source_version = source_versions.get(source,"0")
516 if apt_pkg.VersionCompare(latest_version, source_version) == 0:
517 add_nbs(dubious_nbs, source, latest_version, package, suite_id, session)
519 add_nbs(real_nbs, source, latest_version, package, suite_id, session)
522 do_newer_version('unstable', 'experimental', 'NVIU', session)
525 do_newer_version('testing', 'testing-proposed-updates', 'NVIT', session)
532 if Options["Mode"] == "full":
540 print "Unbuilt binary packages"
541 print "-----------------------"
543 keys = bin_not_built.keys()
546 binaries = bin_not_built[source].keys()
548 print " o %s: %s" % (source, ", ".join(binaries))
552 print "Built from multiple source packages"
553 print "-----------------------------------"
555 keys = duplicate_bins.keys()
558 (source_a, source_b) = key.split("_")
559 print " o %s & %s => %s" % (source_a, source_b, ", ".join(duplicate_bins[key]))
562 if "anais" in checks:
563 print "Architecture Not Allowed In Source"
564 print "----------------------------------"
568 if "dubious nbs" in checks:
569 do_dubious_nbs(dubious_nbs)
572 ################################################################################
574 if __name__ == '__main__':