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, sys
39 from daklib import database
40 from daklib import logging
41 from daklib import utils
43 ################################################################################
50 ################################################################################
52 def Dict(**dict): return dict
54 ################################################################################
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.
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
67 ARCH, COMPONENT and SUITE can be space separated lists, e.g.
68 --architecture=\"m68k i386\""""
71 ################################################################################
73 def version_cmp(a, b):
74 return -apt_pkg.VersionCompare(a[0], b[0])
76 #####################################################
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):
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)
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]
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)
99 Logger.log(["dominated but untouchable", pkg, delete_arch, delete_version, dominant_version, dominant_arch])
101 #####################################################
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:
109 arch = packages[unique_id]["arch"]
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]
118 pkg = packages[unique_id]["pkg"]
119 suite = packages[unique_id]["suite"]
120 delete_table = "bin_associations"
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)
128 # arch: any dominates
129 delete_packages(arch_all_versions, pkg, "any", suite,
130 highest_arch_any_version, delete_table, delete_col,
133 #####################################################
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"]
146 delete_table = "src_associations"
147 delete_col = "source"
149 delete_table = "bin_associations"
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]
156 ################################################################################
158 def cleanup(packages):
159 # Build up the index used by the clean up functions
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)
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"):
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"):
189 # If we do, remove the duplicates
192 for arch in arches.keys():
194 versions.extend(d[suite][pkg][arch])
195 resolve_arch_all_vs_any(versions, packages)
197 ################################################################################
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
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]
212 filename = path + filename
213 if files.has_key(filename):
214 utils.warn("%s (in %s) is duplicated." % (filename, suite))
217 # Sort the files since apt-ftparchive doesn't
220 # Write the list of files out
222 output.write(file+'\n')
225 ############################################################
227 def write_filelist(suite, component, arch, type, list, packages, dislocated_files):
228 # Work out the filename
231 arch = "debian-installer_binary-%s" % (arch)
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
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]
246 filename = path + filename
247 if files.has_key(pkg):
248 utils.warn("%s (in %s/%s, %s) is duplicated." % (pkg, suite, component, filename))
250 files[pkg] = filename
251 # Sort the files since apt-ftparchive doesn't
254 # Write the list of files out
256 output.write(files[pkg]+'\n')
259 ################################################################################
261 def write_filelists(packages, dislocated_files):
262 # Build up the index to iterate over
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()
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))
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" ]
292 binary_types = [ "deb" ]
293 if not Options["Architecture"]:
294 architectures = Cnf.ValueList("Suite::%s::Architectures" % (suite))
296 architectures = utils.split_args(Options["Architectures"])
297 for arch in [ i.lower() for i in architectures ]:
298 d[suite][component].setdefault(arch, {})
304 d[suite][component][arch].setdefault(type, [])
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():
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
316 archall_suite = Cnf.get("Make-Suite-File-List::ArchAllMap::%s" % (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
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)
332 ################################################################################
334 # Want to use stable dislocation support: True or false?
335 def stable_dislocation_p():
336 # If the support is not explicitly enabled, assume it's disabled
337 if not Cnf.FindB("Dinstall::StableDislocationSupport"):
339 # If we don't have a stable suite, obviously a no-op
340 if not Cnf.has_key("Suite::Stable"):
342 # If the suite(s) weren't explicitly listed, all suites are done
343 if not Options["Suite"]:
345 # Otherwise, look in what suites the user specified
346 suites = utils.split_args(Options["Suite"])
348 if "stable" in suites:
353 ################################################################################
356 # If we're only doing a subset of suites, ensure we do enough to
357 # be able to do arch: all mapping.
359 suites = utils.split_args(Options["Suite"])
361 archall_suite = Cnf.get("Make-Suite-File-List::ArchAllMap::%s" % (suite))
362 if archall_suite and archall_suite not in suites:
363 utils.warn("Adding %s as %s maps Arch: all from it." % (archall_suite, suite))
364 suites.append(archall_suite)
365 Options["Suite"] = ",".join(suites)
367 (con_suites, con_architectures, con_components, check_source) = \
368 utils.parse_args(Options)
370 if stable_dislocation_p():
371 dislocated_files = symlink_dists.find_dislocated_stable(Cnf, projectB)
373 dislocated_files = {}
376 SELECT b.id, b.package, a.arch_string, b.version, l.path, f.filename, c.name,
377 f.id, su.suite_name, b.type
378 FROM binaries b, bin_associations ba, architecture a, files f, location l,
379 component c, suite su
380 WHERE b.id = ba.bin AND b.file = f.id AND b.architecture = a.id
381 AND f.location = l.id AND l.component = c.id AND ba.suite = su.id
382 %s %s %s""" % (con_suites, con_architectures, con_components)
386 SELECT s.id, s.source, 'source', s.version, l.path, f.filename, c.name, f.id,
388 FROM source s, src_associations sa, files f, location l, component c, suite su
389 WHERE s.id = sa.source AND s.file = f.id AND f.location = l.id
390 AND l.component = c.id AND sa.suite = su.id %s %s""" % (con_suites, con_components)
391 q = projectB.query(query)
393 # Build up the main index of packages
397 (id, pkg, arch, version, path, filename, component, file_id, suite, type) = i
398 # 'id' comes from either 'binaries' or 'source', so it's not unique
400 packages[unique_id] = Dict(id=id, pkg=pkg, arch=arch, version=version,
401 path=path, filename=filename,
402 component=component, file_id=file_id,
403 suite=suite, type = type)
405 write_filelists(packages, dislocated_files)
407 ################################################################################
410 global Cnf, projectB, Options, Logger
412 Cnf = utils.get_conf()
413 Arguments = [('a', "architecture", "Make-Suite-File-List::Options::Architecture", "HasArg"),
414 ('c', "component", "Make-Suite-File-List::Options::Component", "HasArg"),
415 ('h', "help", "Make-Suite-File-List::Options::Help"),
416 ('n', "no-delete", "Make-Suite-File-List::Options::No-Delete"),
417 ('f', "force", "Make-Suite-File-List::Options::Force"),
418 ('s', "suite", "Make-Suite-File-List::Options::Suite", "HasArg")]
419 for i in ["architecture", "component", "help", "no-delete", "suite", "force" ]:
420 if not Cnf.has_key("Make-Suite-File-List::Options::%s" % (i)):
421 Cnf["Make-Suite-File-List::Options::%s" % (i)] = ""
422 apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv)
423 Options = Cnf.SubTree("Make-Suite-File-List::Options")
427 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
428 database.init(Cnf, projectB)
429 Logger = logging.Logger(Cnf, "make-suite-file-list")
433 #########################################################################################
435 if __name__ == '__main__':