]> git.decadent.org.uk Git - dak.git/blob - jenna
Use parse_args()
[dak.git] / jenna
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  James Troup <james@nocrew.org>
5 # $Id: jenna,v 1.20 2002-07-14 15:02:07 troup Exp $
6
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.
11
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.
16
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
20
21 ################################################################################
22
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
25 # <aj> bah
26 # <aj> you suck!!!!!
27 # <elmo> sorry :(
28 # <aj> you are not!!!
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!
34
35 ################################################################################
36
37 import copy, os, pg, string, sys;
38 import apt_pkg;
39 import claire, db_access, logging, utils;
40
41 ################################################################################
42
43 projectB = None;
44 Cnf = None;
45 Logger = None;
46 Options = None;
47
48 ################################################################################
49
50 def Dict(**dict): return dict
51
52 ################################################################################
53
54 def usage (exit_code=0):
55     print """Usage: jenna [OPTION]
56 Write out file lists suitable for use with apt-ftparchive.
57
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
63
64 ARCH, COMPONENT and SUITE can be space seperated lists, e.g.
65     --architecture=\"m68k i386\"""";
66     sys.exit(exit_code);
67
68 ################################################################################
69
70 def version_cmp(a, b):
71     return -apt_pkg.VersionCompare(a[0], b[0]);
72
73 #####################################################
74
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):
81             continue;
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);
88             else:
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];
92         else:
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);
95             else:
96                 Logger.log(["dominated but untouchable", pkg, delete_arch, delete_version, dominant_version, dominant_arch]);
97
98 #####################################################
99
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:
105         unique_id = i[1];
106         arch = packages[unique_id]["arch"];
107         if arch == "all":
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];
114
115     pkg = packages[unique_id]["pkg"];
116     suite = packages[unique_id]["suite"];
117     delete_table = "bin_associations";
118     delete_col = "bin";
119
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);
124     else:
125         # arch: any dominates
126         delete_packages(arch_all_versions, pkg, "any", suite,
127                         highest_arch_any_version, delete_table, delete_col,
128                         packages);
129
130 #####################################################
131
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"];
142     if arch == "source":
143         delete_table = "src_associations";
144         delete_col = "source";
145     else: # !source
146         delete_table = "bin_associations";
147         delete_col = "bin";
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;
152
153 ################################################################################
154
155 def cleanup(packages):
156     # Build up the index used by the clean up functions
157     d = {};
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):
164             d[suite] = {};
165         if not d[suite].has_key(pkg):
166             d[suite][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);
177
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"):
184                 continue;
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
190             if num_arches > 1:
191                 versions = [];
192                 for arch in arches.keys():
193                     if arch != "source":
194                         versions.extend(d[suite][pkg][arch]);
195                 remove_duplicate_versions(versions, packages);
196
197 ################################################################################
198
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
204     files = {};
205     for id in list:
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];
211         else:
212             filename = path + filename;
213         if files.has_key(filename):
214             utils.warn("%s (in %s) is duplicated." % (filename, suite));
215         else:
216             files[filename] = "";
217     # Sort the files since apt-ftparchive doesn't
218     keys = files.keys();
219     keys.sort();
220     # Write the list of files out
221     for file in keys:
222         output.write(file+'\n')
223     output.close();
224
225 ############################################################
226
227 def write_filelist(suite, component, arch, type, list, packages, dislocated_files):
228     # Work out the filename
229     if arch != "source":
230         if type == "udeb":
231             arch = "debian-installer_binary-%s" % (arch);
232         elif type == "deb":
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
237     files = {};
238     for id in list:
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];
245         else:
246             filename = path + filename;
247         if files.has_key(pkg):
248             utils.warn("%s (in %s/%s, %s) is duplicated." % (pkg, suite, component, filename));
249         else:
250             files[pkg] = filename;
251     # Sort the files since apt-ftparchive doesn't
252     pkgs = files.keys();
253     pkgs.sort();
254     # Write the list of files out
255     for pkg in pkgs:
256         output.write(files[pkg]+'\n')
257     output.close();
258
259 ################################################################################
260
261 def write_filelists(packages, dislocated_files):
262     # Build up the index to iterate over
263     d = {};
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):
270             d[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();
283     else:
284         suites = string.split(Options["Suite"]);
285     for suite in map(string.lower, suites):
286         if not d.has_key(suite):
287             d[suite] = {};
288         if not Options["Component"]:
289             components = Cnf.ValueList("Suite::%s::Components" % (suite));
290         else:
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" ];
299             else:
300                 binary_types = [ "deb" ];
301             if not Options["Architecture"]:
302                 architectures = Cnf.ValueList("Suite::%s::Architectures" % (suite));
303             else:
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] = {};
308                 if arch == "source":
309                     types = [ "dsc" ];
310                 else:
311                     types = binary_types;
312                 for type in types:
313                     if not d[suite][component][arch].has_key(type):
314                         d[suite][component][arch][type] = [];
315     # Then walk it
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():
320                     if arch == "all":
321                         continue;
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
331             list = [];
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);
337
338 ################################################################################
339
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"):
344         return 0;
345     # If we don't have a stable suite, obviously a no-op
346     if not Cnf.has_key("Suite::Stable"):
347         return 0;
348     # If the suite(s) weren't explicitly listed, all suites are done
349     if not Options["Suite"]:
350         return 1;
351     # Otherwise, look in what suites the user specified
352     suites = string.split(Options["Suite"]);
353     return suites.count("stable");
354
355 ################################################################################
356
357 def do_da_do_da():
358     (con_suites, con_architectures, con_components, check_source) = \
359                  utils.parse_args(Options);
360
361     if stable_dislocation_p():
362         dislocated_files = claire.find_dislocated_stable(Cnf, projectB);
363     else:
364         dislocated_files = {};
365
366     query = """
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);
374     if check_source:
375         query = query + """
376 UNION
377 SELECT s.id, s.source, 'source', s.version, l.path, f.filename, c.name, f.id,
378        su.suite_name, 'dsc'
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);
383     ql = q.getresult();
384     # Build up the main index of packages
385     packages = {};
386     unique_id = 0;
387     for i in ql:
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);
395     cleanup(packages);
396     write_filelists(packages, dislocated_files);
397
398 ################################################################################
399
400 def main():
401     global Cnf, projectB, Options, Logger;
402
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");
414     if Options["Help"]:
415         usage();
416
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");
420     do_da_do_da();
421     Logger.close();
422
423 #########################################################################################
424
425 if __name__ == '__main__':
426     main();