]> git.decadent.org.uk Git - dak.git/blob - jeri
lower depends on postgresql to suggests
[dak.git] / jeri
1 #!/usr/bin/env python
2
3 # Dependency check proposed-updates
4 # Copyright (C) 2001  James Troup <james@nocrew.org>
5 # $Id: jeri,v 1.3 2001-11-18 19:57:58 rmurray 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 # <aj> ARRRGGGHHH
24 # <aj> what's wrong with me!?!?!?
25 # <aj> i was just nice to some mormon doorknockers!!!
26 # <Omnic> AJ?!?!
27 # <aj> i know!!!!!
28 # <Omnic> I'm gonna have to kick your ass when you come over
29 # <Culus> aj: GET THE HELL OUT OF THE CABAL! :P
30
31 ################################################################################
32
33 import pg, sys, os, string
34 import utils, db_access
35 import apt_pkg, apt_inst;
36
37 ################################################################################
38
39 Cnf = None;
40 projectB = None;
41 stable = {};
42 stable_virtual = {};
43 architectures = None;
44
45 ################################################################################
46
47 def pp_dep (deps):
48     pp_deps = [];
49     for atom in deps:
50         (pkg, version, constraint) = atom;
51         if constraint:
52             pp_dep = "%s (%s %s)" % (pkg, constraint, version);
53         else:
54             pp_dep = pkg;
55         pp_deps.append(pp_dep);
56     return string.join(pp_deps, " |");
57
58 ################################################################################
59
60 def d_test (dict, key, positive, negative):
61     if not dict:
62         return negative;
63     if dict.has_key(key):
64         return positive;
65     else:
66         return negative;
67
68 ################################################################################
69
70 def check_dep (depends, dep_type, check_archs, filename, files):
71     pkg_unsat = 0;
72     for arch in check_archs:
73         for parsed_dep in apt_pkg.ParseDepends(depends):
74             unsat = [];
75             for atom in parsed_dep:
76                 (dep, version, constraint) = atom;
77                 # As a real package?
78                 if stable.has_key(dep):
79                     if stable[dep].has_key(arch):
80                         if apt_pkg.CheckDep(stable[dep][arch], constraint, version):
81                             if Options["debug"]:
82                                 print "Found %s as a real package." % (pp_dep(parsed_dep));
83                             unsat = 0;
84                             break;
85                 # As a virtual?
86                 if stable_virtual.has_key(dep):
87                     if stable_virtual[dep].has_key(arch):
88                         if not constraint and not version:
89                             if Options["debug"]:
90                                 print "Found %s as a virtual package." % (pp_dep(parsed_dep));
91                             unsat = 0;
92                             break;
93                 # As part of the same .changes?
94                 epochless_version = utils.re_no_epoch.sub('', version)
95                 dep_filename = "%s_%s_%s.deb" % (dep, epochless_version, arch);
96                 if files.has_key(dep_filename):
97                     if Options["debug"]:
98                         print "Found %s in the same upload." % (pp_dep(parsed_dep));
99                     unsat = 0;
100                     break;
101                 # Not found...
102                 # [FIXME: must be a better way ... ]
103                 error = "%s not found. [Real: " % (pp_dep(parsed_dep))
104                 if stable.has_key(dep):
105                     if stable[dep].has_key(arch):
106                         error = error + "%s:%s:%s" % (dep, arch, stable[dep][arch]);
107                     else:
108                         error = error + "%s:-:-" % (dep);
109                 else:
110                     error = error + "-:-:-";
111                 error = error + ", Virtual: ";
112                 if stable_virtual.has_key(dep):
113                     if stable_virtual[dep].has_key(arch):
114                         error = error + "%s:%s" % (dep, arch);
115                     else:
116                         error = error + "%s:-";
117                 else:
118                     error = error + "-:-";
119                 error = error + ", Upload: ";
120                 if files.has_key(dep_filename):
121                     error = error + "yes";
122                 else:
123                     error = error + "no";
124                 error = error + "]";
125                 unsat.append(error);
126
127             if unsat:
128                 sys.stderr.write("MWAAP! %s: '%s' %s can not be satisifed:\n" % (filename, pp_dep(parsed_dep), dep_type));
129                 for error in unsat:
130                     sys.stderr.write("  %s\n" % (error));
131                 pkg_unsat = 1;
132
133     return pkg_unsat;
134
135 def check_package(filename, files):
136     try:
137         control = apt_pkg.ParseSection(apt_inst.debExtractControl(utils.open_file(filename)));
138     except:
139         utils.warn("%s: debExtractControl() raised %s." % (filename, sys.exc_type));
140         return 1;
141     Depends = control.Find("Depends");
142     Pre_Depends = control.Find("Pre-Depends");
143     Recommends = control.Find("Recommends");
144     pkg_arch = control.Find("Architecture");
145     base_file = os.path.basename(filename);
146     if pkg_arch == "all":
147         check_archs = architectures;
148     else:
149         check_archs = [pkg_arch];
150
151     pkg_unsat = 0;
152     if Pre_Depends:
153         pkg_unsat = pkg_unsat + check_dep(Pre_Depends, "pre-dependency", check_archs, base_file, files);
154
155     if Depends:
156         pkg_unsat = pkg_unsat + check_dep(Depends, "dependency", check_archs, base_file, files);
157     #if Recommends:
158     #pkg_unsat = pkg_unsat + check_dep(Recommends, "recommendation", check_archs, base_file, files);
159
160     return pkg_unsat;
161
162 ################################################################################
163
164 def pass_fail (filename, result):
165     if not Options["quiet"]:
166         print "%s:" % (os.path.basename(filename)),
167         if result:
168             print "FAIL";
169         else:
170             print "ok";
171
172 ################################################################################
173
174 def check_changes (filename):
175     try:
176         changes = utils.parse_changes(filename, 0)
177         files = utils.build_file_list(changes, "");
178     except:
179         utils.warn("Error parsing changes file '%s'" % (filename));
180         return;
181
182     result = 0;
183
184     # Move to the pool directory
185     cwd = os.getcwd();
186     file = files.keys()[0];
187     pool_dir = Cnf["Dir::PoolDir"] + '/' + utils.poolify(changes["source"], files[file]["component"]);
188     os.chdir(pool_dir);
189
190     changes_result = 0;
191     for file in files.keys():
192         if file[-4:] == ".deb":
193             result = check_package(file, files);
194             if Options["verbose"]:
195                 pass_fail(file, result);
196             changes_result = changes_result + result;
197
198     pass_fail (filename, changes_result);
199
200     # Move back
201     os.chdir(cwd);
202
203 ################################################################################
204
205 def check_deb (filename):
206     result = check_package(filename, {});
207     pass_fail(filename, result);
208
209
210 ################################################################################
211
212 def check_joey (filename):
213     file = utils.open_file(filename);
214
215     cwd = os.getcwd();
216     os.chdir("%s/dists/proposed-updates" % (Cnf["Dir::RootDir"]));
217
218     for line in file.readlines():
219         line = line[:-1];
220         if string.find(line, 'install') != -1:
221             split_line = string.split(line);
222             install_type = split_line[0];
223             if [ "install", "install-u", "sync-install" ].count(install_type) == 0:
224                 utils.fubar("Unknown install type ('%s') from: %s" % (install_type, line));
225             changes_filename = split_line[1]
226             if len(split_line) != 2:
227                 utils.fubar("Parse error (more than 2 elements): %s" % (line));
228             if Options["debug"]:
229                 print "Processing %s..." % (changes_filename);
230             check_changes(changes_filename);
231
232     os.chdir(cwd);
233
234 ################################################################################
235
236 def parse_packages():
237     global stable, stable_virtual, architectures;
238
239     # Parse the Packages files (since it's a sub-second operation on auric)
240     suite = "stable";
241     stable = {};
242     components = Cnf.SubTree("Suite::%s::Components" % (suite)).List();
243     architectures = Cnf.SubTree("Suite::%s::Architectures" % (suite)).List();
244     for arch in [ "source", "all" ]:
245         if architectures.count(arch):
246             architectures.remove(arch);
247     for component in components:
248         for architecture in architectures:
249             filename = "%s/dists/%s/%s/binary-%s/Packages" % (Cnf["Dir::RootDir"], suite, component, architecture);
250             packages = utils.open_file(filename, 'r');
251             Packages = apt_pkg.ParseTagFile(packages);
252             while Packages.Step():
253                 package = Packages.Section.Find('Package');
254                 version = Packages.Section.Find('Version');
255                 provides = Packages.Section.Find('Provides');
256                 if not stable.has_key(package):
257                     stable[package] = {};
258                 stable[package][architecture] = version;
259                 if provides:
260                     for virtual_pkg in string.split(provides,","):
261                         virtual_pkg = string.strip(virtual_pkg);
262                         if not stable_virtual.has_key(virtual_pkg):
263                             stable_virtual[virtual_pkg] = {};
264                         stable_virtual[virtual_pkg][architecture] = "NA";
265
266 ################################################################################
267
268 def main ():
269     global Cnf, projectB, Options;
270
271     Cnf = utils.get_conf()
272
273     Arguments = [('q',"quiet","Jeri::Options::Quiet"),
274                  ('v',"verbose","Jeri::Options::Verbose"),
275                  ('D',"debug","Jeri::Options::Debug"),
276                  ('h',"help","Jeri::Options::Help"),
277                  ('V',"version","Jeri::Options::Version")];
278     for i in [ "quiet", "verbose", "help", "debug" ]:
279         if not Cnf.has_key("Jeri::Options::%s" % (i)):
280             Cnf["Jeri::Options::%s" % (i)] = "";
281
282     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
283     Options = Cnf.SubTree("Jeri::Options")
284
285     if Options["Help"]:
286         usage(0);
287     if not arguments:
288         utils.fubar("need at least one package name as an argument.");
289
290     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
291     db_access.init(Cnf, projectB);
292
293     print "Parsing packages files...",
294     parse_packages();
295     print "done.";
296
297     for file in arguments:
298         if file[-8:] == ".changes":
299             check_changes(file);
300         elif file[-4:] == ".deb":
301             check_deb(file);
302         elif file[-5:] == ".joey":
303             check_joey(file);
304         else:
305             utils.fubar("Unrecognised file type: '%s'." % (file));
306
307 #######################################################################################
308
309 if __name__ == '__main__':
310     main()