]> git.decadent.org.uk Git - dak.git/blob - dak/cruft_report.py
Merge remote-tracking branch 'drkranz/cruft' into merge
[dak.git] / dak / cruft_report.py
1 #!/usr/bin/env python
2
3 """
4 Check for obsolete binary packages
5
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
10 """
11
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.
16
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.
21
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
25
26 ################################################################################
27
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
31
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
35
36 ################################################################################
37
38 import commands, os, sys, re
39 import apt_pkg
40
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 *
46
47 ################################################################################
48
49 no_longer_in_suite = {}; # Really should be static to add_nbs, but I'm lazy
50
51 source_binaries = {}
52 source_versions = {}
53
54 ################################################################################
55
56 def usage(exit_code=0):
57     print """Usage: dak cruft-report
58 Check for obsolete or duplicated packages.
59
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   -w, --wanna-build-dump    where to find the copies of http://buildd.debian.org/stats/*.txt"""
64     sys.exit(exit_code)
65
66 ################################################################################
67
68 def add_nbs(nbs_d, source, version, package, suite_id, session):
69     # Ensure the package is still in the suite (someone may have already removed it)
70     if no_longer_in_suite.has_key(package):
71         return
72     else:
73         q = session.execute("""SELECT b.id FROM binaries b, bin_associations ba
74                                 WHERE ba.bin = b.id AND ba.suite = :suite_id
75                                   AND b.package = :package LIMIT 1""", {'suite_id': suite_id,
76                                                                          'package': package})
77         if not q.fetchall():
78             no_longer_in_suite[package] = ""
79             return
80
81     nbs_d.setdefault(source, {})
82     nbs_d[source].setdefault(version, {})
83     nbs_d[source][version][package] = ""
84
85 ################################################################################
86
87 # Check for packages built on architectures they shouldn't be.
88 def do_anais(architecture, binaries_list, source, session):
89     if architecture == "any" or architecture == "all":
90         return ""
91
92     anais_output = ""
93     architectures = {}
94     for arch in architecture.split():
95         architectures[arch.strip()] = ""
96     for binary in binaries_list:
97         q = session.execute("""SELECT a.arch_string, b.version
98                                 FROM binaries b, bin_associations ba, architecture a
99                                WHERE ba.suite = :suiteid AND ba.bin = b.id
100                                  AND b.architecture = a.id AND b.package = :package""",
101                              {'suiteid': suite_id, 'package': binary})
102         ql = q.fetchall()
103         versions = []
104         for i in ql:
105             arch = i[0]
106             version = i[1]
107             if architectures.has_key(arch):
108                 versions.append(version)
109         versions.sort(apt_pkg.VersionCompare)
110         if versions:
111             latest_version = versions.pop()
112         else:
113             latest_version = None
114         # Check for 'invalid' architectures
115         versions_d = {}
116         for i in ql:
117             arch = i[0]
118             version = i[1]
119             if not architectures.has_key(arch):
120                 versions_d.setdefault(version, [])
121                 versions_d[version].append(arch)
122
123         if versions_d != {}:
124             anais_output += "\n (*) %s_%s [%s]: %s\n" % (binary, latest_version, source, architecture)
125             versions = versions_d.keys()
126             versions.sort(apt_pkg.VersionCompare)
127             for version in versions:
128                 arches = versions_d[version]
129                 arches.sort()
130                 anais_output += "    o %s: %s\n" % (version, ", ".join(arches))
131     return anais_output
132
133
134 ################################################################################
135
136 # Check for out-of-date binaries on architectures that do not want to build that
137 # package any more, and have them listed as Not-For-Us
138 def do_nfu(nfu_packages):
139     output = ""
140     
141     a2p = {}
142
143     for architecture in nfu_packages:
144         a2p[architecture] = []
145         for (package,bver,sver) in nfu_packages[architecture]:
146             output += "  * [%s] does not want %s (binary %s, source %s)\n" % (architecture, package, bver, sver)
147             a2p[architecture].append(package)
148
149
150     if output:
151         print "Obsolete by Not-For-Us"
152         print "----------------------"
153         print
154         print output
155
156         print "Suggested commands:"
157         for architecture in a2p:
158             if a2p[architecture]:
159                 print (" dak rm -m \"[auto-cruft] NFU\" -s %s -a %s -b %s" % 
160                     (suite.suite_name, architecture, " ".join(a2p[architecture])))
161         print
162
163 def parse_nfu(architecture):
164     cnf = Config()
165     # utils/hpodder_1.1.5.0: Not-For-Us [optional:out-of-date]
166     r = re.compile("^\w+/([^_]+)_.*: Not-For-Us")
167
168     ret = set()
169     
170     filename = "%s/%s-all.txt" % (cnf["Cruft-Report::Options::Wanna-Build-Dump"], architecture)
171
172     # Not all architectures may have a wanna-build dump, so we want to ignore missin
173     # files
174     if os.path.exists(filename):
175         f = utils.open_file(filename)
176         for line in f:
177             if line[0] == ' ':
178                 continue
179
180             m = r.match(line)
181             if m:
182                 ret.add(m.group(1))
183
184         f.close()
185     else:
186         utils.warn("No wanna-build dump file for architecture %s" % architecture)
187     return ret
188
189 ################################################################################
190
191 def do_newer_version(lowersuite_name, highersuite_name, code, session):
192     list = newer_version(lowersuite_name, highersuite_name, session)
193     if len(list) > 0:
194         nv_to_remove = []
195         title = "Newer version in %s" % lowersuite_name
196         print title
197         print "-" * len(title)
198         print
199         for i in list:
200             (source, higher_version, lower_version) = i
201             print " o %s (%s, %s)" % (source, higher_version, lower_version)
202             nv_to_remove.append(source)
203         print
204         print "Suggested command:"
205         print " dak rm -m \"[auto-cruft] %s\" -s %s %s" % (code, highersuite_name,
206                                                            " ".join(nv_to_remove))
207         print
208
209 ################################################################################
210
211 def queryWithoutSource(suite_id, session):
212     """searches for arch: all packages from suite that do no longer
213     reference a source package in the same suite
214
215     subquery unique_binaries: selects all packages with only 1 version
216     in suite since 'dak rm' does not allow to specify version numbers"""
217
218     query = """
219     with unique_binaries as
220         (select package, max(version) as version, max(source) as source
221             from bin_associations_binaries
222             where architecture = 2 and suite = :suite_id
223             group by package having count(*) = 1)
224     select ub.package, ub.version
225         from unique_binaries ub
226         left join src_associations_src sas
227             on ub.source = sas.src and sas.suite = :suite_id
228         where sas.id is null
229         order by ub.package"""
230     return session.execute(query, { 'suite_id': suite_id })
231
232 def reportWithoutSource(suite_name, suite_id, session):
233     rows = queryWithoutSource(suite_id, session)
234     title = 'packages without source in suite %s' % suite_name
235     if rows.rowcount > 0:
236         print '%s\n%s\n' % (title, '-' * len(title))
237     message = '"[auto-cruft] no longer built from source"'
238     for row in rows:
239         (package, version) = row
240         print "* package %s in version %s is no longer built from source" % \
241             (package, version)
242         print "  - suggested command:"
243         print "    dak rm -m %s -s %s -a all -p -R -b %s\n" % \
244             (message, suite_name, package)
245
246 def queryNewerAll(suite_name, session):
247     """searches for arch != all packages that have an arch == all
248     package with a higher version in the same suite"""
249
250     query = """
251 select bab1.package, bab1.version as oldver,
252     array_to_string(array_agg(a.arch_string), ',') as oldarch,
253     bab2.version as newver
254     from bin_associations_binaries bab1
255     join bin_associations_binaries bab2
256         on bab1.package = bab2.package and bab1.version < bab2.version and
257         bab1.suite = bab2.suite and bab1.architecture > 2 and
258         bab2.architecture = 2
259     join architecture a on bab1.architecture = a.id
260     join suite s on bab1.suite = s.id
261     where s.suite_name = :suite_name
262     group by bab1.package, oldver, bab1.suite, newver"""
263     return session.execute(query, { 'suite_name': suite_name })
264
265 def reportNewerAll(suite_name, session):
266     rows = queryNewerAll(suite_name, session)
267     title = 'obsolete arch any packages in suite %s' % suite_name
268     if rows.rowcount > 0:
269         print '%s\n%s\n' % (title, '-' * len(title))
270     message = '"[auto-cruft] obsolete arch any package"'
271     for row in rows:
272         (package, oldver, oldarch, newver) = row
273         print "* package %s is arch any in version %s but arch all in version %s" % \
274             (package, oldver, newver)
275         print "  - suggested command:"
276         print "    dak rm -m %s -s %s -a %s -p -b %s\n" % \
277             (message, suite_name, oldarch, package)
278
279 def queryNBS(suite_id, session):
280     """This one is really complex. It searches arch != all packages that
281     are no longer built from current source packages in suite.
282
283     temp table unique_binaries: will be populated with packages that
284     have only one version in suite because 'dak rm' does not allow
285     specifying version numbers
286
287     temp table newest_binaries: will be populated with packages that are
288     built from current sources
289
290     subquery uptodate_arch: returns all architectures built from current
291     sources
292
293     subquery unique_binaries_uptodate_arch: returns all packages in
294     architectures from uptodate_arch
295
296     subquery unique_binaries_uptodate_arch_agg: same as
297     unique_binaries_uptodate_arch but with column architecture
298     aggregated to array
299
300     subquery uptodate_packages: similar to uptodate_arch but returns all
301     packages built from current sources
302
303     subquery outdated_packages: returns all packages with architectures
304     no longer built from current source
305     """
306
307     query = """
308 create temp table unique_binaries (
309     package text not null,
310     architecture integer not null,
311     source integer not null);
312
313 insert into unique_binaries
314     select bab.package, bab.architecture, max(bab.source)
315         from bin_associations_binaries bab
316         where bab.suite = :suite_id and bab.architecture > 2
317         group by package, architecture having count(*) = 1;
318
319 create temp table newest_binaries (
320     package text not null,
321     architecture integer not null,
322     source text not null,
323     version debversion not null);
324
325 insert into newest_binaries
326     select ub.package, ub.architecture, nsa.source, nsa.version
327         from unique_binaries ub
328         join newest_src_association nsa
329             on ub.source = nsa.src and nsa.suite = :suite_id;
330
331 with uptodate_arch as
332     (select architecture, source, version
333         from newest_binaries
334         group by architecture, source, version),
335     unique_binaries_uptodate_arch as
336     (select ub.package, ub.architecture, ua.source, ua.version
337         from unique_binaries ub
338         join source s
339             on ub.source = s.id
340         join uptodate_arch ua
341             on ub.architecture = ua.architecture and s.source = ua.source),
342     unique_binaries_uptodate_arch_agg as
343     (select ubua.package,
344         array(select unnest(array_agg(a.arch_string)) order by 1) as arch_list,
345         ubua.source, ubua.version
346         from unique_binaries_uptodate_arch ubua
347         join architecture a
348             on ubua.architecture = a.id
349         group by ubua.source, ubua.version, ubua.package),
350     uptodate_packages as
351     (select package, source, version
352         from newest_binaries
353         group by package, source, version),
354     outdated_packages as
355     (select array(select unnest(array_agg(package)) order by 1) as pkg_list,
356         arch_list, source, version
357         from unique_binaries_uptodate_arch_agg
358         where package not in
359             (select package from uptodate_packages)
360         group by arch_list, source, version)
361     select * from outdated_packages order by source"""
362     return session.execute(query, { 'suite_id': suite_id })
363
364 def reportNBS(suite_name, suite_id):
365     session = DBConn().session()
366     nbsRows = queryNBS(suite_id, session)
367     title = 'NBS packages in suite %s' % suite_name
368     if nbsRows.rowcount > 0:
369         print '%s\n%s\n' % (title, '-' * len(title))
370     for row in nbsRows:
371         (pkg_list, arch_list, source, version) = row
372         pkg_string = ' '.join(pkg_list)
373         arch_string = ','.join(arch_list)
374         print "* source package %s version %s no longer builds" % \
375             (source, version)
376         print "  binary package(s): %s" % pkg_string
377         print "  on %s" % arch_string
378         print "  - suggested command:"
379         message = '"[auto-cruft] NBS (no longer built by %s)"' % source
380         print "    dak rm -m %s -s %s -a %s -p -R -b %s\n" % \
381             (message, suite_name, arch_string, pkg_string)
382     session.close()
383
384 def reportAllNBS(suite_name, suite_id, session):
385     reportWithoutSource(suite_name, suite_id, session)
386     reportNewerAll(suite_name, session)
387     reportNBS(suite_name, suite_id)
388
389 ################################################################################
390
391 def do_dubious_nbs(dubious_nbs):
392     print "Dubious NBS"
393     print "-----------"
394     print
395
396     dubious_nbs_keys = dubious_nbs.keys()
397     dubious_nbs_keys.sort()
398     for source in dubious_nbs_keys:
399         print " * %s_%s builds: %s" % (source,
400                                        source_versions.get(source, "??"),
401                                        source_binaries.get(source, "(source does not exist)"))
402         print "      won't admit to building:"
403         versions = dubious_nbs[source].keys()
404         versions.sort(apt_pkg.VersionCompare)
405         for version in versions:
406             packages = dubious_nbs[source][version].keys()
407             packages.sort()
408             print "        o %s: %s" % (version, ", ".join(packages))
409
410         print
411
412 ################################################################################
413
414 def obsolete_source(suite_name, session):
415     """returns obsolete source packages for suite_name without binaries
416     in the same suite sorted by install_date; install_date should help
417     detecting source only (or binary throw away) uploads; duplicates in
418     the suite are skipped
419
420     subquery 'source_suite_unique' returns source package names from
421     suite without duplicates; the rationale behind is that neither
422     cruft-report nor rm cannot handle duplicates (yet)"""
423
424     query = """
425 WITH source_suite_unique AS
426     (SELECT source, suite
427         FROM source_suite GROUP BY source, suite HAVING count(*) = 1)
428 SELECT ss.src, ss.source, ss.version,
429     to_char(ss.install_date, 'YYYY-MM-DD') AS install_date
430     FROM source_suite ss
431     JOIN source_suite_unique ssu
432         ON ss.source = ssu.source AND ss.suite = ssu.suite
433     JOIN suite s ON s.id = ss.suite
434     LEFT JOIN bin_associations_binaries bab
435         ON ss.src = bab.source AND ss.suite = bab.suite
436     WHERE s.suite_name = :suite_name AND bab.id IS NULL
437     ORDER BY install_date"""
438     args = { 'suite_name': suite_name }
439     return session.execute(query, args)
440
441 def source_bin(source, session):
442     """returns binaries built by source for all or no suite grouped and
443     ordered by package name"""
444
445     query = """
446 SELECT b.package
447     FROM binaries b
448     JOIN src_associations_src sas ON b.source = sas.src
449     WHERE sas.source = :source
450     GROUP BY b.package
451     ORDER BY b.package"""
452     args = { 'source': source }
453     return session.execute(query, args)
454
455 def newest_source_bab(suite_name, package, session):
456     """returns newest source that builds binary package in suite grouped
457     and sorted by source and package name"""
458
459     query = """
460 SELECT sas.source, MAX(sas.version) AS srcver
461     FROM src_associations_src sas
462     JOIN bin_associations_binaries bab ON sas.src = bab.source
463     JOIN suite s on s.id = bab.suite
464     WHERE s.suite_name = :suite_name AND bab.package = :package
465         GROUP BY sas.source, bab.package
466         ORDER BY sas.source, bab.package"""
467     args = { 'suite_name': suite_name, 'package': package }
468     return session.execute(query, args)
469
470 def report_obsolete_source(suite_name, session):
471     rows = obsolete_source(suite_name, session)
472     if rows.rowcount == 0:
473         return
474     print \
475 """Obsolete source packages in suite %s
476 ----------------------------------%s\n""" % \
477         (suite_name, '-' * len(suite_name))
478     for os_row in rows.fetchall():
479         (src, old_source, version, install_date) = os_row
480         print " * obsolete source %s version %s installed at %s" % \
481             (old_source, version, install_date)
482         for sb_row in source_bin(old_source, session):
483             (package, ) = sb_row
484             print "   - has built binary %s" % package
485             for nsb_row in newest_source_bab(suite_name, package, session):
486                 (new_source, srcver) = nsb_row
487                 print "     currently built by source %s version %s" % \
488                     (new_source, srcver)
489         print "   - suggested command:"
490         rm_opts = "-S -p -m \"[auto-cruft] obsolete source package\""
491         print "     dak rm -s %s %s %s\n" % (suite_name, rm_opts, old_source)
492
493 def get_suite_binaries(suite, session):
494     # Initalize a large hash table of all binary packages
495     binaries = {}
496
497     print "Getting a list of binary packages in %s..." % suite.suite_name
498     q = session.execute("""SELECT distinct b.package
499                              FROM binaries b, bin_associations ba
500                             WHERE ba.suite = :suiteid AND ba.bin = b.id""",
501                            {'suiteid': suite.suite_id})
502     for i in q.fetchall():
503         binaries[i[0]] = ""
504
505     return binaries
506
507 ################################################################################
508
509 def report_outdated_nonfree(suite, session):
510
511     packages = {}
512     query = """WITH outdated_sources AS (
513                  SELECT s.source, s.version, s.id
514                  FROM source s
515                  JOIN src_associations sa ON sa.source = s.id
516                  WHERE sa.suite IN (
517                    SELECT id
518                    FROM suite
519                    WHERE suite_name = :suite )
520                  AND sa.created < (now() - interval :delay)
521                  EXCEPT SELECT s.source, max(s.version) AS version, max(s.id)
522                  FROM source s
523                  JOIN src_associations sa ON sa.source = s.id
524                  WHERE sa.suite IN (
525                    SELECT id
526                    FROM suite
527                    WHERE suite_name = :suite )
528                  AND sa.created < (now() - interval :delay)
529                  GROUP BY s.source ),
530                binaries AS (
531                  SELECT b.package, s.source, (
532                    SELECT a.arch_string
533                    FROM architecture a
534                    WHERE a.id = b.architecture ) AS arch
535                  FROM binaries b
536                  JOIN outdated_sources s ON s.id = b.source
537                  JOIN bin_associations ba ON ba.bin = b.id
538                  JOIN override o ON o.package = b.package AND o.suite = ba.suite
539                  WHERE ba.suite IN (
540                    SELECT id
541                    FROM suite
542                    WHERE suite_name = :suite )
543                  AND o.component IN (
544                    SELECT id
545                    FROM component
546                    WHERE name = 'non-free' ) )
547                SELECT DISTINCT package, source, arch
548                FROM binaries
549                ORDER BY source, package, arch"""
550
551     res = session.execute(query, {'suite': suite, 'delay': "'15 days'"})
552     for package in res:
553         binary = package[0]
554         source = package[1]
555         arch = package[2]
556         if arch == 'all':
557             continue
558         if not source in packages:
559             packages[source] = {}
560         if not binary in packages[source]:
561             packages[source][binary] = set()
562         packages[source][binary].add(arch)
563     if packages:
564         title = 'Outdated non-free binaries in suite %s' % suite
565         message = '"[auto-cruft] outdated non-free binaries"'
566         print '%s\n%s\n' % (title, '-' * len(title))
567         for source in sorted(packages):
568             archs = set()
569             binaries = set()
570             print '* package %s has outdated non-free binaries' % source
571             print '  - suggested command:'
572             for binary in sorted(packages[source]):
573                 binaries.add(binary)
574                 archs = archs.union(packages[source][binary])
575             print '    dak rm -m %s -s %s -a %s -p -R -b %s\n' % \
576                    (message, suite, ','.join(archs), ' '.join(binaries))
577
578 ################################################################################
579
580 def main ():
581     global suite, suite_id, source_binaries, source_versions
582
583     cnf = Config()
584
585     Arguments = [('h',"help","Cruft-Report::Options::Help"),
586                  ('m',"mode","Cruft-Report::Options::Mode", "HasArg"),
587                  ('s',"suite","Cruft-Report::Options::Suite","HasArg"),
588                  ('w',"wanna-build-dump","Cruft-Report::Options::Wanna-Build-Dump","HasArg")]
589     for i in [ "help" ]:
590         if not cnf.has_key("Cruft-Report::Options::%s" % (i)):
591             cnf["Cruft-Report::Options::%s" % (i)] = ""
592
593     cnf["Cruft-Report::Options::Suite"] = cnf.get("Dinstall::DefaultSuite", "unstable")
594
595     if not cnf.has_key("Cruft-Report::Options::Mode"):
596         cnf["Cruft-Report::Options::Mode"] = "daily"
597
598     if not cnf.has_key("Cruft-Report::Options::Wanna-Build-Dump"):
599         cnf["Cruft-Report::Options::Wanna-Build-Dump"] = "/srv/ftp-master.debian.org/scripts/nfu"
600
601     apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
602
603     Options = cnf.SubTree("Cruft-Report::Options")
604     if Options["Help"]:
605         usage()
606
607     # Set up checks based on mode
608     if Options["Mode"] == "daily":
609         checks = [ "nbs", "nviu", "nvit", "obsolete source", "outdated non-free", "nfu" ]
610     elif Options["Mode"] == "full":
611         checks = [ "nbs", "nviu", "nvit", "obsolete source", "outdated non-free", "nfu", "dubious nbs", "bnb", "bms", "anais" ]
612     elif Options["Mode"] == "bdo":
613         checks = [ "nbs",  "obsolete source" ]
614     else:
615         utils.warn("%s is not a recognised mode - only 'full', 'daily' or 'bdo' are understood." % (Options["Mode"]))
616         usage(1)
617
618     session = DBConn().session()
619
620     bin_pkgs = {}
621     src_pkgs = {}
622     bin2source = {}
623     bins_in_suite = {}
624     nbs = {}
625     source_versions = {}
626
627     anais_output = ""
628
629     nfu_packages = {}
630
631     suite = get_suite(Options["Suite"].lower(), session)
632     if not suite:
633         utils.fubar("Cannot find suite %s" % Options["Suite"].lower())
634
635     suite_id = suite.suite_id
636     suite_name = suite.suite_name.lower()
637
638     if "obsolete source" in checks:
639         report_obsolete_source(suite_name, session)
640
641     if "nbs" in checks:
642         reportAllNBS(suite_name, suite_id, session)
643
644     if "outdated non-free" in checks:
645         report_outdated_nonfree(suite_name, session)
646
647     bin_not_built = {}
648
649     if "bnb" in checks:
650         bins_in_suite = get_suite_binaries(suite, session)
651
652     # Checks based on the Sources files
653     components = get_component_names(session)
654     for component in components:
655         filename = "%s/dists/%s/%s/source/Sources.gz" % (cnf["Dir::Root"], suite_name, component)
656         # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
657         (fd, temp_filename) = utils.temp_filename()
658         (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
659         if (result != 0):
660             sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
661             sys.exit(result)
662         sources = utils.open_file(temp_filename)
663         Sources = apt_pkg.ParseTagFile(sources)
664         while Sources.Step():
665             source = Sources.Section.Find('Package')
666             source_version = Sources.Section.Find('Version')
667             architecture = Sources.Section.Find('Architecture')
668             binaries = Sources.Section.Find('Binary')
669             binaries_list = [ i.strip() for i in  binaries.split(',') ]
670
671             if "bnb" in checks:
672                 # Check for binaries not built on any architecture.
673                 for binary in binaries_list:
674                     if not bins_in_suite.has_key(binary):
675                         bin_not_built.setdefault(source, {})
676                         bin_not_built[source][binary] = ""
677
678             if "anais" in checks:
679                 anais_output += do_anais(architecture, binaries_list, source, session)
680
681             # build indices for checking "no source" later
682             source_index = component + '/' + source
683             src_pkgs[source] = source_index
684             for binary in binaries_list:
685                 bin_pkgs[binary] = source
686             source_binaries[source] = binaries
687             source_versions[source] = source_version
688
689         sources.close()
690         os.unlink(temp_filename)
691
692     # Checks based on the Packages files
693     check_components = components[:]
694     if suite_name != "experimental":
695         check_components.append('main/debian-installer');
696
697     for component in check_components:
698         architectures = [ a.arch_string for a in get_suite_architectures(suite_name,
699                                                                          skipsrc=True, skipall=True,
700                                                                          session=session) ]
701         for architecture in architectures:
702             if component == 'main/debian-installer' and re.match("kfreebsd", architecture):
703                 continue
704             filename = "%s/dists/%s/%s/binary-%s/Packages.gz" % (cnf["Dir::Root"], suite_name, component, architecture)
705             # apt_pkg.ParseTagFile needs a real file handle
706             (fd, temp_filename) = utils.temp_filename()
707             (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
708             if (result != 0):
709                 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
710                 sys.exit(result)
711
712             if "nfu" in checks:
713                 nfu_packages.setdefault(architecture,[])
714                 nfu_entries = parse_nfu(architecture)
715
716             packages = utils.open_file(temp_filename)
717             Packages = apt_pkg.ParseTagFile(packages)
718             while Packages.Step():
719                 package = Packages.Section.Find('Package')
720                 source = Packages.Section.Find('Source', "")
721                 version = Packages.Section.Find('Version')
722                 if source == "":
723                     source = package
724                 if bin2source.has_key(package) and \
725                        apt_pkg.VersionCompare(version, bin2source[package]["version"]) > 0:
726                     bin2source[package]["version"] = version
727                     bin2source[package]["source"] = source
728                 else:
729                     bin2source[package] = {}
730                     bin2source[package]["version"] = version
731                     bin2source[package]["source"] = source
732                 if source.find("(") != -1:
733                     m = re_extract_src_version.match(source)
734                     source = m.group(1)
735                     version = m.group(2)
736                 if not bin_pkgs.has_key(package):
737                     nbs.setdefault(source,{})
738                     nbs[source].setdefault(package, {})
739                     nbs[source][package][version] = ""
740                 else:
741                     if "nfu" in checks:
742                         if package in nfu_entries and \
743                                version != source_versions[source]: # only suggest to remove out-of-date packages
744                             nfu_packages[architecture].append((package,version,source_versions[source]))
745                     
746             packages.close()
747             os.unlink(temp_filename)
748
749     # Distinguish dubious (version numbers match) and 'real' NBS (they don't)
750     dubious_nbs = {}
751     for source in nbs.keys():
752         for package in nbs[source].keys():
753             versions = nbs[source][package].keys()
754             versions.sort(apt_pkg.VersionCompare)
755             latest_version = versions.pop()
756             source_version = source_versions.get(source,"0")
757             if apt_pkg.VersionCompare(latest_version, source_version) == 0:
758                 add_nbs(dubious_nbs, source, latest_version, package, suite_id, session)
759
760     if "nviu" in checks:
761         do_newer_version('unstable', 'experimental', 'NVIU', session)
762
763     if "nvit" in checks:
764         do_newer_version('testing', 'testing-proposed-updates', 'NVIT', session)
765
766     ###
767
768     if Options["Mode"] == "full":
769         print "="*75
770         print
771
772     if "nfu" in checks:
773         do_nfu(nfu_packages)
774
775     if "bnb" in checks:
776         print "Unbuilt binary packages"
777         print "-----------------------"
778         print
779         keys = bin_not_built.keys()
780         keys.sort()
781         for source in keys:
782             binaries = bin_not_built[source].keys()
783             binaries.sort()
784             print " o %s: %s" % (source, ", ".join(binaries))
785         print
786
787     if "bms" in checks:
788         report_multiple_source(suite)
789
790     if "anais" in checks:
791         print "Architecture Not Allowed In Source"
792         print "----------------------------------"
793         print anais_output
794         print
795
796     if "dubious nbs" in checks:
797         do_dubious_nbs(dubious_nbs)
798
799
800 ################################################################################
801
802 if __name__ == '__main__':
803     main()