3 # Sanity check the database
4 # Copyright (C) 2000, 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: tea,v 1.20 2002-06-08 00:23:51 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 # 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*''
26 ################################################################################
28 import pg, sys, os, string, stat, time
29 import utils, db_access
30 import apt_pkg, apt_inst;
32 ################################################################################
41 current_time = time.time();
43 ################################################################################
45 def process_dir (unused, dirname, filenames):
46 global waste, db_files, excluded;
48 if string.find(dirname, '/disks-') != -1 or string.find(dirname, 'upgrade-') != -1:
50 # hack; can't handle .changes files
51 if string.find(dirname, 'proposed-updates') != -1:
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];
60 ################################################################################
65 print "Building list of database files...";
66 q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id")
71 filename = os.path.abspath(i[0] + i[1]);
72 db_files[filename] = "";
73 if os.access(filename, os.R_OK) == 0:
74 utils.warn("'%s' doesn't exist." % (filename));
76 file = utils.open_file(Cnf["Dir::Override"]+'override.unreferenced');
77 for filename in file.readlines():
78 filename = filename[:-1];
79 excluded[filename] = "";
81 print "Checking against existent files...";
83 os.path.walk(Cnf["Dir::Root"]+'dists/', process_dir, None);
86 print "%s wasted..." % (utils.size_type(waste));
88 ################################################################################
93 for component in Cnf.SubTree("Component").List():
94 if component == "mixed":
96 component = string.lower(component);
97 list_filename = '%s%s_%s_source.list' % (Cnf["Dir::Lists"], suite, component);
98 list_file = utils.open_file(list_filename);
99 for line in list_file.readlines():
102 utils.parse_changes(file, dsc_whitespace_rules=1);
103 except utils.invalid_dsc_format_exc, line:
104 utils.warn("syntax error in .dsc file '%s', line %s." % (file, line));
108 utils.warn("Found %s invalid .dsc files." % (count));
110 ################################################################################
112 def check_override():
113 for suite in [ "stable", "unstable" ]:
115 print "-------------"
117 suite_id = db_access.get_suite_id(suite);
118 q = projectB.query("""
119 SELECT DISTINCT b.package FROM binaries b, bin_associations ba
120 WHERE b.id = ba.bin AND ba.suite = %s AND NOT EXISTS
121 (SELECT * FROM override o WHERE o.suite = %s AND o.package = b.package)"""
122 % (suite_id, suite_id));
124 q = projectB.query("""
125 SELECT DISTINCT s.source FROM source s, src_associations sa
126 WHERE s.id = sa.source AND sa.suite = %s AND NOT EXISTS
127 (SELECT * FROM override o WHERE o.suite = %s and o.package = s.source)"""
128 % (suite_id, suite_id));
131 ################################################################################
133 # Ensure that the source files for any given package is all in one
134 # directory so that 'apt-get source' works...
136 def check_source_in_one_dir():
137 # Not the most enterprising method, but hey...
139 q = projectB.query("SELECT id FROM source;");
140 for i in q.getresult():
142 q2 = projectB.query("""
143 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"""
148 for j in q2.getresult():
149 filename = j[0]+j[1];
150 path = os.path.dirname(filename);
153 first_filename = filename;
154 elif first_path != path:
155 symlink = path + '/' + os.path.basename(first_filename);
156 if not os.path.exists(symlink):
158 print "WOAH, we got a live one here... %s [%s] {%s}" % (filename, source_id, symlink);
160 broken_count = broken_count + 1;
161 print "Found %d source packages where the source is not all in one directory." % (broken_count);
163 ################################################################################
166 print "Getting file information from database...";
167 q = projectB.query("SELECT l.path, f.filename, f.md5sum, f.size FROM files f, location l WHERE f.location = l.id")
170 print "Checking file md5sums & sizes...";
172 filename = os.path.abspath(i[0] + i[1]);
176 file = utils.open_file(filename);
178 utils.warn("can't open '%s'." % (filename));
180 md5sum = apt_pkg.md5sum(file);
181 size = os.stat(filename)[stat.ST_SIZE];
182 if md5sum != db_md5sum:
183 utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, db_md5sum));
185 utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, db_size));
189 ################################################################################
191 # Check all files for timestamps in the future; common from hardware
192 # (e.g. alpha) which have far-future dates as their default dates.
196 def Ent(Kind,Name,Link,Mode,UID,GID,Size,MTime,Major,Minor):
199 if MTime > current_time:
200 future_files[current_file] = MTime;
201 print "%s: %s '%s','%s',%u,%u,%u,%u,%u,%u,%u" % (current_file, Kind,Name,Link,Mode,UID,GID,Size, MTime, Major, Minor);
203 def check_timestamps():
206 q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id AND f.filename ~ '.deb$'")
211 filename = os.path.abspath(i[0] + i[1]);
212 if os.access(filename, os.R_OK):
213 file = utils.open_file(filename);
214 current_file = filename;
215 sys.stderr.write("Processing %s.\n" % (filename));
216 apt_inst.debExtract(file,Ent,"control.tar.gz");
218 apt_inst.debExtract(file,Ent,"data.tar.gz");
220 print "Checked %d files (out of %d)." % (count, len(db_files.keys()));
222 ################################################################################
224 def check_missing_tar_gz_in_dsc():
227 print "Building list of database files...";
228 q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id AND f.filename ~ '.dsc$'");
231 print "Checking %d files..." % len(ql);
233 print "No files to check."
235 filename = os.path.abspath(i[0] + i[1]);
237 # NB: don't enforce .dsc syntax
238 dsc = utils.parse_changes(filename);
240 utils.fubar("error parsing .dsc file '%s'." % (filename));
241 dsc_files = utils.build_file_list(dsc, is_a_dsc=1);
243 for file in dsc_files.keys():
244 m = utils.re_issource.match(file);
246 utils.fubar("%s not recognised as source." % (file));
248 if type == "orig.tar.gz" or type == "tar.gz":
251 utils.warn("%s has no .tar.gz in the .dsc file." % (file));
255 utils.warn("Found %s invalid .dsc files." % (count));
257 ################################################################################
260 global Cnf, projectB, db_files, waste, excluded;
262 Cnf = utils.get_conf()
264 apt_pkg.ParseCommandLine(Cnf,[],sys.argv);
265 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
266 db_access.init(Cnf, projectB);
269 #check_source_in_one_dir();
274 check_missing_tar_gz_in_dsc();
276 #######################################################################################
278 if __name__ == '__main__':