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