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>
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.
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.
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
20 ################################################################################
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
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!
34 ################################################################################
36 import copy, os, pg, string, sys
38 import poolize, dak.lib.database, dak.lib.logging, dak.lib.utils
40 ################################################################################
47 ################################################################################
49 def Dict(**dict): return dict
51 ################################################################################
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.
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
63 ARCH, COMPONENT and SUITE can be space separated lists, e.g.
64 --architecture=\"m68k i386\""""
67 ################################################################################
69 def version_cmp(a, b):
70 return -apt_pkg.VersionCompare(a[0], b[0])
72 #####################################################
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):
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)
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]
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)
95 Logger.log(["dominated but untouchable", pkg, delete_arch, delete_version, dominant_version, dominant_arch])
97 #####################################################
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:
105 arch = packages[unique_id]["arch"]
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]
114 pkg = packages[unique_id]["pkg"]
115 suite = packages[unique_id]["suite"]
116 delete_table = "bin_associations"
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)
124 # arch: any dominates
125 delete_packages(arch_all_versions, pkg, "any", suite,
126 highest_arch_any_version, delete_table, delete_col,
129 #####################################################
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"]
142 delete_table = "src_associations"
143 delete_col = "source"
145 delete_table = "bin_associations"
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]
152 ################################################################################
154 def cleanup(packages):
155 # Build up the index used by the clean up functions
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)
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"):
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"):
185 # If we do, remove the duplicates
188 for arch in arches.keys():
190 versions.extend(d[suite][pkg][arch])
191 resolve_arch_all_vs_any(versions, packages)
193 ################################################################################
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
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]
208 filename = path + filename
209 if files.has_key(filename):
210 dak.lib.utils.warn("%s (in %s) is duplicated." % (filename, suite))
213 # Sort the files since apt-ftparchive doesn't
216 # Write the list of files out
218 output.write(file+'\n')
221 ############################################################
223 def write_filelist(suite, component, arch, type, list, packages, dislocated_files):
224 # Work out the filename
227 arch = "debian-installer_binary-%s" % (arch)
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
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]
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))
246 files[pkg] = filename
247 # Sort the files since apt-ftparchive doesn't
250 # Write the list of files out
252 output.write(files[pkg]+'\n')
255 ################################################################################
257 def write_filelists(packages, dislocated_files):
258 # Build up the index to iterate over
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()
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))
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" ]
288 binary_types = [ "deb" ]
289 if not Options["Architecture"]:
290 architectures = Cnf.ValueList("Suite::%s::Architectures" % (suite))
292 architectures = dak.lib.utils.split_args(Options["Architectures"])
293 for arch in map(string.lower, architectures):
294 d[suite][component].setdefault(arch, {})
300 d[suite][component][arch].setdefault(type, [])
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():
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
312 archall_suite = Cnf.get("Make-Suite-File-List::ArchAllMap::%s" % (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
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)
328 ################################################################################
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"):
335 # If we don't have a stable suite, obviously a no-op
336 if not Cnf.has_key("Suite::Stable"):
338 # If the suite(s) weren't explicitly listed, all suites are done
339 if not Options["Suite"]:
341 # Otherwise, look in what suites the user specified
342 suites = dak.lib.utils.split_args(Options["Suite"])
344 if "stable" in suites:
349 ################################################################################
352 # If we're only doing a subset of suites, ensure we do enough to
353 # be able to do arch: all mapping.
355 suites = dak.lib.utils.split_args(Options["Suite"])
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)
363 (con_suites, con_architectures, con_components, check_source) = \
364 dak.lib.utils.parse_args(Options)
366 if stable_dislocation_p():
367 dislocated_files = symlink_dists.find_dislocated_stable(Cnf, projectB)
369 dislocated_files = {}
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)
382 SELECT s.id, s.source, 'source', s.version, l.path, f.filename, c.name, f.id,
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)
389 # Build up the main index of packages
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
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)
401 write_filelists(packages, dislocated_files)
403 ################################################################################
406 global Cnf, projectB, Options, Logger
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")
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")
428 #########################################################################################
430 if __name__ == '__main__':