]> git.decadent.org.uk Git - dak.git/blob - dak/check_proposed_updates.py
Merge branch 'master' into security
[dak.git] / dak / check_proposed_updates.py
1 #!/usr/bin/env python
2
3 # Dependency check proposed-updates
4 # Copyright (C) 2001, 2002, 2004, 2006  James Troup <james@nocrew.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 # | > amd64 is more mature than even some released architectures
23 # |
24 # | This might be true of the architecture, unfortunately it seems to be the
25 # | exact opposite for most of the people involved with it.
26 #
27 # <1089213290.24029.6.camel@descent.netsplit.com>
28
29 ################################################################################
30
31 import pg, sys, os
32 import apt_pkg, apt_inst
33 from daklib import database
34 from daklib import utils
35
36 ################################################################################
37
38 Cnf = None
39 projectB = None
40 Options = None
41 stable = {}
42 stable_virtual = {}
43 architectures = None
44
45 ################################################################################
46
47 def usage (exit_code=0):
48     print """Usage: dak check-proposed-updates [OPTION] <CHANGES FILE | DEB FILE | ADMIN FILE>[...]
49 (Very) Basic dependency checking for proposed-updates.
50
51   -q, --quiet                be quieter about what is being done
52   -v, --verbose              be more verbose about what is being done
53   -h, --help                 show this help and exit
54
55 Need either changes files, deb files or an admin.txt file with a '.joey' suffix."""
56     sys.exit(exit_code)
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." % (utils.pp_deps(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." % (utils.pp_deps(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." % (utils.pp_deps(parsed_dep))
99                     unsat = 0
100                     break
101                 # Not found...
102                 # [FIXME: must be a better way ... ]
103                 error = "%s not found. [Real: " % (utils.pp_deps(parsed_dep))
104                 if stable.has_key(dep):
105                     if stable[dep].has_key(arch):
106                         error += "%s:%s:%s" % (dep, arch, stable[dep][arch])
107                     else:
108                         error += "%s:-:-" % (dep)
109                 else:
110                     error += "-:-:-"
111                 error += ", Virtual: "
112                 if stable_virtual.has_key(dep):
113                     if stable_virtual[dep].has_key(arch):
114                         error += "%s:%s" % (dep, arch)
115                     else:
116                         error += "%s:-"
117                 else:
118                     error += "-:-"
119                 error += ", Upload: "
120                 if files.has_key(dep_filename):
121                     error += "yes"
122                 else:
123                     error += "no"
124                 error += "]"
125                 unsat.append(error)
126
127             if unsat:
128                 sys.stderr.write("MWAAP! %s: '%s' %s can not be satisifed:\n" % (filename, utils.pp_deps(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 += check_dep(Pre_Depends, "pre-dependency", check_archs, base_file, files)
154
155     if Depends:
156         pkg_unsat += check_dep(Depends, "dependency", check_archs, base_file, files)
157     #if Recommends:
158     #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)
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     f = files.keys()[0]
187     pool_dir = Cnf["Dir::Pool"] + '/' + utils.poolify(changes["source"], files[f]["component"])
188     os.chdir(pool_dir)
189
190     changes_result = 0
191     for f in files.keys():
192         if f.endswith(".deb"):
193             result = check_package(f, files)
194             if Options["verbose"]:
195                 pass_fail(f, result)
196             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     f = utils.open_file(filename)
214
215     cwd = os.getcwd()
216     os.chdir("%s/dists/proposed-updates" % (Cnf["Dir::Root"]))
217
218     for line in f.readlines():
219         line = line.rstrip()
220         if line.find('install') != -1:
221             split_line = line.split()
222             if len(split_line) != 2:
223                 utils.fubar("Parse error (not exactly 2 elements): %s" % (line))
224             install_type = split_line[0]
225             if install_type not in [ "install", "install-u", "sync-install" ]:
226                 utils.fubar("Unknown install type ('%s') from: %s" % (install_type, line))
227             changes_filename = split_line[1]
228             if Options["debug"]:
229                 print "Processing %s..." % (changes_filename)
230             check_changes(changes_filename)
231     f.close()
232
233     os.chdir(cwd)
234
235 ################################################################################
236
237 def parse_packages():
238     global stable, stable_virtual, architectures
239
240     # Parse the Packages files (since it's a sub-second operation on auric)
241     suite = "stable"
242     stable = {}
243     components = Cnf.ValueList("Suite::%s::Components" % (suite))
244     architectures = filter(utils.real_arch, Cnf.ValueList("Suite::%s::Architectures" % (suite)))
245     for component in components:
246         for architecture in architectures:
247             filename = "%s/dists/%s/%s/binary-%s/Packages" % (Cnf["Dir::Root"], suite, component, architecture)
248             packages = utils.open_file(filename, 'r')
249             Packages = apt_pkg.ParseTagFile(packages)
250             while Packages.Step():
251                 package = Packages.Section.Find('Package')
252                 version = Packages.Section.Find('Version')
253                 provides = Packages.Section.Find('Provides')
254                 if not stable.has_key(package):
255                     stable[package] = {}
256                 stable[package][architecture] = version
257                 if provides:
258                     for virtual_pkg in provides.split(","):
259                         virtual_pkg = virtual_pkg.strip()
260                         if not stable_virtual.has_key(virtual_pkg):
261                             stable_virtual[virtual_pkg] = {}
262                         stable_virtual[virtual_pkg][architecture] = "NA"
263             packages.close()
264
265 ################################################################################
266
267 def main ():
268     global Cnf, projectB, Options
269
270     Cnf = utils.get_conf()
271
272     Arguments = [('d', "debug", "Check-Proposed-Updates::Options::Debug"),
273                  ('q',"quiet","Check-Proposed-Updates::Options::Quiet"),
274                  ('v',"verbose","Check-Proposed-Updates::Options::Verbose"),
275                  ('h',"help","Check-Proposed-Updates::Options::Help")]
276     for i in [ "debug", "quiet", "verbose", "help" ]:
277         if not Cnf.has_key("Check-Proposed-Updates::Options::%s" % (i)):
278             Cnf["Check-Proposed-Updates::Options::%s" % (i)] = ""
279
280     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
281     Options = Cnf.SubTree("Check-Proposed-Updates::Options")
282
283     if Options["Help"]:
284         usage(0)
285     if not arguments:
286         utils.fubar("need at least one package name as an argument.")
287
288     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
289     database.init(Cnf, projectB)
290
291     print "Parsing packages files...",
292     parse_packages()
293     print "done."
294
295     for f in arguments:
296         if f.endswith(".changes"):
297             check_changes(f)
298         elif f.endswith(".deb"):
299             check_deb(f)
300         elif f.endswith(".joey"):
301             check_joey(f)
302         else:
303             utils.fubar("Unrecognised file type: '%s'." % (f))
304
305 #######################################################################################
306
307 if __name__ == '__main__':
308     main()