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 $
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.
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.
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
21 ################################################################################
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*''
28 ################################################################################
30 import pg, sys, os, stat, time
31 import utils, db_access
32 import apt_pkg, apt_inst;
34 ################################################################################
43 current_time = time.time();
45 ################################################################################
47 def usage(exit_code=0):
48 print """Usage: tea MODE
49 Run various sanity checks of the archive and/or database.
51 -h, --help show this help and exit.
53 The following MODEs are available:
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
65 ################################################################################
67 def process_dir (unused, dirname, filenames):
68 global waste, db_files, excluded;
70 if dirname.find('/disks-') != -1 or dirname.find('upgrade-') != -1:
72 # hack; can't handle .changes files
73 if dirname.find('proposed-updates') != -1:
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];
82 ################################################################################
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")
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));
98 file = utils.open_file(Cnf["Dir::Override"]+'override.unreferenced');
99 for filename in file.readlines():
100 filename = filename[:-1];
101 excluded[filename] = "";
103 print "Checking against existent files...";
105 os.path.walk(Cnf["Dir::Root"]+'dists/', process_dir, None);
108 print "%s wasted..." % (utils.size_type(waste));
110 ################################################################################
115 for component in Cnf.SubTree("Component").List():
116 if component == "mixed":
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():
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));
130 utils.warn("Found %s invalid .dsc files." % (count));
132 ################################################################################
134 def check_override():
135 for suite in [ "stable", "unstable" ]:
137 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));
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));
153 ################################################################################
155 # Ensure that the source files for any given package is all in one
156 # directory so that 'apt-get source' works...
158 def check_source_in_one_dir():
159 # Not the most enterprising method, but hey...
161 q = projectB.query("SELECT id FROM source;");
162 for i in q.getresult():
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"""
170 for j in q2.getresult():
171 filename = j[0] + j[1];
172 path = os.path.dirname(filename);
175 first_filename = filename;
176 elif first_path != path:
177 symlink = path + '/' + os.path.basename(first_filename);
178 if not os.path.exists(symlink):
180 print "WOAH, we got a live one here... %s [%s] {%s}" % (filename, source_id, symlink);
183 print "Found %d source packages where the source is not all in one directory." % (broken_count);
185 ################################################################################
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")
192 print "Checking file md5sums & sizes...";
194 filename = os.path.abspath(i[0] + i[1]);
198 file = utils.open_file(filename);
200 utils.warn("can't open '%s'." % (filename));
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));
207 utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, db_size));
211 ################################################################################
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.
218 def Ent(Kind,Name,Link,Mode,UID,GID,Size,MTime,Major,Minor):
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);
225 def check_timestamps():
228 q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id AND f.filename ~ '.deb$'")
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");
240 apt_inst.debExtract(file,Ent,"data.tar.gz");
242 print "Checked %d files (out of %d)." % (count, len(db_files.keys()));
244 ################################################################################
246 def check_missing_tar_gz_in_dsc():
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$'");
253 print "Checking %d files..." % len(ql);
255 print "No files to check."
257 filename = os.path.abspath(i[0] + i[1]);
259 # NB: don't enforce .dsc syntax
260 dsc = utils.parse_changes(filename);
262 utils.fubar("error parsing .dsc file '%s'." % (filename));
263 dsc_files = utils.build_file_list(dsc, is_a_dsc=1);
265 for file in dsc_files.keys():
266 m = utils.re_issource.match(file);
268 utils.fubar("%s not recognised as source." % (file));
270 if type == "orig.tar.gz" or type == "tar.gz":
273 utils.warn("%s has no .tar.gz in the .dsc file." % (file));
277 utils.warn("Found %s invalid .dsc files." % (count));
279 ################################################################################
282 global Cnf, projectB, db_files, waste, excluded;
284 Cnf = utils.get_conf();
285 Arguments = [('h',"help","Tea::Options::Help")];
287 if not Cnf.has_key("Tea::Options::%s" % (i)):
288 Cnf["Tea::Options::%s" % (i)] = "";
290 args = apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv);
292 Options = Cnf.SubTree("Tea::Options")
297 utils.warn("tea requires at least one argument");
300 utils.warn("tea accepts only one argument");
302 mode = args[0].lower();
304 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
305 db_access.init(Cnf, projectB);
307 if mode == "md5sums":
309 elif mode == "files":
311 elif mode == "dsc-syntax":
313 elif mode == "missing-overrides":
315 elif mode == "source-in-one-dir":
316 check_source_in_one_dir();
317 elif mode == "timestamps":
319 elif mode == "tar-gz-in-dsc":
320 check_missing_tar_gz_in_dsc();
322 utils.warn("unknown mode '%s'" % (mode));
325 ################################################################################
327 if __name__ == '__main__':