3 # Generate file lists used by apt-ftparchive to generate Packages and Sources files
4 # Copyright (C) 2000, 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: jenna,v 1.20 2002-07-14 15:02:07 troup Exp $
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ################################################################################
23 # <elmo> I'm doing it in python btw.. nothing against your monster
24 # SQL, but the python wins in terms of speed and readiblity
29 # <aj> you mock my SQL!!!!
30 # <elmo> you want have contest of skillz??????
31 # <aj> all your skillz are belong to my sql!!!!
32 # <elmo> yo momma are belong to my python!!!!
33 # <aj> yo momma was SQLin' like a pig last night!
35 ################################################################################
37 import copy, os, pg, string, sys;
39 import claire, db_access, logging, utils;
41 ################################################################################
48 ################################################################################
50 def Dict(**dict): return dict
52 ################################################################################
54 def usage (exit_code=0):
55 print """Usage: jenna [OPTION]
56 Write out file lists suitable for use with apt-ftparchive.
58 -a, --architecture=ARCH only write file lists for this architecture
59 -c, --component=COMPONENT only write file lists for this component
60 -h, --help show this help and exit
61 -n, --no-delete don't delete older versions
62 -s, --suite=SUITE only write file lists for this suite
64 ARCH, COMPONENT and SUITE can be space seperated lists, e.g.
65 --architecture=\"m68k i386\"""";
68 ################################################################################
70 def version_cmp(a, b):
71 return -apt_pkg.VersionCompare(a[0], b[0]);
73 #####################################################
75 def delete_packages(delete_versions, pkg, dominant_arch, suite,
76 dominant_version, delete_table, delete_col, packages):
77 suite_id = db_access.get_suite_id(suite);
78 for version in delete_versions:
79 delete_unique_id = version[1];
80 if not packages.has_key(delete_unique_id):
82 delete_version = version[0];
83 delete_id = packages[delete_unique_id]["id"];
84 delete_arch = packages[delete_unique_id]["arch"];
85 if not Cnf.Find("Suite::%s::Untouchable" % (suite)):
86 if Options["No-Delete"]:
87 print "Would delete %s_%s_%s in %s in favour of %s_%s" % (pkg, delete_arch, delete_version, suite, dominant_version, dominant_arch);
89 Logger.log(["dominated", pkg, delete_arch, delete_version, dominant_version, dominant_arch]);
90 projectB.query("DELETE FROM %s WHERE suite = %s AND %s = %s" % (delete_table, suite_id, delete_col, delete_id));
91 del packages[delete_unique_id];
93 if Options["No-Delete"]:
94 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);
96 Logger.log(["dominated but untouchable", pkg, delete_arch, delete_version, dominant_version, dominant_arch]);
98 #####################################################
100 # Per-suite&pkg: resolve arch-all, vs. arch-any, assumes only one arch-all
101 def resolve_arch_all_vs_any(versions, packages):
102 arch_all_version = None;
103 arch_any_versions = copy.copy(versions);
104 for i in arch_any_versions:
106 arch = packages[unique_id]["arch"];
108 arch_all_versions = i;
109 arch_all_version = i[0];
110 arch_any_versions.remove(i);
111 # Sort arch: any versions into descending order
112 arch_any_versions.sort(version_cmp);
113 highest_arch_any_version = arch_any_versions[0][0];
115 pkg = packages[unique_id]["pkg"];
116 suite = packages[unique_id]["suite"];
117 delete_table = "bin_associations";
120 if apt_pkg.VersionCompare(highest_arch_any_version, arch_all_version) != 1:
121 # arch: all dominates
122 delete_packages(arch_any_versions, pkg, "all", suite,
123 arch_all_version, delete_table, delete_col, packages);
125 # arch: any dominates
126 delete_packages(arch_all_versions, pkg, "any", suite,
127 highest_arch_any_version, delete_table, delete_col,
130 #####################################################
132 # Per-suite&pkg&arch: resolve duplicate versions
133 def remove_duplicate_versions(versions, packages):
134 # Sort versions into descending order
135 versions.sort(version_cmp);
136 dominant_versions = versions[0];
137 dominated_versions = versions[1:];
138 (dominant_version, dominant_unqiue_id) = dominant_versions;
139 pkg = packages[dominant_unqiue_id]["pkg"];
140 arch = packages[dominant_unqiue_id]["arch"];
141 suite = packages[dominant_unqiue_id]["suite"];
143 delete_table = "src_associations";
144 delete_col = "source";
146 delete_table = "bin_associations";
148 # Remove all but the highest
149 delete_packages(dominated_versions, pkg, arch, suite,
150 dominant_version, delete_table, delete_col, packages);
151 return dominant_versions;
153 ################################################################################
155 def cleanup(packages):
156 # Build up the index used by the clean up functions
158 for unique_id in packages.keys():
159 suite = packages[unique_id]["suite"];
160 pkg = packages[unique_id]["pkg"];
161 arch = packages[unique_id]["arch"];
162 version = packages[unique_id]["version"];
163 if not d.has_key(suite):
165 if not d[suite].has_key(pkg):
167 if not d[suite][pkg].has_key(arch):
168 d[suite][pkg][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"):
188 num_arches = num_arches - 1;
189 # If we do, remove the duplicates
192 for arch in arches.keys():
194 versions.extend(d[suite][pkg][arch]);
195 remove_duplicate_versions(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));
216 files[filename] = "";
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 if not d.has_key(suite):
271 if not d[suite].has_key(component):
272 d[suite][component] = {};
273 if not d[suite][component].has_key(arch):
274 d[suite][component][arch] = {};
275 if not d[suite][component].has_key(arch):
276 d[suite][component][arch] = {};
277 if not d[suite][component][arch].has_key(type):
278 d[suite][component][arch][type] = [];
279 d[suite][component][arch][type].append(unique_id);
280 # Flesh out the index
281 if not Options["Suite"]:
282 suites = Cnf.SubTree("Suite").List();
284 suites = string.split(Options["Suite"]);
285 for suite in map(string.lower, suites):
286 if not d.has_key(suite):
288 if not Options["Component"]:
289 components = Cnf.ValueList("Suite::%s::Components" % (suite));
291 components = string.split(Options["Components"]);
292 udeb_components = Cnf.ValueList("Suite::%s::UdebComponents" % (suite));
293 udeb_components = udeb_components;
294 for component in components:
295 if not d[suite].has_key(component):
296 d[suite][component] = {};
297 if component in udeb_components:
298 binary_types = [ "deb", "udeb" ];
300 binary_types = [ "deb" ];
301 if not Options["Architecture"]:
302 architectures = Cnf.ValueList("Suite::%s::Architectures" % (suite));
304 architectures = string.split(Options["Architectures"]);
305 for arch in map(string.lower, architectures):
306 if not d[suite][component].has_key(arch):
307 d[suite][component][arch] = {};
311 types = binary_types;
313 if not d[suite][component][arch].has_key(type):
314 d[suite][component][arch][type] = [];
316 for suite in d.keys():
317 if Cnf.has_key("Suite::%s::Components" % (suite)):
318 for component in d[suite].keys():
319 for arch in d[suite][component].keys():
322 for type in d[suite][component][arch].keys():
323 list = d[suite][component][arch][type];
324 # If it's a binary, we need to add in the arch: all debs too
325 if arch != "source" and d[suite][component].has_key("all") \
326 and d[suite][component]["all"].has_key(type):
327 list.extend(d[suite][component]["all"][type]);
328 write_filelist(suite, component, arch, type, list,
329 packages, dislocated_files);
330 else: # legacy-mixed suite
332 for component in d[suite].keys():
333 for arch in d[suite][component].keys():
334 for type in d[suite][component][arch].keys():
335 list.extend(d[suite][component][arch][type]);
336 write_legacy_mixed_filelist(suite, list, packages, dislocated_files);
338 ################################################################################
340 # Want to use stable dislocation support: True or false?
341 def stable_dislocation_p():
342 # If the support is not explicitly enabled, assume it's disabled
343 if not Cnf.FindB("Dinstall::StableDislocationSupport"):
345 # If we don't have a stable suite, obviously a no-op
346 if not Cnf.has_key("Suite::Stable"):
348 # If the suite(s) weren't explicitly listed, all suites are done
349 if not Options["Suite"]:
351 # Otherwise, look in what suites the user specified
352 suites = string.split(Options["Suite"]);
353 return suites.count("stable");
355 ################################################################################
358 (con_suites, con_architectures, con_components, check_source) = \
359 utils.parse_args(Options);
361 if stable_dislocation_p():
362 dislocated_files = claire.find_dislocated_stable(Cnf, projectB);
364 dislocated_files = {};
367 SELECT b.id, b.package, a.arch_string, b.version, l.path, f.filename, c.name,
368 f.id, su.suite_name, b.type
369 FROM binaries b, bin_associations ba, architecture a, files f, location l,
370 component c, suite su
371 WHERE b.id = ba.bin AND b.file = f.id AND b.architecture = a.id
372 AND f.location = l.id AND l.component = c.id AND ba.suite = su.id
373 %s %s %s""" % (con_suites, con_architectures, con_components);
377 SELECT s.id, s.source, 'source', s.version, l.path, f.filename, c.name, f.id,
379 FROM source s, src_associations sa, files f, location l, component c, suite su
380 WHERE s.id = sa.source AND s.file = f.id AND f.location = l.id
381 AND l.component = c.id AND sa.suite = su.id %s %s""" % (con_suites, con_components);
382 q = projectB.query(query);
384 # Build up the main index of packages
388 (id, pkg, arch, version, path, filename, component, file_id, suite, type) = i;
389 # 'id' comes from either 'binaries' or 'source', so it's not unique
390 unique_id = unique_id + 1;
391 packages[unique_id] = Dict(id=id, pkg=pkg, arch=arch, version=version,
392 path=path, filename=filename,
393 component=component, file_id=file_id,
394 suite=suite, type = type);
396 write_filelists(packages, dislocated_files);
398 ################################################################################
401 global Cnf, projectB, Options, Logger;
403 Cnf = utils.get_conf();
404 Arguments = [('a', "architecture", "Jenna::Options::Architecture", "HasArg"),
405 ('c', "component", "Jenna::Options::Component", "HasArg"),
406 ('h', "help", "Jenna::Options::Help"),
407 ('n', "no-delete", "Jenna::Options::No-Delete"),
408 ('s', "suite", "Jenna::Options::Suite", "HasArg")];
409 for i in ["architecture", "component", "help", "no-delete", "suite" ]:
410 if not Cnf.has_key("Jenna::Options::%s" % (i)):
411 Cnf["Jenna::Options::%s" % (i)] = "";
412 apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
413 Options = Cnf.SubTree("Jenna::Options");
417 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
418 db_access.init(Cnf, projectB);
419 Logger = logging.Logger(Cnf, "jenna");
423 #########################################################################################
425 if __name__ == '__main__':