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 ################################################################################
39 warnings.filterwarnings('ignore', \
40 "apt_pkg\.ParseTagFile\(\) is deprecated\. Please see apt_pkg\.TagFile\(\) for the replacement\.", \
42 warnings.filterwarnings('ignore', \
43 "Attribute '.*' of the 'apt_pkg\.TagFile' object is deprecated, use '.*' instead\.", \
45 warnings.filterwarnings('ignore', \
46 "Attribute 'Find' of the 'apt_pkg\.TagSection' object is deprecated, use 'find' instead\.", \
49 ################################################################################
51 import commands, os, sys, re
54 from daklib.config import Config
55 from daklib.dbconn import *
56 from daklib import utils
57 from daklib.regexes import re_extract_src_version
58 from daklib.cruft import *
60 ################################################################################
62 no_longer_in_suite = {}; # Really should be static to add_nbs, but I'm lazy
67 ################################################################################
69 def usage(exit_code=0):
70 print """Usage: dak cruft-report
71 Check for obsolete or duplicated packages.
73 -h, --help show this help and exit.
74 -m, --mode=MODE chose the MODE to run in (full or daily).
75 -s, --suite=SUITE check suite SUITE.
76 -w, --wanna-build-dump where to find the copies of http://buildd.debian.org/stats/*.txt"""
79 ################################################################################
81 def add_nbs(nbs_d, source, version, package, suite_id, session):
82 # Ensure the package is still in the suite (someone may have already removed it)
83 if no_longer_in_suite.has_key(package):
86 q = session.execute("""SELECT b.id FROM binaries b, bin_associations ba
87 WHERE ba.bin = b.id AND ba.suite = :suite_id
88 AND b.package = :package LIMIT 1""", {'suite_id': suite_id,
91 no_longer_in_suite[package] = ""
94 nbs_d.setdefault(source, {})
95 nbs_d[source].setdefault(version, {})
96 nbs_d[source][version][package] = ""
98 ################################################################################
100 # Check for packages built on architectures they shouldn't be.
101 def do_anais(architecture, binaries_list, source, session):
102 if architecture == "any" or architecture == "all":
107 for arch in architecture.split():
108 architectures[arch.strip()] = ""
109 for binary in binaries_list:
110 q = session.execute("""SELECT a.arch_string, b.version
111 FROM binaries b, bin_associations ba, architecture a
112 WHERE ba.suite = :suiteid AND ba.bin = b.id
113 AND b.architecture = a.id AND b.package = :package""",
114 {'suiteid': suite_id, 'package': binary})
120 if architectures.has_key(arch):
121 versions.append(version)
122 versions.sort(apt_pkg.VersionCompare)
124 latest_version = versions.pop()
126 latest_version = None
127 # Check for 'invalid' architectures
132 if not architectures.has_key(arch):
133 versions_d.setdefault(version, [])
134 versions_d[version].append(arch)
137 anais_output += "\n (*) %s_%s [%s]: %s\n" % (binary, latest_version, source, architecture)
138 versions = versions_d.keys()
139 versions.sort(apt_pkg.VersionCompare)
140 for version in versions:
141 arches = versions_d[version]
143 anais_output += " o %s: %s\n" % (version, ", ".join(arches))
147 ################################################################################
149 # Check for out-of-date binaries on architectures that do not want to build that
150 # package any more, and have them listed as Not-For-Us
151 def do_nfu(nfu_packages):
156 for architecture in nfu_packages:
157 a2p[architecture] = []
158 for (package,bver,sver) in nfu_packages[architecture]:
159 output += " * [%s] does not want %s (binary %s, source %s)\n" % (architecture, package, bver, sver)
160 a2p[architecture].append(package)
164 print "Obsolete by Not-For-Us"
165 print "----------------------"
169 print "Suggested commands:"
170 for architecture in a2p:
171 if a2p[architecture]:
172 print (" dak rm -m \"[auto-cruft] NFU\" -s %s -a %s -b %s" %
173 (suite.suite_name, architecture, " ".join(a2p[architecture])))
176 def parse_nfu(architecture):
178 # utils/hpodder_1.1.5.0: Not-For-Us [optional:out-of-date]
179 r = re.compile("^\w+/([^_]+)_.*: Not-For-Us")
183 filename = "%s/%s-all.txt" % (cnf["Cruft-Report::Options::Wanna-Build-Dump"], architecture)
185 # Not all architectures may have a wanna-build dump, so we want to ignore missin
187 if os.path.exists(filename):
188 f = utils.open_file(filename)
199 utils.warn("No wanna-build dump file for architecture %s" % architecture)
202 ################################################################################
204 def do_newer_version(lowersuite_name, highersuite_name, code, session):
205 list = newer_version(lowersuite_name, highersuite_name, session)
208 title = "Newer version in %s" % lowersuite_name
210 print "-" * len(title)
213 (source, higher_version, lower_version) = i
214 print " o %s (%s, %s)" % (source, higher_version, lower_version)
215 nv_to_remove.append(source)
217 print "Suggested command:"
218 print " dak rm -m \"[auto-cruft] %s\" -s %s %s" % (code, highersuite_name,
219 " ".join(nv_to_remove))
222 ################################################################################
224 def queryWithoutSource(suite_id, session):
225 """searches for arch: all packages from suite that do no longer
226 reference a source package in the same suite
228 subquery unique_binaries: selects all packages with only 1 version
229 in suite since 'dak rm' does not allow to specify version numbers"""
232 with unique_binaries as
233 (select package, max(version) as version, max(source) as source
234 from bin_associations_binaries
235 where architecture = 2 and suite = :suite_id
236 group by package having count(*) = 1)
237 select ub.package, ub.version
238 from unique_binaries ub
239 left join src_associations_src sas
240 on ub.source = sas.src and sas.suite = :suite_id
242 order by ub.package"""
243 return session.execute(query, { 'suite_id': suite_id })
245 def reportWithoutSource(suite_name, suite_id, session):
246 rows = queryWithoutSource(suite_id, session)
247 title = 'packages without source in suite %s' % suite_name
248 if rows.rowcount > 0:
249 print '%s\n%s\n' % (title, '-' * len(title))
250 message = '"[auto-cruft] no longer built from source"'
252 (package, version) = row
253 print "* package %s in version %s is no longer built from source" % \
255 print " - suggested command:"
256 print " dak rm -m %s -s %s -a all -p -R -b %s\n" % \
257 (message, suite_name, package)
259 def queryNewerAll(suite_name, session):
260 """searches for arch != all packages that have an arch == all
261 package with a higher version in the same suite"""
264 select bab1.package, bab1.version as oldver,
265 array_to_string(array_agg(a.arch_string), ',') as oldarch,
266 bab2.version as newver
267 from bin_associations_binaries bab1
268 join bin_associations_binaries bab2
269 on bab1.package = bab2.package and bab1.version < bab2.version and
270 bab1.suite = bab2.suite and bab1.architecture > 2 and
271 bab2.architecture = 2
272 join architecture a on bab1.architecture = a.id
273 join suite s on bab1.suite = s.id
274 where s.suite_name = :suite_name
275 group by bab1.package, oldver, bab1.suite, newver"""
276 return session.execute(query, { 'suite_name': suite_name })
278 def reportNewerAll(suite_name, session):
279 rows = queryNewerAll(suite_name, session)
280 title = 'obsolete arch any packages in suite %s' % suite_name
281 if rows.rowcount > 0:
282 print '%s\n%s\n' % (title, '-' * len(title))
283 message = '"[auto-cruft] obsolete arch any package"'
285 (package, oldver, oldarch, newver) = row
286 print "* package %s is arch any in version %s but arch all in version %s" % \
287 (package, oldver, newver)
288 print " - suggested command:"
289 print " dak rm -m %s -s %s -a %s -p -b %s\n" % \
290 (message, suite_name, oldarch, package)
292 def queryNBS(suite_id, session):
293 """This one is really complex. It searches arch != all packages that
294 are no longer built from current source packages in suite.
296 temp table unique_binaries: will be populated with packages that
297 have only one version in suite because 'dak rm' does not allow
298 specifying version numbers
300 temp table newest_binaries: will be populated with packages that are
301 built from current sources
303 subquery uptodate_arch: returns all architectures built from current
306 subquery unique_binaries_uptodate_arch: returns all packages in
307 architectures from uptodate_arch
309 subquery unique_binaries_uptodate_arch_agg: same as
310 unique_binaries_uptodate_arch but with column architecture
313 subquery uptodate_packages: similar to uptodate_arch but returns all
314 packages built from current sources
316 subquery outdated_packages: returns all packages with architectures
317 no longer built from current source
321 create temp table unique_binaries (
322 package text not null,
323 architecture integer not null,
324 source integer not null);
326 insert into unique_binaries
327 select bab.package, bab.architecture, max(bab.source)
328 from bin_associations_binaries bab
329 where bab.suite = :suite_id and bab.architecture > 2
330 group by package, architecture having count(*) = 1;
332 create temp table newest_binaries (
333 package text not null,
334 architecture integer not null,
335 source text not null,
336 version debversion not null);
338 insert into newest_binaries
339 select ub.package, ub.architecture, nsa.source, nsa.version
340 from unique_binaries ub
341 join newest_src_association nsa
342 on ub.source = nsa.src and nsa.suite = :suite_id;
344 with uptodate_arch as
345 (select architecture, source, version
347 group by architecture, source, version),
348 unique_binaries_uptodate_arch as
349 (select ub.package, ub.architecture, ua.source, ua.version
350 from unique_binaries ub
353 join uptodate_arch ua
354 on ub.architecture = ua.architecture and s.source = ua.source),
355 unique_binaries_uptodate_arch_agg as
356 (select ubua.package,
357 array(select unnest(array_agg(a.arch_string)) order by 1) as arch_list,
358 ubua.source, ubua.version
359 from unique_binaries_uptodate_arch ubua
361 on ubua.architecture = a.id
362 group by ubua.source, ubua.version, ubua.package),
364 (select package, source, version
366 group by package, source, version),
368 (select array(select unnest(array_agg(package)) order by 1) as pkg_list,
369 arch_list, source, version
370 from unique_binaries_uptodate_arch_agg
372 (select package from uptodate_packages)
373 group by arch_list, source, version)
374 select * from outdated_packages order by source"""
375 return session.execute(query, { 'suite_id': suite_id })
377 def reportNBS(suite_name, suite_id):
378 session = DBConn().session()
379 nbsRows = queryNBS(suite_id, session)
380 title = 'NBS packages in suite %s' % suite_name
381 if nbsRows.rowcount > 0:
382 print '%s\n%s\n' % (title, '-' * len(title))
384 (pkg_list, arch_list, source, version) = row
385 pkg_string = ' '.join(pkg_list)
386 arch_string = ','.join(arch_list)
387 print "* source package %s version %s no longer builds" % \
389 print " binary package(s): %s" % pkg_string
390 print " on %s" % arch_string
391 print " - suggested command:"
392 message = '"[auto-cruft] NBS (no longer built by %s)"' % source
393 print " dak rm -m %s -s %s -a %s -p -R -b %s\n" % \
394 (message, suite_name, arch_string, pkg_string)
397 def reportAllNBS(suite_name, suite_id, session):
398 reportWithoutSource(suite_name, suite_id, session)
399 reportNewerAll(suite_name, session)
400 reportNBS(suite_name, suite_id)
402 ################################################################################
404 def do_dubious_nbs(dubious_nbs):
409 dubious_nbs_keys = dubious_nbs.keys()
410 dubious_nbs_keys.sort()
411 for source in dubious_nbs_keys:
412 print " * %s_%s builds: %s" % (source,
413 source_versions.get(source, "??"),
414 source_binaries.get(source, "(source does not exist)"))
415 print " won't admit to building:"
416 versions = dubious_nbs[source].keys()
417 versions.sort(apt_pkg.VersionCompare)
418 for version in versions:
419 packages = dubious_nbs[source][version].keys()
421 print " o %s: %s" % (version, ", ".join(packages))
425 ################################################################################
427 def obsolete_source(suite_name, session):
428 """returns obsolete source packages for suite_name without binaries
429 in the same suite sorted by install_date; install_date should help
430 detecting source only (or binary throw away) uploads; duplicates in
431 the suite are skipped
433 subquery 'source_suite_unique' returns source package names from
434 suite without duplicates; the rationale behind is that neither
435 cruft-report nor rm cannot handle duplicates (yet)"""
438 WITH source_suite_unique AS
439 (SELECT source, suite
440 FROM source_suite GROUP BY source, suite HAVING count(*) = 1)
441 SELECT ss.src, ss.source, ss.version,
442 to_char(ss.install_date, 'YYYY-MM-DD') AS install_date
444 JOIN source_suite_unique ssu
445 ON ss.source = ssu.source AND ss.suite = ssu.suite
446 JOIN suite s ON s.id = ss.suite
447 LEFT JOIN bin_associations_binaries bab
448 ON ss.src = bab.source AND ss.suite = bab.suite
449 WHERE s.suite_name = :suite_name AND bab.id IS NULL
450 ORDER BY install_date"""
451 args = { 'suite_name': suite_name }
452 return session.execute(query, args)
454 def source_bin(source, session):
455 """returns binaries built by source for all or no suite grouped and
456 ordered by package name"""
461 JOIN src_associations_src sas ON b.source = sas.src
462 WHERE sas.source = :source
464 ORDER BY b.package"""
465 args = { 'source': source }
466 return session.execute(query, args)
468 def newest_source_bab(suite_name, package, session):
469 """returns newest source that builds binary package in suite grouped
470 and sorted by source and package name"""
473 SELECT sas.source, MAX(sas.version) AS srcver
474 FROM src_associations_src sas
475 JOIN bin_associations_binaries bab ON sas.src = bab.source
476 JOIN suite s on s.id = bab.suite
477 WHERE s.suite_name = :suite_name AND bab.package = :package
478 GROUP BY sas.source, bab.package
479 ORDER BY sas.source, bab.package"""
480 args = { 'suite_name': suite_name, 'package': package }
481 return session.execute(query, args)
483 def report_obsolete_source(suite_name, session):
484 rows = obsolete_source(suite_name, session)
485 if rows.rowcount == 0:
488 """Obsolete source packages in suite %s
489 ----------------------------------%s\n""" % \
490 (suite_name, '-' * len(suite_name))
491 for os_row in rows.fetchall():
492 (src, old_source, version, install_date) = os_row
493 print " * obsolete source %s version %s installed at %s" % \
494 (old_source, version, install_date)
495 for sb_row in source_bin(old_source, session):
497 print " - has built binary %s" % package
498 for nsb_row in newest_source_bab(suite_name, package, session):
499 (new_source, srcver) = nsb_row
500 print " currently built by source %s version %s" % \
502 print " - suggested command:"
503 rm_opts = "-S -p -m \"[auto-cruft] obsolete source package\""
504 print " dak rm -s %s %s %s\n" % (suite_name, rm_opts, old_source)
506 def get_suite_binaries(suite, session):
507 # Initalize a large hash table of all binary packages
510 print "Getting a list of binary packages in %s..." % suite.suite_name
511 q = session.execute("""SELECT distinct b.package
512 FROM binaries b, bin_associations ba
513 WHERE ba.suite = :suiteid AND ba.bin = b.id""",
514 {'suiteid': suite.suite_id})
515 for i in q.fetchall():
520 ################################################################################
523 global suite, suite_id, source_binaries, source_versions
527 Arguments = [('h',"help","Cruft-Report::Options::Help"),
528 ('m',"mode","Cruft-Report::Options::Mode", "HasArg"),
529 ('s',"suite","Cruft-Report::Options::Suite","HasArg"),
530 ('w',"wanna-build-dump","Cruft-Report::Options::Wanna-Build-Dump","HasArg")]
532 if not cnf.has_key("Cruft-Report::Options::%s" % (i)):
533 cnf["Cruft-Report::Options::%s" % (i)] = ""
534 cnf["Cruft-Report::Options::Suite"] = cnf["Dinstall::DefaultSuite"]
536 if not cnf.has_key("Cruft-Report::Options::Mode"):
537 cnf["Cruft-Report::Options::Mode"] = "daily"
539 if not cnf.has_key("Cruft-Report::Options::Wanna-Build-Dump"):
540 cnf["Cruft-Report::Options::Wanna-Build-Dump"] = "/srv/ftp-master.debian.org/scripts/nfu"
542 apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
544 Options = cnf.SubTree("Cruft-Report::Options")
548 # Set up checks based on mode
549 if Options["Mode"] == "daily":
550 checks = [ "nbs", "nviu", "nvit", "obsolete source", "nfu" ]
551 elif Options["Mode"] == "full":
552 checks = [ "nbs", "nviu", "nvit", "obsolete source", "nfu", "dubious nbs", "bnb", "bms", "anais" ]
554 utils.warn("%s is not a recognised mode - only 'full' or 'daily' are understood." % (Options["Mode"]))
557 session = DBConn().session()
570 suite = get_suite(Options["Suite"].lower(), session)
571 suite_id = suite.suite_id
572 suite_name = suite.suite_name.lower()
574 if "obsolete source" in checks:
575 report_obsolete_source(suite_name, session)
578 reportAllNBS(suite_name, suite_id, session)
583 bins_in_suite = get_suite_binaries(suite, session)
585 # Checks based on the Sources files
586 components = cnf.ValueList("Suite::%s::Components" % (suite_name))
587 for component in components:
588 filename = "%s/dists/%s/%s/source/Sources.gz" % (cnf["Dir::Root"], suite_name, component)
589 # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
590 (fd, temp_filename) = utils.temp_filename()
591 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
593 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
595 sources = utils.open_file(temp_filename)
596 Sources = apt_pkg.ParseTagFile(sources)
597 while Sources.Step():
598 source = Sources.Section.Find('Package')
599 source_version = Sources.Section.Find('Version')
600 architecture = Sources.Section.Find('Architecture')
601 binaries = Sources.Section.Find('Binary')
602 binaries_list = [ i.strip() for i in binaries.split(',') ]
605 # Check for binaries not built on any architecture.
606 for binary in binaries_list:
607 if not bins_in_suite.has_key(binary):
608 bin_not_built.setdefault(source, {})
609 bin_not_built[source][binary] = ""
611 if "anais" in checks:
612 anais_output += do_anais(architecture, binaries_list, source, session)
614 # build indices for checking "no source" later
615 source_index = component + '/' + source
616 src_pkgs[source] = source_index
617 for binary in binaries_list:
618 bin_pkgs[binary] = source
619 source_binaries[source] = binaries
620 source_versions[source] = source_version
623 os.unlink(temp_filename)
625 # Checks based on the Packages files
626 check_components = components[:]
627 if suite_name != "experimental":
628 check_components.append('main/debian-installer');
630 for component in check_components:
631 architectures = [ a.arch_string for a in get_suite_architectures(suite_name,
632 skipsrc=True, skipall=True,
634 for architecture in architectures:
635 if component == 'main/debian-installer' and re.match("kfreebsd", architecture):
637 filename = "%s/dists/%s/%s/binary-%s/Packages.gz" % (cnf["Dir::Root"], suite_name, component, architecture)
638 # apt_pkg.ParseTagFile needs a real file handle
639 (fd, temp_filename) = utils.temp_filename()
640 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
642 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
646 nfu_packages.setdefault(architecture,[])
647 nfu_entries = parse_nfu(architecture)
649 packages = utils.open_file(temp_filename)
650 Packages = apt_pkg.ParseTagFile(packages)
651 while Packages.Step():
652 package = Packages.Section.Find('Package')
653 source = Packages.Section.Find('Source', "")
654 version = Packages.Section.Find('Version')
657 if bin2source.has_key(package) and \
658 apt_pkg.VersionCompare(version, bin2source[package]["version"]) > 0:
659 bin2source[package]["version"] = version
660 bin2source[package]["source"] = source
662 bin2source[package] = {}
663 bin2source[package]["version"] = version
664 bin2source[package]["source"] = source
665 if source.find("(") != -1:
666 m = re_extract_src_version.match(source)
669 if not bin_pkgs.has_key(package):
670 nbs.setdefault(source,{})
671 nbs[source].setdefault(package, {})
672 nbs[source][package][version] = ""
675 if package in nfu_entries and \
676 version != source_versions[source]: # only suggest to remove out-of-date packages
677 nfu_packages[architecture].append((package,version,source_versions[source]))
680 os.unlink(temp_filename)
682 # Distinguish dubious (version numbers match) and 'real' NBS (they don't)
684 for source in nbs.keys():
685 for package in nbs[source].keys():
686 versions = nbs[source][package].keys()
687 versions.sort(apt_pkg.VersionCompare)
688 latest_version = versions.pop()
689 source_version = source_versions.get(source,"0")
690 if apt_pkg.VersionCompare(latest_version, source_version) == 0:
691 add_nbs(dubious_nbs, source, latest_version, package, suite_id, session)
694 do_newer_version('unstable', 'experimental', 'NVIU', session)
697 do_newer_version('testing', 'testing-proposed-updates', 'NVIT', session)
701 if Options["Mode"] == "full":
709 print "Unbuilt binary packages"
710 print "-----------------------"
712 keys = bin_not_built.keys()
715 binaries = bin_not_built[source].keys()
717 print " o %s: %s" % (source, ", ".join(binaries))
721 report_multiple_source(suite)
723 if "anais" in checks:
724 print "Architecture Not Allowed In Source"
725 print "----------------------------------"
729 if "dubious nbs" in checks:
730 do_dubious_nbs(dubious_nbs)
733 ################################################################################
735 if __name__ == '__main__':