4 Check for obsolete binary packages
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2000-2006 James Troup <james@nocrew.org>
8 @copyright: 2009 Torsten Werner <twerner@debian.org>
9 @license: GNU General Public License version 2 or later
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ################################################################################
28 # ``If you're claiming that's a "problem" that needs to be "fixed",
29 # you might as well write some letters to God about how unfair entropy
30 # is while you're at it.'' -- 20020802143104.GA5628@azure.humbug.org.au
32 ## TODO: fix NBS looping for version, implement Dubious NBS, fix up output of
33 ## duplicate source package stuff, improve experimental ?, add overrides,
34 ## avoid ANAIS for duplicated packages
36 ################################################################################
38 import commands, os, sys, re
41 from daklib.config import Config
42 from daklib.dbconn import *
43 from daklib import utils
44 from daklib.regexes import re_extract_src_version
45 from daklib.cruft import *
47 ################################################################################
49 no_longer_in_suite = {}; # Really should be static to add_nbs, but I'm lazy
54 ################################################################################
56 def usage(exit_code=0):
57 print """Usage: dak cruft-report
58 Check for obsolete or duplicated packages.
60 -h, --help show this help and exit.
61 -m, --mode=MODE chose the MODE to run in (full, daily, bdo).
62 -s, --suite=SUITE check suite SUITE.
63 -R, --rdep-check check reverse dependencies
64 -w, --wanna-build-dump where to find the copies of https://buildd.debian.org/stats/*.txt"""
67 ################################################################################
69 def add_nbs(nbs_d, source, version, package, suite_id, session):
70 # Ensure the package is still in the suite (someone may have already removed it)
71 if no_longer_in_suite.has_key(package):
74 q = session.execute("""SELECT b.id FROM binaries b, bin_associations ba
75 WHERE ba.bin = b.id AND ba.suite = :suite_id
76 AND b.package = :package LIMIT 1""", {'suite_id': suite_id,
79 no_longer_in_suite[package] = ""
82 nbs_d.setdefault(source, {})
83 nbs_d[source].setdefault(version, {})
84 nbs_d[source][version][package] = ""
86 ################################################################################
88 # Check for packages built on architectures they shouldn't be.
89 def do_anais(architecture, binaries_list, source, session):
90 if architecture == "any" or architecture == "all":
95 for arch in architecture.split():
96 architectures[arch.strip()] = ""
97 for binary in binaries_list:
98 q = session.execute("""SELECT a.arch_string, b.version
99 FROM binaries b, bin_associations ba, architecture a
100 WHERE ba.suite = :suiteid AND ba.bin = b.id
101 AND b.architecture = a.id AND b.package = :package""",
102 {'suiteid': suite_id, 'package': binary})
108 if architectures.has_key(arch):
109 versions.append(version)
110 versions.sort(apt_pkg.version_compare)
112 latest_version = versions.pop()
114 latest_version = None
115 # Check for 'invalid' architectures
120 if not architectures.has_key(arch):
121 versions_d.setdefault(version, [])
122 versions_d[version].append(arch)
125 anais_output += "\n (*) %s_%s [%s]: %s\n" % (binary, latest_version, source, architecture)
126 versions = versions_d.keys()
127 versions.sort(apt_pkg.version_compare)
128 for version in versions:
129 arches = versions_d[version]
131 anais_output += " o %s: %s\n" % (version, ", ".join(arches))
135 ################################################################################
137 # Check for out-of-date binaries on architectures that do not want to build that
138 # package any more, and have them listed as Not-For-Us
139 def do_nfu(nfu_packages):
144 for architecture in nfu_packages:
145 a2p[architecture] = []
146 for (package,bver,sver) in nfu_packages[architecture]:
147 output += " * [%s] does not want %s (binary %s, source %s)\n" % (architecture, package, bver, sver)
148 a2p[architecture].append(package)
152 print "Obsolete by Not-For-Us"
153 print "----------------------"
157 print "Suggested commands:"
158 for architecture in a2p:
159 if a2p[architecture]:
160 print (" dak rm -m \"[auto-cruft] NFU\" -s %s -a %s -b %s" %
161 (suite.suite_name, architecture, " ".join(a2p[architecture])))
164 def parse_nfu(architecture):
166 # utils/hpodder_1.1.5.0: Not-For-Us [optional:out-of-date]
167 r = re.compile("^\w+/([^_]+)_.*: Not-For-Us")
171 filename = "%s/%s-all.txt" % (cnf["Cruft-Report::Options::Wanna-Build-Dump"], architecture)
173 # Not all architectures may have a wanna-build dump, so we want to ignore missin
175 if os.path.exists(filename):
176 f = utils.open_file(filename)
187 utils.warn("No wanna-build dump file for architecture %s" % architecture)
190 ################################################################################
192 def do_newer_version(lowersuite_name, highersuite_name, code, session):
193 list = newer_version(lowersuite_name, highersuite_name, session)
196 title = "Newer version in %s" % lowersuite_name
198 print "-" * len(title)
201 (source, higher_version, lower_version) = i
202 print " o %s (%s, %s)" % (source, higher_version, lower_version)
203 nv_to_remove.append(source)
205 print "Suggested command:"
206 print " dak rm -m \"[auto-cruft] %s\" -s %s %s" % (code, highersuite_name,
207 " ".join(nv_to_remove))
210 ################################################################################
213 def reportWithoutSource(suite_name, suite_id, session, rdeps=False):
214 rows = query_without_source(suite_id, session)
215 title = 'packages without source in suite %s' % suite_name
216 if rows.rowcount > 0:
217 print '%s\n%s\n' % (title, '-' * len(title))
218 message = '"[auto-cruft] no longer built from source"'
220 (package, version) = row
221 print "* package %s in version %s is no longer built from source" % \
223 print " - suggested command:"
224 print " dak rm -m %s -s %s -a all -p -R -b %s" % \
225 (message, suite_name, package)
227 if utils.check_reverse_depends([package], suite_name, [], session, True):
230 print " - No dependency problem found\n"
234 def queryNewerAll(suite_name, session):
235 """searches for arch != all packages that have an arch == all
236 package with a higher version in the same suite"""
239 select bab1.package, bab1.version as oldver,
240 array_to_string(array_agg(a.arch_string), ',') as oldarch,
241 bab2.version as newver
242 from bin_associations_binaries bab1
243 join bin_associations_binaries bab2
244 on bab1.package = bab2.package and bab1.version < bab2.version and
245 bab1.suite = bab2.suite and bab1.architecture > 2 and
246 bab2.architecture = 2
247 join architecture a on bab1.architecture = a.id
248 join suite s on bab1.suite = s.id
249 where s.suite_name = :suite_name
250 group by bab1.package, oldver, bab1.suite, newver"""
251 return session.execute(query, { 'suite_name': suite_name })
253 def reportNewerAll(suite_name, session):
254 rows = queryNewerAll(suite_name, session)
255 title = 'obsolete arch any packages in suite %s' % suite_name
256 if rows.rowcount > 0:
257 print '%s\n%s\n' % (title, '-' * len(title))
258 message = '"[auto-cruft] obsolete arch any package"'
260 (package, oldver, oldarch, newver) = row
261 print "* package %s is arch any in version %s but arch all in version %s" % \
262 (package, oldver, newver)
263 print " - suggested command:"
264 print " dak rm -m %s -s %s -a %s -p -b %s\n" % \
265 (message, suite_name, oldarch, package)
269 def reportNBS(suite_name, suite_id, rdeps=False):
270 session = DBConn().session()
271 nbsRows = queryNBS(suite_id, session)
272 title = 'NBS packages in suite %s' % suite_name
273 if nbsRows.rowcount > 0:
274 print '%s\n%s\n' % (title, '-' * len(title))
276 (pkg_list, arch_list, source, version) = row
277 pkg_string = ' '.join(pkg_list)
278 arch_string = ','.join(arch_list)
279 print "* source package %s version %s no longer builds" % \
281 print " binary package(s): %s" % pkg_string
282 print " on %s" % arch_string
283 print " - suggested command:"
284 message = '"[auto-cruft] NBS (no longer built by %s)"' % source
285 print " dak rm -m %s -s %s -a %s -p -R -b %s" % \
286 (message, suite_name, arch_string, pkg_string)
288 if utils.check_reverse_depends(pkg_list, suite_name, arch_list, session, True):
291 print " - No dependency problem found\n"
296 def reportAllNBS(suite_name, suite_id, session, rdeps=False):
297 reportWithoutSource(suite_name, suite_id, session, rdeps)
298 reportNewerAll(suite_name, session)
299 reportNBS(suite_name, suite_id, rdeps)
301 ################################################################################
303 def do_dubious_nbs(dubious_nbs):
308 dubious_nbs_keys = dubious_nbs.keys()
309 dubious_nbs_keys.sort()
310 for source in dubious_nbs_keys:
311 print " * %s_%s builds: %s" % (source,
312 source_versions.get(source, "??"),
313 source_binaries.get(source, "(source does not exist)"))
314 print " won't admit to building:"
315 versions = dubious_nbs[source].keys()
316 versions.sort(apt_pkg.version_compare)
317 for version in versions:
318 packages = dubious_nbs[source][version].keys()
320 print " o %s: %s" % (version, ", ".join(packages))
324 ################################################################################
326 def obsolete_source(suite_name, session):
327 """returns obsolete source packages for suite_name without binaries
328 in the same suite sorted by install_date; install_date should help
329 detecting source only (or binary throw away) uploads; duplicates in
330 the suite are skipped
332 subquery 'source_suite_unique' returns source package names from
333 suite without duplicates; the rationale behind is that neither
334 cruft-report nor rm cannot handle duplicates (yet)"""
337 WITH source_suite_unique AS
338 (SELECT source, suite
339 FROM source_suite GROUP BY source, suite HAVING count(*) = 1)
340 SELECT ss.src, ss.source, ss.version,
341 to_char(ss.install_date, 'YYYY-MM-DD') AS install_date
343 JOIN source_suite_unique ssu
344 ON ss.source = ssu.source AND ss.suite = ssu.suite
345 JOIN suite s ON s.id = ss.suite
346 LEFT JOIN bin_associations_binaries bab
347 ON ss.src = bab.source AND ss.suite = bab.suite
348 WHERE s.suite_name = :suite_name AND bab.id IS NULL
349 ORDER BY install_date"""
350 args = { 'suite_name': suite_name }
351 return session.execute(query, args)
353 def source_bin(source, session):
354 """returns binaries built by source for all or no suite grouped and
355 ordered by package name"""
360 JOIN src_associations_src sas ON b.source = sas.src
361 WHERE sas.source = :source
363 ORDER BY b.package"""
364 args = { 'source': source }
365 return session.execute(query, args)
367 def newest_source_bab(suite_name, package, session):
368 """returns newest source that builds binary package in suite grouped
369 and sorted by source and package name"""
372 SELECT sas.source, MAX(sas.version) AS srcver
373 FROM src_associations_src sas
374 JOIN bin_associations_binaries bab ON sas.src = bab.source
375 JOIN suite s on s.id = bab.suite
376 WHERE s.suite_name = :suite_name AND bab.package = :package
377 GROUP BY sas.source, bab.package
378 ORDER BY sas.source, bab.package"""
379 args = { 'suite_name': suite_name, 'package': package }
380 return session.execute(query, args)
382 def report_obsolete_source(suite_name, session):
383 rows = obsolete_source(suite_name, session)
384 if rows.rowcount == 0:
387 """Obsolete source packages in suite %s
388 ----------------------------------%s\n""" % \
389 (suite_name, '-' * len(suite_name))
390 for os_row in rows.fetchall():
391 (src, old_source, version, install_date) = os_row
392 print " * obsolete source %s version %s installed at %s" % \
393 (old_source, version, install_date)
394 for sb_row in source_bin(old_source, session):
396 print " - has built binary %s" % package
397 for nsb_row in newest_source_bab(suite_name, package, session):
398 (new_source, srcver) = nsb_row
399 print " currently built by source %s version %s" % \
401 print " - suggested command:"
402 rm_opts = "-S -p -m \"[auto-cruft] obsolete source package\""
403 print " dak rm -s %s %s %s\n" % (suite_name, rm_opts, old_source)
405 def get_suite_binaries(suite, session):
406 # Initalize a large hash table of all binary packages
409 print "Getting a list of binary packages in %s..." % suite.suite_name
410 q = session.execute("""SELECT distinct b.package
411 FROM binaries b, bin_associations ba
412 WHERE ba.suite = :suiteid AND ba.bin = b.id""",
413 {'suiteid': suite.suite_id})
414 for i in q.fetchall():
419 ################################################################################
421 def report_outdated_nonfree(suite, session, rdeps=False):
424 query = """WITH outdated_sources AS (
425 SELECT s.source, s.version, s.id
427 JOIN src_associations sa ON sa.source = s.id
431 WHERE suite_name = :suite )
432 AND sa.created < (now() - interval :delay)
433 EXCEPT SELECT s.source, max(s.version) AS version, max(s.id)
435 JOIN src_associations sa ON sa.source = s.id
439 WHERE suite_name = :suite )
440 AND sa.created < (now() - interval :delay)
443 SELECT b.package, s.source, (
446 WHERE a.id = b.architecture ) AS arch
448 JOIN outdated_sources s ON s.id = b.source
449 JOIN bin_associations ba ON ba.bin = b.id
450 JOIN override o ON o.package = b.package AND o.suite = ba.suite
454 WHERE suite_name = :suite )
458 WHERE name = 'non-free' ) )
459 SELECT DISTINCT package, source, arch
461 ORDER BY source, package, arch"""
463 res = session.execute(query, {'suite': suite, 'delay': "'15 days'"})
470 if not source in packages:
471 packages[source] = {}
472 if not binary in packages[source]:
473 packages[source][binary] = set()
474 packages[source][binary].add(arch)
476 title = 'Outdated non-free binaries in suite %s' % suite
477 message = '"[auto-cruft] outdated non-free binaries"'
478 print '%s\n%s\n' % (title, '-' * len(title))
479 for source in sorted(packages):
482 print '* package %s has outdated non-free binaries' % source
483 print ' - suggested command:'
484 for binary in sorted(packages[source]):
486 archs = archs.union(packages[source][binary])
487 print ' dak rm -m %s -s %s -a %s -p -R -b %s' % \
488 (message, suite, ','.join(archs), ' '.join(binaries))
490 if utils.check_reverse_depends(list(binaries), suite, archs, session, True):
493 print " - No dependency problem found\n"
497 ################################################################################
500 global suite, suite_id, source_binaries, source_versions
504 Arguments = [('h',"help","Cruft-Report::Options::Help"),
505 ('m',"mode","Cruft-Report::Options::Mode", "HasArg"),
506 ('R',"rdep-check", "Cruft-Report::Options::Rdep-Check"),
507 ('s',"suite","Cruft-Report::Options::Suite","HasArg"),
508 ('w',"wanna-build-dump","Cruft-Report::Options::Wanna-Build-Dump","HasArg")]
509 for i in [ "help", "Rdep-Check" ]:
510 if not cnf.has_key("Cruft-Report::Options::%s" % (i)):
511 cnf["Cruft-Report::Options::%s" % (i)] = ""
513 cnf["Cruft-Report::Options::Suite"] = cnf.get("Dinstall::DefaultSuite", "unstable")
515 if not cnf.has_key("Cruft-Report::Options::Mode"):
516 cnf["Cruft-Report::Options::Mode"] = "daily"
518 if not cnf.has_key("Cruft-Report::Options::Wanna-Build-Dump"):
519 cnf["Cruft-Report::Options::Wanna-Build-Dump"] = "/srv/ftp-master.debian.org/scripts/nfu"
521 apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
523 Options = cnf.subtree("Cruft-Report::Options")
527 if Options["Rdep-Check"]:
532 # Set up checks based on mode
533 if Options["Mode"] == "daily":
534 checks = [ "nbs", "nviu", "nvit", "obsolete source", "outdated non-free", "nfu" ]
535 elif Options["Mode"] == "full":
536 checks = [ "nbs", "nviu", "nvit", "obsolete source", "outdated non-free", "nfu", "dubious nbs", "bnb", "bms", "anais" ]
537 elif Options["Mode"] == "bdo":
538 checks = [ "nbs", "obsolete source" ]
540 utils.warn("%s is not a recognised mode - only 'full', 'daily' or 'bdo' are understood." % (Options["Mode"]))
543 session = DBConn().session()
556 suite = get_suite(Options["Suite"].lower(), session)
558 utils.fubar("Cannot find suite %s" % Options["Suite"].lower())
560 suite_id = suite.suite_id
561 suite_name = suite.suite_name.lower()
563 if "obsolete source" in checks:
564 report_obsolete_source(suite_name, session)
567 reportAllNBS(suite_name, suite_id, session, rdeps)
569 if "outdated non-free" in checks:
570 report_outdated_nonfree(suite_name, session, rdeps)
575 bins_in_suite = get_suite_binaries(suite, session)
577 # Checks based on the Sources files
578 components = get_component_names(session)
579 for component in components:
580 filename = "%s/dists/%s/%s/source/Sources.gz" % (suite.archive.path, suite_name, component)
581 # apt_pkg.TagFile needs a real file handle and can't handle a GzipFile instance...
582 (fd, temp_filename) = utils.temp_filename()
583 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
585 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
587 sources = utils.open_file(temp_filename)
588 Sources = apt_pkg.TagFile(sources)
589 while Sources.step():
590 source = Sources.section.find('Package')
591 source_version = Sources.section.find('Version')
592 architecture = Sources.section.find('Architecture')
593 binaries = Sources.section.find('Binary')
594 binaries_list = [ i.strip() for i in binaries.split(',') ]
597 # Check for binaries not built on any architecture.
598 for binary in binaries_list:
599 if not bins_in_suite.has_key(binary):
600 bin_not_built.setdefault(source, {})
601 bin_not_built[source][binary] = ""
603 if "anais" in checks:
604 anais_output += do_anais(architecture, binaries_list, source, session)
606 # build indices for checking "no source" later
607 source_index = component + '/' + source
608 src_pkgs[source] = source_index
609 for binary in binaries_list:
610 bin_pkgs[binary] = source
611 source_binaries[source] = binaries
612 source_versions[source] = source_version
615 os.unlink(temp_filename)
617 # Checks based on the Packages files
618 check_components = components[:]
619 if suite_name != "experimental":
620 check_components.append('main/debian-installer');
622 for component in check_components:
623 architectures = [ a.arch_string for a in get_suite_architectures(suite_name,
624 skipsrc=True, skipall=True,
626 for architecture in architectures:
627 if component == 'main/debian-installer' and re.match("kfreebsd", architecture):
629 filename = "%s/dists/%s/%s/binary-%s/Packages.gz" % (suite.archive.path, suite_name, component, architecture)
630 # apt_pkg.TagFile needs a real file handle
631 (fd, temp_filename) = utils.temp_filename()
632 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
634 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
638 nfu_packages.setdefault(architecture,[])
639 nfu_entries = parse_nfu(architecture)
641 packages = utils.open_file(temp_filename)
642 Packages = apt_pkg.TagFile(packages)
643 while Packages.step():
644 package = Packages.section.find('Package')
645 source = Packages.section.find('Source', "")
646 version = Packages.section.find('Version')
649 if bin2source.has_key(package) and \
650 apt_pkg.version_compare(version, bin2source[package]["version"]) > 0:
651 bin2source[package]["version"] = version
652 bin2source[package]["source"] = source
654 bin2source[package] = {}
655 bin2source[package]["version"] = version
656 bin2source[package]["source"] = source
657 if source.find("(") != -1:
658 m = re_extract_src_version.match(source)
661 if not bin_pkgs.has_key(package):
662 nbs.setdefault(source,{})
663 nbs[source].setdefault(package, {})
664 nbs[source][package][version] = ""
667 if package in nfu_entries and \
668 version != source_versions[source]: # only suggest to remove out-of-date packages
669 nfu_packages[architecture].append((package,version,source_versions[source]))
672 os.unlink(temp_filename)
674 # Distinguish dubious (version numbers match) and 'real' NBS (they don't)
676 for source in nbs.keys():
677 for package in nbs[source].keys():
678 versions = nbs[source][package].keys()
679 versions.sort(apt_pkg.version_compare)
680 latest_version = versions.pop()
681 source_version = source_versions.get(source,"0")
682 if apt_pkg.version_compare(latest_version, source_version) == 0:
683 add_nbs(dubious_nbs, source, latest_version, package, suite_id, session)
686 do_newer_version('unstable', 'experimental', 'NVIU', session)
689 do_newer_version('testing', 'testing-proposed-updates', 'NVIT', session)
693 if Options["Mode"] == "full":
701 print "Unbuilt binary packages"
702 print "-----------------------"
704 keys = bin_not_built.keys()
707 binaries = bin_not_built[source].keys()
709 print " o %s: %s" % (source, ", ".join(binaries))
713 report_multiple_source(suite)
715 if "anais" in checks:
716 print "Architecture Not Allowed In Source"
717 print "----------------------------------"
721 if "dubious nbs" in checks:
722 do_dubious_nbs(dubious_nbs)
725 ################################################################################
727 if __name__ == '__main__':