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