]> git.decadent.org.uk Git - dak.git/blob - tea
make use of utils.{warn,fubar}. clean up extraneous \n's in fernanda and natalie...
[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.11 2001-06-22 22:53:14 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     global db_files;
58
59     print "Building list of Database files...";
60
61     q = projectB.query("SELECT l.path, f.filename FROM files f, location l WHERE f.location = l.id")
62     ql = q.getresult();
63
64     db_files = {};
65     for i in ql:
66         filename = os.path.abspath(i[0] + i[1]);
67         db_files[filename] = "";
68         if os.access(filename, os.R_OK) == 0:
69             utils.warn("'%s' doesn't exist." % (filename));
70
71     file = utils.open_file(Cnf["Dir::OverrideDir"]+'override.unreferenced','r');
72     for filename in file.readlines():
73         filename = filename[:-1];
74         excluded[filename] = "";
75
76     print "Checking against existent files...";
77
78     os.path.walk(Cnf["Dir::RootDir"]+'dists/', process_dir, None);
79
80     print
81     print "%s wasted..." % (utils.size_type(waste));
82
83 ################################################################################
84
85 def check_dscs():
86     count = 0;
87     suite = 'unstable';
88     for component in Cnf.SubTree("Component").List():
89         if component == "mixed":
90             continue;
91         component = string.lower(component);
92         list_filename = '%s%s_%s_source.list' % (Cnf["Dir::ListsDir"], suite, component);
93         list_file = utils.open_file(list_filename, 'r');
94         for line in list_file.readlines():
95             file = line[:-1];
96             try:
97                 utils.parse_changes(file, 1);
98             except utils.invalid_dsc_format_exc, line:
99                 utils.warn("syntax error in .dsc file '%s', line %s." % (file, line));
100                 count = count + 1;
101
102     if count:
103         utils.warn("Found %s invalid .dsc files." % (count));
104
105 ################################################################################
106
107 def check_override():
108     for suite in [ "stable", "unstable" ]:
109         print suite
110         print "-------------"
111         print
112         suite_id = db_access.get_suite_id(suite);
113         q = projectB.query("""
114 SELECT DISTINCT b.package FROM binaries b, bin_associations ba
115  WHERE b.id = ba.bin AND ba.suite = %s AND NOT EXISTS
116        (SELECT * FROM override o WHERE o.suite = %s AND o.package = b.package)"""
117                            % (suite_id, suite_id));
118         print q
119         q = projectB.query("""
120 SELECT DISTINCT s.source FROM source s, src_associations sa
121   WHERE s.id = sa.source AND sa.suite = %s AND NOT EXISTS
122        (SELECT * FROM override o WHERE o.suite = %s and o.package = s.source)"""
123                            % (suite_id, suite_id));
124         print q
125
126 ################################################################################
127
128 # Ensure that the source files for any given package is all in one
129 # directory so that 'apt-get source' works...
130
131 def check_source_in_one_dir():
132     # Not the most enterprising method, but hey...
133     broken_count = 0;
134     q = projectB.query("SELECT id FROM source;");
135     for i in q.getresult():
136         source_id = i[0];
137         q2 = projectB.query("""
138 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"""
139                             % (source_id));
140         first_path = "";
141         first_filename = "";
142         broken = 0;
143         for j in q2.getresult():
144             filename = j[0]+j[1];
145             path = os.path.dirname(filename);
146             if first_path == "":
147                 first_path = path;
148                 first_filename = filename;
149             elif first_path != path:
150                 symlink = path + '/' + os.path.basename(first_filename);
151                 if not os.path.exists(symlink):
152                     broken = 1;
153                     print "WOAH, we got a live one here... %s [%s] {%s}" % (filename, source_id, symlink);
154         if broken:
155             broken_count = broken_count + 1;
156     print "Found %d source packages where the source is not all in one directory." % (broken_count);
157
158 ################################################################################
159
160 def check_md5sums():
161     print "Getting file information from database...";
162     q = projectB.query("SELECT l.path, f.filename, f.md5sum, f.size FROM files f, location l WHERE f.location = l.id")
163     ql = q.getresult();
164     
165     print "Checking file md5sums & sizes...";
166     for i in ql:
167         filename = os.path.abspath(i[0] + i[1]);
168         db_md5sum = i[2];
169         db_size = int(i[3]);
170         try:
171             file = utils.open_file(filename, 'r');
172         except:
173             utils.warn("can't open '%s'." % (filename));
174             continue;
175         md5sum = apt_pkg.md5sum(file);
176         size = os.stat(filename)[stat.ST_SIZE];
177         if md5sum != db_md5sum:
178             utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, db_md5sum));
179         if size != db_size:
180             utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, db_size));
181
182     print "Done."
183
184 ################################################################################
185
186 def main ():
187     global Cnf, projectB, db_files, waste, excluded;
188
189     apt_pkg.init();
190     
191     Cnf = apt_pkg.newConfiguration();
192     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
193
194     Arguments = [('d',"debug","Tea::Options::Debug", "IntVal"),
195                  ('h',"help","Tea::Options::Help"),
196                  ('v',"version","Tea::Options::Version")];
197
198     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
199     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
200     db_access.init(Cnf, projectB);
201     
202     #check_md5sums();
203     check_source_in_one_dir();
204     #check_override();
205     #check_dscs();
206     #check_files();
207
208 #######################################################################################
209
210 if __name__ == '__main__':
211     main()
212