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 duplicate source package stuff, improve experimental ?, add overrides, avoid ANAIS for duplicated packages
28 ################################################################################
30 import commands, pg, os, sys, time, re
32 from daklib import database
33 from daklib import utils
34 from daklib.regexes import re_extract_src_version
36 ################################################################################
40 suite = "unstable" # Default
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):
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 = 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))
68 no_longer_in_suite[package] = ""
71 nbs_d.setdefault(source, {})
72 nbs_d[source].setdefault(version, {})
73 nbs_d[source][version][package] = ""
75 ################################################################################
77 # Check for packages built on architectures they shouldn't be.
78 def do_anais(architecture, binaries_list, source):
79 if architecture == "any" or architecture == "all":
84 for arch in architecture.split():
85 architectures[arch.strip()] = ""
86 for binary in binaries_list:
87 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))
93 if architectures.has_key(arch):
94 versions.append(version)
95 versions.sort(apt_pkg.VersionCompare)
97 latest_version = versions.pop()
100 # Check for 'invalid' architectures
105 if not architectures.has_key(arch):
106 versions_d.setdefault(version, [])
107 versions_d[version].append(arch)
110 anais_output += "\n (*) %s_%s [%s]: %s\n" % (binary, latest_version, source, architecture)
111 versions = versions_d.keys()
112 versions.sort(apt_pkg.VersionCompare)
113 for version in versions:
114 arches = versions_d[version]
116 anais_output += " o %s: %s\n" % (version, ", ".join(arches))
120 ################################################################################
122 # Check for out-of-date binaries on architectures that do not want to build that
123 # package any more, and have them listed as Not-For-Us
124 def do_nfu(nfu_packages):
129 for architecture in nfu_packages:
130 a2p[architecture] = []
131 for (package,bver,sver) in nfu_packages[architecture]:
132 output += " * [%s] does not want %s (binary %s, source %s)\n" % (architecture, package, bver, sver)
133 a2p[architecture].append(package)
137 print "Obsolete by Not-For-Us"
138 print "----------------------"
142 print "Suggested commands:"
143 for architecture in a2p:
144 if a2p[architecture]:
145 print (" dak rm -m \"[auto-cruft] NFU\" -s %s -a %s -b %s" %
146 (suite, architecture, " ".join(a2p[architecture])))
149 def parse_nfu(architecture):
150 # utils/hpodder_1.1.5.0: Not-For-Us [optional:out-of-date]
151 r = re.compile("^\w+/([^_]+)_.*: Not-For-Us")
155 filename = "%s/%s-all.txt" % (Cnf["Cruft-Report::Options::Wanna-Build-Dump"], architecture)
157 # Not all architectures may have a wanna-build dump, so we want to ignore missin
159 if os.path.exists(filename):
160 f = utils.open_file(filename)
171 utils.warn("No wanna-build dump file for architecture %s", architecture)
174 ################################################################################
177 experimental_id = database.get_suite_id("experimental")
178 if experimental_id == -1:
180 # Check for packages in experimental obsoleted by versions in unstable
181 q = projectB.query("""
182 SELECT s.source, s.version AS experimental, s2.version AS unstable
183 FROM src_associations sa, source s, source s2, src_associations sa2
184 WHERE sa.suite = %s AND sa2.suite = %d AND sa.source = s.id
185 AND sa2.source = s2.id AND s.source = s2.source
186 AND s.version < s2.version""" % (experimental_id,
187 database.get_suite_id("unstable")))
191 print "Newer version in unstable"
192 print "-------------------------"
195 (source, experimental_version, unstable_version) = i
196 print " o %s (%s, %s)" % (source, experimental_version, unstable_version)
197 nviu_to_remove.append(source)
199 print "Suggested command:"
200 print " dak rm -m \"[auto-cruft] NVIU\" -s experimental %s" % (" ".join(nviu_to_remove))
203 ################################################################################
205 def do_nbs(real_nbs):
206 output = "Not Built from Source\n"
207 output += "---------------------\n\n"
210 nbs_keys = real_nbs.keys()
212 for source in nbs_keys:
213 output += " * %s_%s builds: %s\n" % (source,
214 source_versions.get(source, "??"),
215 source_binaries.get(source, "(source does not exist)"))
216 output += " but no longer builds:\n"
217 versions = real_nbs[source].keys()
218 versions.sort(apt_pkg.VersionCompare)
219 for version in versions:
220 packages = real_nbs[source][version].keys()
223 nbs_to_remove.append(pkg)
224 output += " o %s: %s\n" % (version, ", ".join(packages))
231 print "Suggested command:"
232 print " dak rm -m \"[auto-cruft] NBS\" -s %s -b %s" % (suite, " ".join(nbs_to_remove))
235 ################################################################################
237 def do_dubious_nbs(dubious_nbs):
242 dubious_nbs_keys = dubious_nbs.keys()
243 dubious_nbs_keys.sort()
244 for source in dubious_nbs_keys:
245 print " * %s_%s builds: %s" % (source,
246 source_versions.get(source, "??"),
247 source_binaries.get(source, "(source does not exist)"))
248 print " won't admit to building:"
249 versions = dubious_nbs[source].keys()
250 versions.sort(apt_pkg.VersionCompare)
251 for version in versions:
252 packages = dubious_nbs[source][version].keys()
254 print " o %s: %s" % (version, ", ".join(packages))
258 ################################################################################
260 def do_obsolete_source(duplicate_bins, bin2source):
262 for key in duplicate_bins.keys():
263 (source_a, source_b) = key.split('_')
264 for source in [ source_a, source_b ]:
265 if not obsolete.has_key(source):
266 if not source_binaries.has_key(source):
267 # Source has already been removed
270 obsolete[source] = [ i.strip() for i in source_binaries[source].split(',') ]
271 for binary in duplicate_bins[key]:
272 if bin2source.has_key(binary) and bin2source[binary]["source"] == source:
274 if binary in obsolete[source]:
275 obsolete[source].remove(binary)
278 output = "Obsolete source package\n"
279 output += "-----------------------\n\n"
280 obsolete_keys = obsolete.keys()
282 for source in obsolete_keys:
283 if not obsolete[source]:
284 to_remove.append(source)
285 output += " * %s (%s)\n" % (source, source_versions[source])
286 for binary in [ i.strip() for i in source_binaries[source].split(',') ]:
287 if bin2source.has_key(binary):
288 output += " o %s (%s) is built by %s.\n" \
289 % (binary, bin2source[binary]["version"],
290 bin2source[binary]["source"])
292 output += " o %s is not built.\n" % binary
298 print "Suggested command:"
299 print " dak rm -S -p -m \"[auto-cruft] obsolete source package\" %s" % (" ".join(to_remove))
302 def get_suite_binaries():
303 # Initalize a large hash table of all binary packages
307 sys.stderr.write("[Getting a list of binary packages in %s..." % (suite))
308 q = projectB.query("SELECT distinct b.package FROM binaries b, bin_associations ba WHERE ba.suite = %s AND ba.bin = b.id" % (suite_id))
310 sys.stderr.write("done. (%d seconds)]\n" % (int(time.time()-before)))
316 ################################################################################
319 global Cnf, projectB, suite, suite_id, source_binaries, source_versions
321 Cnf = utils.get_conf()
323 Arguments = [('h',"help","Cruft-Report::Options::Help"),
324 ('m',"mode","Cruft-Report::Options::Mode", "HasArg"),
325 ('s',"suite","Cruft-Report::Options::Suite","HasArg"),
326 ('w',"wanna-build-dump","Cruft-Report::Options::Wanna-Build-Dump","HasArg")]
328 if not Cnf.has_key("Cruft-Report::Options::%s" % (i)):
329 Cnf["Cruft-Report::Options::%s" % (i)] = ""
330 Cnf["Cruft-Report::Options::Suite"] = Cnf["Dinstall::DefaultSuite"]
332 if not Cnf.has_key("Cruft-Report::Options::Mode"):
333 Cnf["Cruft-Report::Options::Mode"] = "daily"
335 if not Cnf.has_key("Cruft-Report::Options::Wanna-Build-Dump"):
336 Cnf["Cruft-Report::Options::Wanna-Build-Dump"] = "/srv/ftp.debian.org/scripts/nfu"
338 apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
340 Options = Cnf.SubTree("Cruft-Report::Options")
344 # Set up checks based on mode
345 if Options["Mode"] == "daily":
346 checks = [ "nbs", "nviu", "obsolete source" ]
347 elif Options["Mode"] == "full":
348 checks = [ "nbs", "nviu", "obsolete source", "nfu", "dubious nbs", "bnb", "bms", "anais" ]
350 utils.warn("%s is not a recognised mode - only 'full' or 'daily' are understood." % (Options["Mode"]))
353 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
354 database.init(Cnf, projectB)
368 suite = Options["Suite"]
369 suite_id = database.get_suite_id(suite)
374 bins_in_suite = get_suite_binaries()
376 # Checks based on the Sources files
377 components = Cnf.ValueList("Suite::%s::Components" % (suite))
378 for component in components:
379 filename = "%s/dists/%s/%s/source/Sources.gz" % (Cnf["Dir::Root"], suite, component)
380 # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
381 (fd, temp_filename) = utils.temp_filename()
382 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
384 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
386 sources = utils.open_file(temp_filename)
387 Sources = apt_pkg.ParseTagFile(sources)
388 while Sources.Step():
389 source = Sources.Section.Find('Package')
390 source_version = Sources.Section.Find('Version')
391 architecture = Sources.Section.Find('Architecture')
392 binaries = Sources.Section.Find('Binary')
393 binaries_list = [ i.strip() for i in binaries.split(',') ]
396 # Check for binaries not built on any architecture.
397 for binary in binaries_list:
398 if not bins_in_suite.has_key(binary):
399 bin_not_built.setdefault(source, {})
400 bin_not_built[source][binary] = ""
402 if "anais" in checks:
403 anais_output += do_anais(architecture, binaries_list, source)
405 # Check for duplicated packages and build indices for checking "no source" later
406 source_index = component + '/' + source
407 if src_pkgs.has_key(source):
408 print " %s is a duplicated source package (%s and %s)" % (source, source_index, src_pkgs[source])
409 src_pkgs[source] = source_index
410 for binary in binaries_list:
411 if bin_pkgs.has_key(binary):
412 key_list = [ source, bin_pkgs[binary] ]
414 key = '_'.join(key_list)
415 duplicate_bins.setdefault(key, [])
416 duplicate_bins[key].append(binary)
417 bin_pkgs[binary] = source
418 source_binaries[source] = binaries
419 source_versions[source] = source_version
422 os.unlink(temp_filename)
424 # Checks based on the Packages files
425 check_components = components[:]
426 if suite != "experimental":
427 check_components.append('main/debian-installer');
428 for component in check_components:
429 architectures = filter(utils.real_arch, database.get_suite_architectures(suite))
430 for architecture in architectures:
431 filename = "%s/dists/%s/%s/binary-%s/Packages.gz" % (Cnf["Dir::Root"], suite, component, architecture)
432 # apt_pkg.ParseTagFile needs a real file handle
433 (fd, temp_filename) = utils.temp_filename()
434 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
436 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
440 nfu_packages.setdefault(architecture,[])
441 nfu_entries = parse_nfu(architecture)
443 packages = utils.open_file(temp_filename)
444 Packages = apt_pkg.ParseTagFile(packages)
445 while Packages.Step():
446 package = Packages.Section.Find('Package')
447 source = Packages.Section.Find('Source', "")
448 version = Packages.Section.Find('Version')
451 if bin2source.has_key(package) and \
452 apt_pkg.VersionCompare(version, bin2source[package]["version"]) > 0:
453 bin2source[package]["version"] = version
454 bin2source[package]["source"] = source
456 bin2source[package] = {}
457 bin2source[package]["version"] = version
458 bin2source[package]["source"] = source
459 if source.find("(") != -1:
460 m = re_extract_src_version.match(source)
463 if not bin_pkgs.has_key(package):
464 nbs.setdefault(source,{})
465 nbs[source].setdefault(package, {})
466 nbs[source][package][version] = ""
468 previous_source = bin_pkgs[package]
469 if previous_source != source:
470 key_list = [ source, previous_source ]
472 key = '_'.join(key_list)
473 duplicate_bins.setdefault(key, [])
474 if package not in duplicate_bins[key]:
475 duplicate_bins[key].append(package)
477 if package in nfu_entries and \
478 version != source_versions[source]: # only suggest to remove out-of-date packages
479 nfu_packages[architecture].append((package,version,source_versions[source]))
482 os.unlink(temp_filename)
484 if "obsolete source" in checks:
485 do_obsolete_source(duplicate_bins, bin2source)
487 # Distinguish dubious (version numbers match) and 'real' NBS (they don't)
490 for source in nbs.keys():
491 for package in nbs[source].keys():
492 versions = nbs[source][package].keys()
493 versions.sort(apt_pkg.VersionCompare)
494 latest_version = versions.pop()
495 source_version = source_versions.get(source,"0")
496 if apt_pkg.VersionCompare(latest_version, source_version) == 0:
497 add_nbs(dubious_nbs, source, latest_version, package)
499 add_nbs(real_nbs, source, latest_version, package)
509 if Options["Mode"] == "full":
517 print "Unbuilt binary packages"
518 print "-----------------------"
520 keys = bin_not_built.keys()
523 binaries = bin_not_built[source].keys()
525 print " o %s: %s" % (source, ", ".join(binaries))
529 print "Built from multiple source packages"
530 print "-----------------------------------"
532 keys = duplicate_bins.keys()
535 (source_a, source_b) = key.split("_")
536 print " o %s & %s => %s" % (source_a, source_b, ", ".join(duplicate_bins[key]))
539 if "anais" in checks:
540 print "Architecture Not Allowed In Source"
541 print "----------------------------------"
545 if "dubious nbs" in checks:
546 do_dubious_nbs(dubious_nbs)
549 ################################################################################
551 if __name__ == '__main__':