]> git.decadent.org.uk Git - dak.git/blob - tea
[doogie] updates. [me] updates.
[dak.git] / tea
1 #!/usr/bin/env python
2
3 # Sanity check the database
4 # Copyright (C) 2000, 2001  James Troup <james@nocrew.org>
5 # $Id: tea,v 1.6 2001-03-02 02:30:12 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 #   And, lo, a great and menacing voice rose from the depths, and with
22 #   great wrath and vehemence it's voice boomed across the
23 #   land... ``hehehehehehe... that *tickles*''
24 #                                                       -- aj on IRC
25
26 ################################################################################
27
28 import pg, sys, os, string, stat
29 import utils, db_access
30 import apt_pkg;
31
32 ################################################################################
33
34 Cnf = None;
35 projectB = None;
36 db_files = {};
37 waste = 0.0;
38 excluded = {};
39
40 def process_dir (arg, dirname, filenames):
41     global waste, db_files, excluded;
42     
43     if string.find(dirname, '/disks-') != -1 or string.find(dirname, 'upgrade-') != -1: 
44         return;
45     # hack; can't handle .changes files
46     if string.find(dirname, 'proposed-updates') != -1:
47         return;
48     for name in filenames:
49         filename = os.path.abspath(dirname+'/'+name);
50         filename = string.replace(filename, 'potato-proposed-updates', 'proposed-updates');
51         if os.path.isfile(filename) and not os.path.islink(filename) and not db_files.has_key(filename) and not excluded.has_key(filename):
52             waste = waste + os.stat(filename)[stat.ST_SIZE];
53             print filename
54 ################################################################################
55
56 def check_files():
57
58     print "Building list of Database files...";
59
60     q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id")
61     ql = q.getresult();
62
63     db_files = {};
64     for i in ql:
65         filename = os.path.abspath(i[0] + i[1]);
66         db_files[filename] = "";
67         if os.access(filename, os.R_OK) == 0:
68             sys.stderr.write("W: '%s' doesn't exist.\n" % (filename));
69
70     file = utils.open_file(Cnf["Dir::OverrideDir"]+'override.unreferenced','r');
71     for filename in file.readlines():
72         filename = filename[:-1];
73         excluded[filename] = "";
74
75     print "Checking against existent files...";
76
77     os.path.walk(Cnf["Dir::RootDir"]+'dists/', process_dir, None);
78
79     print
80     print "%s wasted..." % (utils.size_type(waste));
81
82 ################################################################################
83
84 def check_dscs():
85     count = 0;
86     suite = 'unstable';
87     for component in Cnf.SubTree("Component").List():
88         if component == "mixed":
89             continue;
90         component = string.lower(component);
91         list_filename = '%s%s_%s_source.list' % (Cnf["Dir::ListsDir"], suite, component);
92         list_file = utils.open_file(list_filename, 'r');
93         for line in list_file.readlines():
94             file = line[:-1];
95             try:
96                 utils.parse_changes(file, 1);
97             except utils.invalid_dsc_format_exc, line:
98                 sys.stderr.write("E: syntax error in .dsc file '%s', line %s.\n" % (file, line));
99                 count = count + 1;
100
101     if count:
102         sys.stderr.write("Found %s invalid .dsc files.\n" % (count));
103
104 ################################################################################
105
106 def check_override():
107     for suite in [ "stable", "unstable" ]:
108         print suite
109         print "-------------"
110         print
111         suite_id = db_access.get_suite_id(suite);
112         q = projectB.query("""
113 SELECT DISTINCT b.package FROM binaries b, bin_associations ba
114  WHERE b.id = ba.bin AND ba.suite = %s AND NOT EXISTS
115        (SELECT * FROM override o WHERE o.suite = %s AND o.package = b.package)"""
116                            % (suite_id, suite_id));
117         print q
118         q = projectB.query("""
119 SELECT DISTINCT s.source FROM source s, src_associations sa
120   WHERE s.id = sa.source AND sa.suite = %s AND NOT EXISTS
121        (SELECT * FROM override o WHERE o.suite = %s and o.package = s.source)"""
122                            % (suite_id, suite_id));
123         print q
124
125 ################################################################################
126
127 # Ensure that the source files for any given package is all in one
128 # directory so that 'apt-get source' works...
129
130 def check_source_in_one_dir():
131     # Not the most enterprising method, but hey...
132     broken_count = 0;
133     q = projectB.query("SELECT id FROM source;");
134     for i in q.getresult():
135         source_id = i[0];
136         q2 = projectB.query("""
137 SELECT f.filename FROM files f, dsc_files df WHERE df.source = %s AND f.id = df.file"""
138                             % (source_id));
139         first_path = "";
140         broken = 0;
141         for j in q2.getresult():
142             path = os.path.dirname(j[0]);
143             if first_path == "":
144                 first_path = path;
145             elif first_path != path:
146                 broken = 1;
147                 #print "Woah, we got a live one here... %s" % (source_id);
148         if broken:
149             broken_count = broken_count + 1;
150             print q2
151     print "Found %d source packages where the source is not all in one directory." % (broken_count);
152
153 ################################################################################
154
155 def check_md5sums():
156     print "Getting file information from database...";
157     q = projectB.query("SELECT l.path, f.filename, f.md5sum, f.size FROM files f, location l WHERE f.location = l.id")
158     ql = q.getresult();
159     
160     print "Checking file md5sums & sizes...";
161     for i in ql:
162         filename = os.path.abspath(i[0] + i[1]);
163         db_md5sum = i[2];
164         db_size = int(i[3]);
165         try:
166             file = utils.open_file(filename, 'r');
167         except:
168             sys.stderr.write("E: can't open '%s'.\n" % (filename));
169             continue;
170         md5sum = apt_pkg.md5sum(file);
171         size = os.stat(filename)[stat.ST_SIZE];
172         if md5sum != db_md5sum:
173             sys.stderr.write("E: **WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db]).\n" % (filename, md5sum, db_md5sum));
174         if size != db_size:
175             sys.stderr.write("E: **WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db]).\n" % (filename, size, db_size));
176
177     print "Done."
178
179 ################################################################################
180
181 def main ():
182     global Cnf, projectB, db_files, waste, excluded;
183
184     apt_pkg.init();
185     
186     Cnf = apt_pkg.newConfiguration();
187     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
188
189     Arguments = [('d',"debug","Tea::Options::Debug", "IntVal"),
190                  ('h',"help","Tea::Options::Help"),
191                  ('v',"version","Tea::Options::Version")];
192
193     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
194     projectB = pg.connect('projectb', 'localhost');
195     db_access.init(Cnf, projectB);
196
197     check_md5sums();
198     #check_source_in_one_dir();
199     #check_override();
200     #check_dscs();
201     #check_files();
202
203 #######################################################################################
204
205 if __name__ == '__main__':
206     main()
207