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