]> git.decadent.org.uk Git - dak.git/blob - tea
add date checking support
[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.15 2002-01-19 18:57:49 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, time
29 import utils, db_access
30 import apt_pkg, apt_inst;
31
32 ################################################################################
33
34 Cnf = None;
35 projectB = None;
36 db_files = {};
37 waste = 0.0;
38 excluded = {};
39 current_file = None;
40 future_files = {};
41 current_time = time.time();
42
43 ################################################################################
44
45 def process_dir (arg, dirname, filenames):
46     global waste, db_files, excluded;
47
48     if string.find(dirname, '/disks-') != -1 or string.find(dirname, 'upgrade-') != -1:
49         return;
50     # hack; can't handle .changes files
51     if string.find(dirname, 'proposed-updates') != -1:
52         return;
53     for name in filenames:
54         filename = os.path.abspath(dirname+'/'+name);
55         filename = string.replace(filename, 'potato-proposed-updates', 'proposed-updates');
56         if os.path.isfile(filename) and not os.path.islink(filename) and not db_files.has_key(filename) and not excluded.has_key(filename):
57             waste = waste + os.stat(filename)[stat.ST_SIZE];
58             print filename
59
60 ################################################################################
61
62 def check_files():
63     global db_files;
64
65     print "Building list of Database files...";
66
67     q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id")
68     ql = q.getresult();
69
70     db_files = {};
71     for i in ql:
72         filename = os.path.abspath(i[0] + i[1]);
73         db_files[filename] = "";
74         if os.access(filename, os.R_OK) == 0:
75             utils.warn("'%s' doesn't exist." % (filename));
76
77     file = utils.open_file(Cnf["Dir::OverrideDir"]+'override.unreferenced');
78     for filename in file.readlines():
79         filename = filename[:-1];
80         excluded[filename] = "";
81
82     print "Checking against existent files...";
83
84     os.path.walk(Cnf["Dir::RootDir"]+'dists/', process_dir, None);
85
86     print
87     print "%s wasted..." % (utils.size_type(waste));
88
89 ################################################################################
90
91 def check_dscs():
92     count = 0;
93     suite = 'unstable';
94     for component in Cnf.SubTree("Component").List():
95         if component == "mixed":
96             continue;
97         component = string.lower(component);
98         list_filename = '%s%s_%s_source.list' % (Cnf["Dir::ListsDir"], suite, component);
99         list_file = utils.open_file(list_filename);
100         for line in list_file.readlines():
101             file = line[:-1];
102             try:
103                 utils.parse_changes(file, 1);
104             except utils.invalid_dsc_format_exc, line:
105                 utils.warn("syntax error in .dsc file '%s', line %s." % (file, line));
106                 count = count + 1;
107
108     if count:
109         utils.warn("Found %s invalid .dsc files." % (count));
110
111 ################################################################################
112
113 def check_override():
114     for suite in [ "stable", "unstable" ]:
115         print suite
116         print "-------------"
117         print
118         suite_id = db_access.get_suite_id(suite);
119         q = projectB.query("""
120 SELECT DISTINCT b.package FROM binaries b, bin_associations ba
121  WHERE b.id = ba.bin AND ba.suite = %s AND NOT EXISTS
122        (SELECT * FROM override o WHERE o.suite = %s AND o.package = b.package)"""
123                            % (suite_id, suite_id));
124         print q
125         q = projectB.query("""
126 SELECT DISTINCT s.source FROM source s, src_associations sa
127   WHERE s.id = sa.source AND sa.suite = %s AND NOT EXISTS
128        (SELECT * FROM override o WHERE o.suite = %s and o.package = s.source)"""
129                            % (suite_id, suite_id));
130         print q
131
132 ################################################################################
133
134 # Ensure that the source files for any given package is all in one
135 # directory so that 'apt-get source' works...
136
137 def check_source_in_one_dir():
138     # Not the most enterprising method, but hey...
139     broken_count = 0;
140     q = projectB.query("SELECT id FROM source;");
141     for i in q.getresult():
142         source_id = i[0];
143         q2 = projectB.query("""
144 SELECT l.path, f.filename FROM files f, dsc_files df, location l WHERE df.source = %s AND f.id = df.file AND l.id = f.location"""
145                             % (source_id));
146         first_path = "";
147         first_filename = "";
148         broken = 0;
149         for j in q2.getresult():
150             filename = j[0]+j[1];
151             path = os.path.dirname(filename);
152             if first_path == "":
153                 first_path = path;
154                 first_filename = filename;
155             elif first_path != path:
156                 symlink = path + '/' + os.path.basename(first_filename);
157                 if not os.path.exists(symlink):
158                     broken = 1;
159                     print "WOAH, we got a live one here... %s [%s] {%s}" % (filename, source_id, symlink);
160         if broken:
161             broken_count = broken_count + 1;
162     print "Found %d source packages where the source is not all in one directory." % (broken_count);
163
164 ################################################################################
165
166 def check_md5sums():
167     print "Getting file information from database...";
168     q = projectB.query("SELECT l.path, f.filename, f.md5sum, f.size FROM files f, location l WHERE f.location = l.id")
169     ql = q.getresult();
170
171     print "Checking file md5sums & sizes...";
172     for i in ql:
173         filename = os.path.abspath(i[0] + i[1]);
174         db_md5sum = i[2];
175         db_size = int(i[3]);
176         try:
177             file = utils.open_file(filename);
178         except:
179             utils.warn("can't open '%s'." % (filename));
180             continue;
181         md5sum = apt_pkg.md5sum(file);
182         size = os.stat(filename)[stat.ST_SIZE];
183         if md5sum != db_md5sum:
184             utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, db_md5sum));
185         if size != db_size:
186             utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, db_size));
187
188     print "Done."
189
190 ################################################################################
191 #
192 # Check all files for timestamps in the future; common from hardware
193 # (e.g. alpha) which have far-future dates as their default dates.
194
195
196
197 def Ent(Kind,Name,Link,Mode,UID,GID,Size,MTime,Major,Minor):
198     global future_files;
199
200     if MTime > current_time:
201         future_files[current_file] = MTime;
202         print "%s: %s '%s','%s',%u,%u,%u,%u,%u" % (current_file, Kind,Name,Link,Mode,UID,GID,Size, MTime);
203
204 def check_timestamps():
205     global current_file;
206
207     q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id AND f.filename ~ '.deb$'")
208     ql = q.getresult();
209     db_files = {};
210     count = 0;
211     for i in ql:
212         filename = os.path.abspath(i[0] + i[1]);
213         if os.access(filename, os.R_OK):
214             file = utils.open_file(filename);
215             current_file = filename;
216             sys.stderr.write("Processing %s.\n" % (filename));
217             apt_inst.debExtract(file,Ent,"control.tar.gz");
218             file.seek(0);
219             apt_inst.debExtract(file,Ent,"data.tar.gz");
220             count = count + 1;
221     print "Checked %d files (out of %d)." % (count, len(db_files.keys()));
222
223 ################################################################################
224
225 def main ():
226     global Cnf, projectB, db_files, waste, excluded;
227
228     Cnf = utils.get_conf()
229
230     apt_pkg.ParseCommandLine(Cnf,[],sys.argv);
231     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
232     db_access.init(Cnf, projectB);
233
234     #check_md5sums();
235     #check_source_in_one_dir();
236     #check_override();
237     #check_dscs();
238     #check_files();
239     check_timestamps();
240
241 #######################################################################################
242
243 if __name__ == '__main__':
244     main();
245