]> git.decadent.org.uk Git - dak.git/blob - jenna
logging support
[dak.git] / jenna
1 #!/usr/bin/env python
2
3 # Generate file list which is then fed to apt-ftparchive to generate Packages and Sources files
4 # Copyright (C) 2000, 2001  James Troup <james@nocrew.org>
5 # $Id: jenna,v 1.13 2001-08-21 15:47:47 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 # BTAF: "GOD *DAMMIT*!!  What the FUCK happened to my free will??"
24 #
25 # -- http://www.angryflower.com/timelo.gif
26
27 #######################################################################################
28
29 import pg, string, os, sys
30 import apt_pkg
31 import db_access, utils, claire, logging
32
33 projectB = None
34 Cnf = None
35 Logger = None;
36
37 def generate_src_list(suite, component, output, dislocated_files):
38     sources = {}
39
40     suite_id = db_access.get_suite_id(suite);
41
42     if component == "-":
43         q = projectB.query("SELECT s.source, l.path, f.filename, f.id FROM source s, src_associations sa, location l, files f WHERE sa.source = s.id AND sa.suite = '%d' AND l.id = f.location AND s.file = f.id ORDER BY s.source, s.version"
44                            % (suite_id));
45     else:
46         q = projectB.query("SELECT s.source, l.path, f.filename, f.id FROM source s, src_associations sa, location l, component c, files f WHERE lower(c.name) = '%s' AND (c.id = l.component OR l.component = NULL) AND sa.source = s.id AND sa.suite = '%d' AND l.id = f.location AND s.file = f.id ORDER BY s.source, s.version"
47                            % (component, suite_id));
48     entries = q.getresult();
49     for entry in entries:
50         (source, path, filename, file_id) = entry;
51         if dislocated_files.has_key(file_id):
52             filename = dislocated_files[file_id];
53         else:
54             filename = path + filename;
55         if sources.has_key(source):
56             utils.warn("%s in %s / %s / source is duplicated." % (source, suite, component));
57         else:
58             sources[source] = filename;
59
60     # Write the list of files out
61     source_keys = sources.keys();
62     source_keys.sort();
63     for source in source_keys:
64         output.write(sources[source]+'\n')
65
66 #########################################################################################
67
68 def generate_bin_list(suite, component, architecture, output, type, dislocated_files):
69     packages = {}
70
71     suite_id = db_access.get_suite_id(suite);
72
73     if component == "-":
74         q = projectB.query("SELECT b.package, l.path, f.filename, f.id FROM architecture a, binaries b, bin_associations ba, location l, files f WHERE ( a.arch_string = '%s' OR a.arch_string = 'all' ) AND a.id = b.architecture AND ba.bin = b.id AND ba.suite = '%d' AND l.id = f.location AND b.file = f.id AND b.type = '%s' ORDER BY b.package, b.version, a.arch_string" % (architecture, suite_id, type));
75     else:
76         q = projectB.query("SELECT b.package, l.path, f.filename, f.id FROM architecture a, binaries b, bin_associations ba, location l, component c, files f WHERE lower(c.name) = '%s' AND (c.id = l.component OR l.component = NULL) AND (a.arch_string = '%s' OR a.arch_string = 'all') AND a.id = b.architecture AND ba.bin = b.id AND ba.suite = '%d' AND l.id = f.location AND b.file = f.id AND b.type = '%s' ORDER BY b.package, b.version, a.arch_string" % (component, architecture, suite_id, type));
77     entries = q.getresult();
78     for entry in entries:
79         (package, path, filename, file_id) = entry;
80         if dislocated_files.has_key(file_id):
81             filename = dislocated_files[file_id];
82         else:
83             filename = path + filename;
84         if packages.has_key(package):
85             utils.warn("%s in %s / %s / %s / %s is duplicated." % (package, suite, component, architecture, type));
86         else:
87             packages[package] = filename;
88
89     # Write the list of files out
90     package_keys = packages.keys();
91     package_keys.sort();
92     for package in package_keys:
93         output.write(packages[package]+'\n')
94
95 #########################################################################################
96
97 ##########
98 # <elmo> I'm doing it in python btw.. nothing against your monster
99 #        SQL, but the python wins in terms of speed and readiblity
100 # <aj> bah
101 # <aj> you suck!!!!!
102 # <elmo> sorry :(
103 # <aj> you are not!!!
104 # <aj> you mock my SQL!!!!
105 # <elmo> you want have contest of skillz??????
106 # <aj> all your skillz are belong to my sql!!!!
107 # <elmo> yo momma are belong to my python!!!!
108 # <aj> yo momma was SQLin' like a pig last night!
109 ##########
110
111 # If something has gone from arch:all to arch:any or vice-versa,
112 # clean out the old versions here.  The rest of jenna won't do this
113 # because it's lame. I have no idea. </aj>
114
115 def clean_duplicate_packages(suite):
116     Logger.log(["Cleaning duplicate packages", suite]);
117
118     suite_id = db_access.get_suite_id(suite)
119     q = projectB.query("""
120 SELECT b1.package,
121        b1.id, b1.version, a1.arch_string,
122        b2.id, b2.version, a2.arch_string
123   FROM bin_associations ba1, binaries b1, architecture a1,
124        bin_associations ba2, binaries b2, architecture a2
125  WHERE ba1.suite = ba2.suite AND ba1.suite = %s
126    AND ba1.bin = b1.id AND b1.architecture = a1.id
127    AND ba2.bin = b2.id AND b2.architecture = a2.id
128    AND b1.package = b2.package
129    AND (a1.id = a2.id OR a1.arch_string = 'all' OR a2.arch_string = 'all')
130    AND b1.id != b2.id
131    AND versioncmp(b1.version, b2.version) <= 0
132 ORDER BY b1.package, b1.version, a1.arch_string;""" % (suite_id))
133
134     ql = q.getresult()
135     seen = {}
136     for i in ql:
137         (package, oldid, oldver, oldarch, newid, newver, newarch) = i
138         if not seen.has_key(oldid):
139             seen[oldid] = newid
140             Logger.log(["Removing", package, oldver, oldarch, newver, newarch]);
141             projectB.query("DELETE FROM bin_associations WHERE suite = %s AND bin = %s" % (suite_id, oldid))
142         else:
143             Logger.log(["Superceded", package, oldver, oldarch, newver, newarch]);
144
145 # If something has moved from one component to another we need to
146 # clean out the old versions here.  The rest of jenna won't do this
147 # because it works on a per-component level for flexibility.
148
149 def clean_suite (suite):
150     Logger.log(["Cleaning out packages", suite]);
151
152     suite_id = db_access.get_suite_id(suite)
153     q = projectB.query("""
154 SELECT b.id, b.package, a.arch_string, b.version, l.path, f.filename, c.name
155   FROM binaries b, bin_associations ba, files f, location l, architecture a, component c
156   WHERE ba.suite = %s AND ba.bin = b.id AND b.file = f.id AND
157         f.location = l.id AND l.component = c.id AND b.architecture = a.id
158 UNION
159 SELECT s.id, s.source, 'source', s.version, l.path, f.filename, c.name
160   FROM source s, src_associations sa, files f, location l, component c
161   WHERE sa.suite = %s AND sa.source = s.id AND s.file = f.id AND
162         f.location = l.id AND l.component = c.id;""" % (suite_id, suite_id));
163     ql = q.getresult();
164     d = {};
165     for i in ql:
166         (id, package, architecture, version, path, filename, component) = i;
167         filename = path + filename;
168         if architecture == "source":
169             delete_table = "src_associations";
170             delete_col = "source";
171         else:
172             delete_table = "bin_associations";
173             delete_col = "bin";
174         key = "%s~%s" % (package, architecture);
175         if os.path.exists(filename):
176             if d.has_key(key):
177                 (other_version, other_component, other_id) = d[key];
178                 if apt_pkg.VersionCompare(version, other_version) != 1:
179                     (keep_version, keep_component) = (other_version, other_component)
180                     (delete_id, delete_version, delete_component) = (id, version, component)
181                 else:
182                     (keep_version, keep_component) = (version, component)
183                     (delete_id, delete_version, delete_component) = (other_id, other_version, other_component)
184                     d[key] = (version, component, id);
185                 if not Cnf.Find("Suite::%s::Untouchable" % (suite)):
186                     Logger.log(["deleting", package, architecture, delete_version, delete_component, keep_version, keep_component]);
187                     projectB.query("DELETE FROM %s WHERE suite = %s AND %s = %s" % (delete_table, suite_id, delete_col, delete_id));
188                 else:
189                     Logger.log(["[untouchable]", package, architecture, delete_version, delete_component, keep_version, keep_component]);
190             else:
191                 d[key] = (version, component, id);
192         else:
193             utils.warn("%s is in %s but doesn't appear to exist?" % (filename, suite));
194
195 #########################################################################################
196
197 def main():
198     global Cnf, projectB, Logger;
199     dislocated_files = {};
200
201     apt_pkg.init();
202
203     Cnf = apt_pkg.newConfiguration();
204     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
205
206     Arguments = [('a',"architecture","Jenna::Options::Architecture", "HasArg"),
207                  ('c',"component","Jenna::Options::Component", "HasArg"),
208                  ('d',"debug","Jenna::Options::Debug", "IntVal"),
209                  ('h',"help","Jenna::Options::Help"),
210                  ('s',"suite", "Jenna::Options::Suite", "HasArg"),
211                  ('v',"verbose","Jenna::Options::Verbose"),
212                  ('V',"version","Jenna::Options::Version")];
213
214     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
215
216     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
217     db_access.init(Cnf, projectB);
218     Logger = logging.Logger(Cnf, "jenna");
219
220     if Cnf["Jenna::Options::Suite"] == "":
221         Cnf["Jenna::Options::Suite"] = string.join(Cnf.SubTree("Suite").List());
222     for suite in string.split(Cnf["Jenna::Options::Suite"]):
223         suite = string.lower(suite);
224         if suite == 'stable':
225             dislocated_files = claire.find_dislocated_stable(Cnf, projectB);
226         else:
227             dislocated_files = {};
228         clean_suite(suite);
229         clean_duplicate_packages(suite)
230         components = Cnf["Jenna::Options::Component"];
231         if not Cnf.has_key("Suite::%s::Components" % (suite)):
232             components = "-";
233         if components == "":
234             components = string.join(Cnf.SubTree("Suite::%s::Components" % (suite)).List());
235         for component in string.split(components):
236             component = string.lower(component)
237             architectures = Cnf["Jenna::Options::Architecture"];
238             if architectures == "":
239                 architectures = string.join(Cnf.SubTree("Suite::%s::Architectures" % (suite)).List());
240             for architecture in string.split(architectures):
241                 architecture = string.lower(architecture)
242                 if architecture == "all":
243                     continue
244                 if architecture == "source":
245                     Logger.log(["Processing dists/%s/%s/%s..." % (suite, component, architecture)]);
246                     output = utils.open_file("%s/%s_%s_%s.list" % (Cnf["Dir::ListsDir"], suite, component, architecture), "w")
247                     generate_src_list(suite, component, output, dislocated_files);
248                     output.close();
249                 else:
250                     Logger.log(["Processing dists/%s/%s/binary-%s..." % (suite, component, architecture)]);
251                     output = utils.open_file("%s/%s_%s_binary-%s.list" % (Cnf["Dir::ListsDir"], suite, component, architecture), "w");
252                     generate_bin_list(suite, component, architecture, output, "deb", dislocated_files);
253                     output.close();
254                     if component == "main" and (suite == "unstable" or suite == "testing"): # FIXME: must be a cleaner way to say debian-installer is main only?
255                         Logger.log(["Processing dists/%s/%s/debian-installer/binary-%s..." % (suite,component, architecture)]);
256                         output = utils.open_file("%s/%s_%s_debian-installer_binary-%s.list" % (Cnf["Dir::ListsDir"], suite, component, architecture), "w");
257                         generate_bin_list(suite, component, architecture, output, "udeb", dislocated_files);
258                         output.close();
259     Logger.close();
260
261 #########################################################################################
262
263 if __name__ == '__main__':
264     main()