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