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