]> git.decadent.org.uk Git - dak.git/blob - tea
two bug fixes in source_exists changes
[dak.git] / tea
1 #!/usr/bin/env python
2
3 # Sanity check the database
4 # Copyright (C) 2000, 2001, 2002  James Troup <james@nocrew.org>
5 # $Id: tea,v 1.22 2003-01-02 18:14: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 #   And, lo, a great and menacing voice rose from the depths, and with
24 #   great wrath and vehemence it's voice boomed across the
25 #   land... ``hehehehehehe... that *tickles*''
26 #                                                       -- aj on IRC
27
28 ################################################################################
29
30 import pg, sys, os, stat, time
31 import utils, db_access
32 import apt_pkg, apt_inst;
33
34 ################################################################################
35
36 Cnf = None;
37 projectB = None;
38 db_files = {};
39 waste = 0.0;
40 excluded = {};
41 current_file = None;
42 future_files = {};
43 current_time = time.time();
44
45 ################################################################################
46
47 def usage(exit_code=0):
48     print """Usage: tea MODE
49 Run various sanity checks of the archive and/or database.
50
51   -h, --help                show this help and exit.
52
53 The following MODEs are available:
54
55   md5sums            - validate the md5sums stored in the database
56   files              - check files in the database against what's in the archive
57   dsc-syntax         - validate the syntax of .dsc files in the archive
58   missing-overrides  - check for missing overrides
59   source-in-one-dir  - ensure the source for each package is in one directory
60   timestamps         - check for future timestamps in .deb's
61   tar-gz-in-dsc      - ensure each .dsc lists a .tar.gz file
62 """
63     sys.exit(exit_code)
64
65 ################################################################################
66
67 def process_dir (unused, dirname, filenames):
68     global waste, db_files, excluded;
69
70     if dirname.find('/disks-') != -1 or dirname.find('upgrade-') != -1:
71         return;
72     # hack; can't handle .changes files
73     if dirname.find('proposed-updates') != -1:
74         return;
75     for name in filenames:
76         filename = os.path.abspath(dirname+'/'+name);
77         filename = filename.replace('potato-proposed-updates', 'proposed-updates');
78         if os.path.isfile(filename) and not os.path.islink(filename) and not db_files.has_key(filename) and not excluded.has_key(filename):
79             waste += os.stat(filename)[stat.ST_SIZE];
80             print filename
81
82 ################################################################################
83
84 def check_files():
85     global db_files;
86
87     print "Building list of database files...";
88     q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id")
89     ql = q.getresult();
90
91     db_files.clear();
92     for i in ql:
93         filename = os.path.abspath(i[0] + i[1]);
94         db_files[filename] = "";
95         if os.access(filename, os.R_OK) == 0:
96             utils.warn("'%s' doesn't exist." % (filename));
97
98     file = utils.open_file(Cnf["Dir::Override"]+'override.unreferenced');
99     for filename in file.readlines():
100         filename = filename[:-1];
101         excluded[filename] = "";
102
103     print "Checking against existent files...";
104
105     os.path.walk(Cnf["Dir::Root"]+'dists/', process_dir, None);
106
107     print
108     print "%s wasted..." % (utils.size_type(waste));
109
110 ################################################################################
111
112 def check_dscs():
113     count = 0;
114     suite = 'unstable';
115     for component in Cnf.SubTree("Component").List():
116         if component == "mixed":
117             continue;
118         component = component.lower();
119         list_filename = '%s%s_%s_source.list' % (Cnf["Dir::Lists"], suite, component);
120         list_file = utils.open_file(list_filename);
121         for line in list_file.readlines():
122             file = line[:-1];
123             try:
124                 utils.parse_changes(file, dsc_whitespace_rules=1);
125             except utils.invalid_dsc_format_exc, line:
126                 utils.warn("syntax error in .dsc file '%s', line %s." % (file, line));
127                 count += 1;
128
129     if count:
130         utils.warn("Found %s invalid .dsc files." % (count));
131
132 ################################################################################
133
134 def check_override():
135     for suite in [ "stable", "unstable" ]:
136         print suite
137         print "-------------"
138         print
139         suite_id = db_access.get_suite_id(suite);
140         q = projectB.query("""
141 SELECT DISTINCT b.package FROM binaries b, bin_associations ba
142  WHERE b.id = ba.bin AND ba.suite = %s AND NOT EXISTS
143        (SELECT * FROM override o WHERE o.suite = %s AND o.package = b.package)"""
144                            % (suite_id, suite_id));
145         print q
146         q = projectB.query("""
147 SELECT DISTINCT s.source FROM source s, src_associations sa
148   WHERE s.id = sa.source AND sa.suite = %s AND NOT EXISTS
149        (SELECT * FROM override o WHERE o.suite = %s and o.package = s.source)"""
150                            % (suite_id, suite_id));
151         print q
152
153 ################################################################################
154
155 # Ensure that the source files for any given package is all in one
156 # directory so that 'apt-get source' works...
157
158 def check_source_in_one_dir():
159     # Not the most enterprising method, but hey...
160     broken_count = 0;
161     q = projectB.query("SELECT id FROM source;");
162     for i in q.getresult():
163         source_id = i[0];
164         q2 = projectB.query("""
165 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"""
166                             % (source_id));
167         first_path = "";
168         first_filename = "";
169         broken = 0;
170         for j in q2.getresult():
171             filename = j[0] + j[1];
172             path = os.path.dirname(filename);
173             if first_path == "":
174                 first_path = path;
175                 first_filename = filename;
176             elif first_path != path:
177                 symlink = path + '/' + os.path.basename(first_filename);
178                 if not os.path.exists(symlink):
179                     broken = 1;
180                     print "WOAH, we got a live one here... %s [%s] {%s}" % (filename, source_id, symlink);
181         if broken:
182             broken_count += 1;
183     print "Found %d source packages where the source is not all in one directory." % (broken_count);
184
185 ################################################################################
186
187 def check_md5sums():
188     print "Getting file information from database...";
189     q = projectB.query("SELECT l.path, f.filename, f.md5sum, f.size FROM files f, location l WHERE f.location = l.id")
190     ql = q.getresult();
191
192     print "Checking file md5sums & sizes...";
193     for i in ql:
194         filename = os.path.abspath(i[0] + i[1]);
195         db_md5sum = i[2];
196         db_size = int(i[3]);
197         try:
198             file = utils.open_file(filename);
199         except:
200             utils.warn("can't open '%s'." % (filename));
201             continue;
202         md5sum = apt_pkg.md5sum(file);
203         size = os.stat(filename)[stat.ST_SIZE];
204         if md5sum != db_md5sum:
205             utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, db_md5sum));
206         if size != db_size:
207             utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, db_size));
208
209     print "Done."
210
211 ################################################################################
212 #
213 # Check all files for timestamps in the future; common from hardware
214 # (e.g. alpha) which have far-future dates as their default dates.
215
216
217
218 def Ent(Kind,Name,Link,Mode,UID,GID,Size,MTime,Major,Minor):
219     global future_files;
220
221     if MTime > current_time:
222         future_files[current_file] = MTime;
223         print "%s: %s '%s','%s',%u,%u,%u,%u,%u,%u,%u" % (current_file, Kind,Name,Link,Mode,UID,GID,Size, MTime, Major, Minor);
224
225 def check_timestamps():
226     global current_file;
227
228     q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id AND f.filename ~ '.deb$'")
229     ql = q.getresult();
230     db_files.clear();
231     count = 0;
232     for i in ql:
233         filename = os.path.abspath(i[0] + i[1]);
234         if os.access(filename, os.R_OK):
235             file = utils.open_file(filename);
236             current_file = filename;
237             sys.stderr.write("Processing %s.\n" % (filename));
238             apt_inst.debExtract(file,Ent,"control.tar.gz");
239             file.seek(0);
240             apt_inst.debExtract(file,Ent,"data.tar.gz");
241             count += 1;
242     print "Checked %d files (out of %d)." % (count, len(db_files.keys()));
243
244 ################################################################################
245
246 def check_missing_tar_gz_in_dsc():
247     count = 0;
248
249     print "Building list of database files...";
250     q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id AND f.filename ~ '.dsc$'");
251     ql = q.getresult();
252     if ql:
253         print "Checking %d files..." % len(ql);
254     else:
255         print "No files to check."
256     for i in ql:
257         filename = os.path.abspath(i[0] + i[1]);
258         try:
259             # NB: don't enforce .dsc syntax
260             dsc = utils.parse_changes(filename);
261         except:
262             utils.fubar("error parsing .dsc file '%s'." % (filename));
263         dsc_files = utils.build_file_list(dsc, is_a_dsc=1);
264         has_tar = 0;
265         for file in dsc_files.keys():
266             m = utils.re_issource.match(file);
267             if not m:
268                 utils.fubar("%s not recognised as source." % (file));
269             type = m.group(3);
270             if type == "orig.tar.gz" or type == "tar.gz":
271                 has_tar = 1;
272         if not has_tar:
273             utils.warn("%s has no .tar.gz in the .dsc file." % (file));
274             count += 1;
275
276     if count:
277         utils.warn("Found %s invalid .dsc files." % (count));
278
279 ################################################################################
280
281 def main ():
282     global Cnf, projectB, db_files, waste, excluded;
283
284     Cnf = utils.get_conf();
285     Arguments = [('h',"help","Tea::Options::Help")];
286     for i in [ "help" ]:
287         if not Cnf.has_key("Tea::Options::%s" % (i)):
288             Cnf["Tea::Options::%s" % (i)] = "";
289
290     args = apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv);
291
292     Options = Cnf.SubTree("Tea::Options")
293     if Options["Help"]:
294         usage();
295
296     if len(args) < 1:
297         utils.warn("tea requires at least one argument");
298         usage(1);
299     elif len(args) > 1:
300         utils.warn("tea accepts only one argument");
301         usage(1);
302     mode = args[0].lower();
303
304     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
305     db_access.init(Cnf, projectB);
306
307     if mode == "md5sums":
308         check_md5sums();
309     elif mode == "files":
310         check_files();
311     elif mode == "dsc-syntax":
312         check_dscs();
313     elif mode == "missing-overrides":
314         check_override();
315     elif mode == "source-in-one-dir":
316         check_source_in_one_dir();
317     elif mode == "timestamps":
318         check_timestamps();
319     elif mode == "tar-gz-in-dsc":
320         check_missing_tar_gz_in_dsc();
321     else:
322         utils.warn("unknown mode '%s'" % (mode));
323         usage(1);
324
325 ################################################################################
326
327 if __name__ == '__main__':
328     main();
329