]> git.decadent.org.uk Git - dak.git/blob - dak/make_suite_file_list.py
Merge commit 'dak-master/master'
[dak.git] / dak / make_suite_file_list.py
1 #!/usr/bin/env python
2
3 # Generate file lists used by apt-ftparchive to generate Packages and Sources files
4 # Copyright (C) 2000, 2001, 2002, 2003, 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 # <elmo> I'm doing it in python btw.. nothing against your monster
23 #        SQL, but the python wins in terms of speed and readiblity
24 # <aj> bah
25 # <aj> you suck!!!!!
26 # <elmo> sorry :(
27 # <aj> you are not!!!
28 # <aj> you mock my SQL!!!!
29 # <elmo> you want have contest of skillz??????
30 # <aj> all your skillz are belong to my sql!!!!
31 # <elmo> yo momma are belong to my python!!!!
32 # <aj> yo momma was SQLin' like a pig last night!
33
34 ################################################################################
35
36 import copy, os, pg, sys
37 import apt_pkg
38 import symlink_dists
39 from daklib import database
40 from daklib import logging
41 from daklib import utils
42
43 ################################################################################
44
45 projectB = None
46 Cnf = None
47 Logger = None
48 Options = None
49
50 ################################################################################
51
52 def Dict(**dict): return dict
53
54 ################################################################################
55
56 def usage (exit_code=0):
57     print """Usage: dak make-suite-file-list [OPTION]
58 Write out file lists suitable for use with apt-ftparchive.
59
60   -a, --architecture=ARCH   only write file lists for this architecture
61   -c, --component=COMPONENT only write file lists for this component
62   -f, --force               ignore Untouchable suite directives in dak.conf
63   -h, --help                show this help and exit
64   -n, --no-delete           don't delete older versions
65   -s, --suite=SUITE         only write file lists for this suite
66
67 ARCH, COMPONENT and SUITE can be space separated lists, e.g.
68     --architecture=\"m68k i386\""""
69     sys.exit(exit_code)
70
71 ################################################################################
72
73 def version_cmp(a, b):
74     return -apt_pkg.VersionCompare(a[0], b[0])
75
76 #####################################################
77
78 def delete_packages(delete_versions, pkg, dominant_arch, suite,
79                     dominant_version, delete_table, delete_col, packages):
80     suite_id = database.get_suite_id(suite)
81     for version in delete_versions:
82         delete_unique_id = version[1]
83         if not packages.has_key(delete_unique_id):
84             continue
85         delete_version = version[0]
86         delete_id = packages[delete_unique_id]["id"]
87         delete_arch = packages[delete_unique_id]["arch"]
88         if not Cnf.Find("Suite::%s::Untouchable" % (suite)) or Options["Force"]:
89             if Options["No-Delete"]:
90                 print "Would delete %s_%s_%s in %s in favour of %s_%s" % (pkg, delete_arch, delete_version, suite, dominant_version, dominant_arch)
91             else:
92                 Logger.log(["dominated", pkg, delete_arch, delete_version, dominant_version, dominant_arch])
93                 projectB.query("DELETE FROM %s WHERE suite = %s AND %s = %s" % (delete_table, suite_id, delete_col, delete_id))
94             del packages[delete_unique_id]
95         else:
96             if Options["No-Delete"]:
97                 print "Would delete %s_%s_%s in favour of %s_%s, but %s is untouchable" % (pkg, delete_arch, delete_version, dominant_version, dominant_arch, suite)
98             else:
99                 Logger.log(["dominated but untouchable", pkg, delete_arch, delete_version, dominant_version, dominant_arch])
100
101 #####################################################
102
103 # Per-suite&pkg: resolve arch-all, vs. arch-any, assumes only one arch-all
104 def resolve_arch_all_vs_any(versions, packages):
105     arch_all_version = None
106     arch_any_versions = copy.copy(versions)
107     for i in arch_any_versions:
108         unique_id = i[1]
109         arch = packages[unique_id]["arch"]
110         if arch == "all":
111             arch_all_versions = [i]
112             arch_all_version = i[0]
113             arch_any_versions.remove(i)
114     # Sort arch: any versions into descending order
115     arch_any_versions.sort(version_cmp)
116     highest_arch_any_version = arch_any_versions[0][0]
117
118     pkg = packages[unique_id]["pkg"]
119     suite = packages[unique_id]["suite"]
120     delete_table = "bin_associations"
121     delete_col = "bin"
122
123     if apt_pkg.VersionCompare(highest_arch_any_version, arch_all_version) < 1:
124         # arch: all dominates
125         delete_packages(arch_any_versions, pkg, "all", suite,
126                         arch_all_version, delete_table, delete_col, packages)
127     else:
128         # arch: any dominates
129         delete_packages(arch_all_versions, pkg, "any", suite,
130                         highest_arch_any_version, delete_table, delete_col,
131                         packages)
132
133 #####################################################
134
135 # Per-suite&pkg&arch: resolve duplicate versions
136 def remove_duplicate_versions(versions, packages):
137     # Sort versions into descending order
138     versions.sort(version_cmp)
139     dominant_versions = versions[0]
140     dominated_versions = versions[1:]
141     (dominant_version, dominant_unqiue_id) = dominant_versions
142     pkg = packages[dominant_unqiue_id]["pkg"]
143     arch = packages[dominant_unqiue_id]["arch"]
144     suite = packages[dominant_unqiue_id]["suite"]
145     if arch == "source":
146         delete_table = "src_associations"
147         delete_col = "source"
148     else: # !source
149         delete_table = "bin_associations"
150         delete_col = "bin"
151     # Remove all but the highest
152     delete_packages(dominated_versions, pkg, arch, suite,
153                     dominant_version, delete_table, delete_col, packages)
154     return [dominant_versions]
155
156 ################################################################################
157
158 def cleanup(packages):
159     # Build up the index used by the clean up functions
160     d = {}
161     for unique_id in packages.keys():
162         suite = packages[unique_id]["suite"]
163         pkg = packages[unique_id]["pkg"]
164         arch = packages[unique_id]["arch"]
165         version = packages[unique_id]["version"]
166         d.setdefault(suite, {})
167         d[suite].setdefault(pkg, {})
168         d[suite][pkg].setdefault(arch, [])
169         d[suite][pkg][arch].append([version, unique_id])
170     # Clean up old versions
171     for suite in d.keys():
172         for pkg in d[suite].keys():
173             for arch in d[suite][pkg].keys():
174                 versions = d[suite][pkg][arch]
175                 if len(versions) > 1:
176                     d[suite][pkg][arch] = remove_duplicate_versions(versions, packages)
177
178     # Arch: all -> any and vice versa
179     for suite in d.keys():
180         for pkg in d[suite].keys():
181             arches = d[suite][pkg]
182             # If we don't have any arch: all; we've nothing to do
183             if not arches.has_key("all"):
184                 continue
185             # Check to see if we have arch: all and arch: !all (ignoring source)
186             num_arches = len(arches.keys())
187             if arches.has_key("source"):
188                 num_arches -= 1
189             # If we do, remove the duplicates
190             if num_arches > 1:
191                 versions = []
192                 for arch in arches.keys():
193                     if arch != "source":
194                         versions.extend(d[suite][pkg][arch])
195                 resolve_arch_all_vs_any(versions, packages)
196
197 ################################################################################
198
199 def write_legacy_mixed_filelist(suite, list, packages, dislocated_files):
200     # Work out the filename
201     filename = os.path.join(Cnf["Dir::Lists"], "%s_-_all.list" % (suite))
202     output = utils.open_file(filename, "w")
203     # Generate the final list of files
204     files = {}
205     for id in list:
206         path = packages[id]["path"]
207         filename = packages[id]["filename"]
208         file_id = packages[id]["file_id"]
209         if suite == "stable" and dislocated_files.has_key(file_id):
210             filename = dislocated_files[file_id]
211         else:
212             filename = path + filename
213         if files.has_key(filename):
214             utils.warn("%s (in %s) is duplicated." % (filename, suite))
215         else:
216             files[filename] = ""
217     # Sort the files since apt-ftparchive doesn't
218     keys = files.keys()
219     keys.sort()
220     # Write the list of files out
221     for file in keys:
222         output.write(file+'\n')
223     output.close()
224
225 ############################################################
226
227 def write_filelist(suite, component, arch, type, list, packages, dislocated_files):
228     # Work out the filename
229     if arch != "source":
230         if type == "udeb":
231             arch = "debian-installer_binary-%s" % (arch)
232         elif type == "deb":
233             arch = "binary-%s" % (arch)
234     filename = os.path.join(Cnf["Dir::Lists"], "%s_%s_%s.list" % (suite, component, arch))
235     output = utils.open_file(filename, "w")
236     # Generate the final list of files
237     files = {}
238     for id in list:
239         path = packages[id]["path"]
240         filename = packages[id]["filename"]
241         file_id = packages[id]["file_id"]
242         pkg = packages[id]["pkg"]
243         if suite == "stable" and dislocated_files.has_key(file_id):
244             filename = dislocated_files[file_id]
245         else:
246             filename = path + filename
247         if files.has_key(pkg):
248             utils.warn("%s (in %s/%s, %s) is duplicated." % (pkg, suite, component, filename))
249         else:
250             files[pkg] = filename
251     # Sort the files since apt-ftparchive doesn't
252     pkgs = files.keys()
253     pkgs.sort()
254     # Write the list of files out
255     for pkg in pkgs:
256         output.write(files[pkg]+'\n')
257     output.close()
258
259 ################################################################################
260
261 def write_filelists(packages, dislocated_files):
262     # Build up the index to iterate over
263     d = {}
264     for unique_id in packages.keys():
265         suite = packages[unique_id]["suite"]
266         component = packages[unique_id]["component"]
267         arch = packages[unique_id]["arch"]
268         type = packages[unique_id]["type"]
269         d.setdefault(suite, {})
270         d[suite].setdefault(component, {})
271         d[suite][component].setdefault(arch, {})
272         d[suite][component][arch].setdefault(type, [])
273         d[suite][component][arch][type].append(unique_id)
274     # Flesh out the index
275     if not Options["Suite"]:
276         suites = Cnf.SubTree("Suite").List()
277     else:
278         suites = utils.split_args(Options["Suite"])
279     for suite in [ i.lower() for i in suites ]:
280         d.setdefault(suite, {})
281         if not Options["Component"]:
282             components = Cnf.ValueList("Suite::%s::Components" % (suite))
283         else:
284             components = utils.split_args(Options["Component"])
285         udeb_components = Cnf.ValueList("Suite::%s::UdebComponents" % (suite))
286         udeb_components = udeb_components
287         for component in components:
288             d[suite].setdefault(component, {})
289             if component in udeb_components:
290                 binary_types = [ "deb", "udeb" ]
291             else:
292                 binary_types = [ "deb" ]
293             if not Options["Architecture"]:
294                 architectures = Cnf.ValueList("Suite::%s::Architectures" % (suite))
295             else:
296                 architectures = utils.split_args(Options["Architectures"])
297             for arch in [ i.lower() for i in architectures ]:
298                 d[suite][component].setdefault(arch, {})
299                 if arch == "source":
300                     types = [ "dsc" ]
301                 else:
302                     types = binary_types
303                 for type in types:
304                     d[suite][component][arch].setdefault(type, [])
305     # Then walk it
306     for suite in d.keys():
307         if Cnf.has_key("Suite::%s::Components" % (suite)):
308             for component in d[suite].keys():
309                 for arch in d[suite][component].keys():
310                     if arch == "all":
311                         continue
312                     for type in d[suite][component][arch].keys():
313                         list = d[suite][component][arch][type]
314                         # If it's a binary, we need to add in the arch: all debs too
315                         if arch != "source":
316                             archall_suite = Cnf.get("Make-Suite-File-List::ArchAllMap::%s" % (suite))
317                             if archall_suite:
318                                 list.extend(d[archall_suite][component]["all"][type])
319                             elif d[suite][component].has_key("all") and \
320                                      d[suite][component]["all"].has_key(type):
321                                 list.extend(d[suite][component]["all"][type])
322                         write_filelist(suite, component, arch, type, list,
323                                        packages, dislocated_files)
324         else: # legacy-mixed suite
325             list = []
326             for component in d[suite].keys():
327                 for arch in d[suite][component].keys():
328                     for type in d[suite][component][arch].keys():
329                         list.extend(d[suite][component][arch][type])
330             write_legacy_mixed_filelist(suite, list, packages, dislocated_files)
331
332 ################################################################################
333
334 def do_da_do_da():
335     # If we're only doing a subset of suites, ensure we do enough to
336     # be able to do arch: all mapping.
337     if Options["Suite"]:
338         suites = utils.split_args(Options["Suite"])
339         for suite in suites:
340             archall_suite = Cnf.get("Make-Suite-File-List::ArchAllMap::%s" % (suite))
341             if archall_suite and archall_suite not in suites:
342                 utils.warn("Adding %s as %s maps Arch: all from it." % (archall_suite, suite))
343                 suites.append(archall_suite)
344         Options["Suite"] = ",".join(suites)
345
346     (con_suites, con_architectures, con_components, check_source) = \
347                  utils.parse_args(Options)
348
349     dislocated_files = {}
350
351     query = """
352 SELECT b.id, b.package, a.arch_string, b.version, l.path, f.filename, c.name,
353        f.id, su.suite_name, b.type
354   FROM binaries b, bin_associations ba, architecture a, files f, location l,
355        component c, suite su
356   WHERE b.id = ba.bin AND b.file = f.id AND b.architecture = a.id
357     AND f.location = l.id AND l.component = c.id AND ba.suite = su.id
358     %s %s %s""" % (con_suites, con_architectures, con_components)
359     if check_source:
360         query += """
361 UNION
362 SELECT s.id, s.source, 'source', s.version, l.path, f.filename, c.name, f.id,
363        su.suite_name, 'dsc'
364   FROM source s, src_associations sa, files f, location l, component c, suite su
365   WHERE s.id = sa.source AND s.file = f.id AND f.location = l.id
366     AND l.component = c.id AND sa.suite = su.id %s %s""" % (con_suites, con_components)
367     q = projectB.query(query)
368     ql = q.getresult()
369     # Build up the main index of packages
370     packages = {}
371     unique_id = 0
372     for i in ql:
373         (id, pkg, arch, version, path, filename, component, file_id, suite, type) = i
374         # 'id' comes from either 'binaries' or 'source', so it's not unique
375         unique_id += 1
376         packages[unique_id] = Dict(id=id, pkg=pkg, arch=arch, version=version,
377                                    path=path, filename=filename,
378                                    component=component, file_id=file_id,
379                                    suite=suite, type = type)
380     cleanup(packages)
381     write_filelists(packages, dislocated_files)
382
383 ################################################################################
384
385 def main():
386     global Cnf, projectB, Options, Logger
387
388     Cnf = utils.get_conf()
389     Arguments = [('a', "architecture", "Make-Suite-File-List::Options::Architecture", "HasArg"),
390                  ('c', "component", "Make-Suite-File-List::Options::Component", "HasArg"),
391                  ('h', "help", "Make-Suite-File-List::Options::Help"),
392                  ('n', "no-delete", "Make-Suite-File-List::Options::No-Delete"),
393                  ('f', "force", "Make-Suite-File-List::Options::Force"),
394                  ('s', "suite", "Make-Suite-File-List::Options::Suite", "HasArg")]
395     for i in ["architecture", "component", "help", "no-delete", "suite", "force" ]:
396         if not Cnf.has_key("Make-Suite-File-List::Options::%s" % (i)):
397             Cnf["Make-Suite-File-List::Options::%s" % (i)] = ""
398     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
399     Options = Cnf.SubTree("Make-Suite-File-List::Options")
400     if Options["Help"]:
401         usage()
402
403     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
404     database.init(Cnf, projectB)
405     Logger = logging.Logger(Cnf, "make-suite-file-list")
406     do_da_do_da()
407     Logger.close()
408
409 #########################################################################################
410
411 if __name__ == '__main__':
412     main()