]> git.decadent.org.uk Git - dak.git/blob - rene
e9a9c04bc3f37a827ed5b615ddddc1e4c9db146a
[dak.git] / rene
1 #!/usr/bin/env python
2
3 # Check for obsolete binary packages
4 # Copyright (C) 2000, 2001, 2002  James Troup <james@nocrew.org>
5 # $Id: rene,v 1.16 2003-01-02 18:10:02 troup Exp $
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 ################################################################################
22
23 # ``If you're claiming that's a "problem" that needs to be "fixed",
24 #   you might as well write some letters to God about how unfair entropy
25 #   is while you're at it.'' -- 20020802143104.GA5628@azure.humbug.org.au
26
27 ################################################################################
28
29 import commands, pg, os, sys, tempfile;
30 import utils, db_access;
31 import apt_pkg;
32
33 ################################################################################
34
35 Cnf = None;
36 projectB = None;
37
38 ################################################################################
39
40 def usage(exit_code=0):
41     print """Usage: rene
42 Check for obsolete or duplicated packages.
43
44   -h, --help                show this help and exit."""
45     sys.exit(exit_code)
46
47 ################################################################################
48
49 def main ():
50     global Cnf, projectB;
51
52     Cnf = utils.get_conf();
53
54     Arguments = [('h',"help","Rene::Options::Help")];
55     for i in [ "help" ]:
56         if not Cnf.has_key("Rene::Options::%s" % (i)):
57             Cnf["Rene::Options::%s" % (i)] = "";
58
59     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv);
60
61     Options = Cnf.SubTree("Rene::Options")
62     if Options["Help"]:
63         usage();
64
65     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
66     db_access.init(Cnf, projectB);
67
68     bin_pkgs = {};
69     miss_src = {};
70     src_pkgs = {};
71     source_binaries = {};
72
73     suite = "unstable";
74     suite_id = db_access.get_suite_id(suite);
75
76     components = Cnf.ValueList("Suite::%s::Components" % (suite));
77     for component in components:
78         filename = "%s/dists/%s/%s/source/Sources.gz" % (Cnf["Dir::Root"], suite, component);
79         # apt_pkg.ParseTagFile needs a real file handle and can't handle a GzipFile instance...
80         temp_filename = tempfile.mktemp();
81         fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
82         os.close(fd);
83         (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename));
84         if (result != 0):
85             sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output));
86             sys.exit(result);
87         sources = utils.open_file(temp_filename);
88         Sources = apt_pkg.ParseTagFile(sources);
89         while Sources.Step():
90             source = Sources.Section.Find('Package');
91             architecture = Sources.Section.Find('Architecture');
92             binaries = Sources.Section.Find('Binary');
93
94             # Check for packages built on architectures they shouldn't be.
95             if architecture != "any" and architecture != "all":
96                 architectures = {};
97                 for arch in architecture.split():
98                     architectures[arch.strip()] = "";
99                 for binary in binaries.split(','):
100                     binary = binary.strip();
101                     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));
102                     ql = q.getresult();
103                     if not ql:
104                         utils.warn("%s lists %s as a binary, but it doesn't seem to exist in %s?" % (source, binary, suite));
105                     # Loop around twice; first to get the latest 'valid' version
106                     versions = [];
107                     for i in q.getresult():
108                         arch = i[0];
109                         version = i[1];
110                         if architectures.has_key(arch):
111                             versions.append(version);
112                     versions.sort(apt_pkg.VersionCompare);
113                     if versions:
114                         latest_version = versions.pop()
115                     else:
116                         latest_version = None;
117                     # ... then to check for 'invalid' architectures
118                     for i in q.getresult():
119                         arch = i[0];
120                         version = i[1];
121                         if not architectures.has_key(arch):
122                             print "[%s]: %s appears for %s (vs. '%s')" % (source, binary, arch, architecture),
123                             if not latest_version:
124                                 print "** mwaap, mwapp!  Ignore me **";
125                                 continue;
126                             if apt_pkg.VersionCompare(latest_version, version) != -1:
127                                 print "- out of date.",
128                             else:
129                                 print "- current.",
130                             print "[%s vs %s (%s)]" % (latest_version, version, arch);
131
132             # Check for duplicated packages and build indices for checking "no source" later
133             source_index = component + '/' + source;
134             if src_pkgs.has_key(source):
135                 print " %s is a duplicated source package (%s and %s)" % (source, source_index, src_pkgs[source]);
136             src_pkgs[source] = source_index;
137             for binary in binaries.split(','):
138                 binary = binary.strip();
139                 if bin_pkgs.has_key(binary):
140                     print " binary %s is duplicated in source packages %s and %s" % (binary, source, bin_pkgs[binary]);
141                 bin_pkgs[binary] = source;
142             source_binaries[source] = binaries;
143
144         sources.close();
145         os.unlink(temp_filename);
146
147     for component in components:
148         architectures = filter(utils.real_arch, Cnf.ValueList("Suite::%s::Architectures" % (suite)));
149         for architecture in architectures:
150             filename = "%s/dists/%s/%s/binary-%s/Packages" % (Cnf["Dir::Root"], suite, component, architecture);
151             packages = utils.open_file(filename);
152             Packages = apt_pkg.ParseTagFile(packages);
153             while Packages.Step():
154                 package = Packages.Section.Find('Package');
155                 source = Packages.Section.Find('Source', "");
156                 if source == "":
157                     source = package;
158                 if source.find("(") != -1:
159                     m = utils.re_extract_src_version.match(source)
160                     source = m.group(1)
161                 if not bin_pkgs.has_key(package) and not miss_src.has_key(package):
162                     miss_src[package] = 1;
163                     print " %s has no source [%s: %s]" % (package, source, source_binaries.get(source, "(source does not exist)"));
164             packages.close();
165
166     # Check for packages in experimental obsoleted by versions in unstable
167     #
168     # [If melanie was callable from python, we could auto-remove these
169     #  packages...]
170
171     suite_id = db_access.get_suite_id("unstable");
172     q = projectB.query("""
173 SELECT s.source, s.version AS experimental, s2.version AS unstable
174   FROM src_associations sa, source s, source s2, src_associations sa2
175   WHERE sa.suite = 1 AND sa2.suite = %d AND sa.source = s.id
176    AND sa2.source = s2.id AND s.source = s2.source
177    AND versioncmp(s.version, s2.version) < 0""" % (suite_id));
178     ql = q.getresult();
179     if ql:
180         print
181         print q
182
183 ################################################################################
184
185 if __name__ == '__main__':
186     main()