]> git.decadent.org.uk Git - dak.git/blob - amber
* katie.py (source_exists): expand the list of distributionsthe source may exist...
[dak.git] / amber
1 #!/usr/bin/env python
2
3 # Wrapper for Debian Security team
4 # Copyright (C) 2002, 2003  James Troup <james@nocrew.org>
5 # $Id: amber,v 1.8 2003-03-14 19:06:02 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, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # 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
20 # USA
21
22 ################################################################################
23
24 # <aj> neuro: <usual question>?
25 # <neuro> aj: PPG: the movie!  july 3!
26 # <aj> _PHWOAR_!!!!!
27 # <aj> (you think you can distract me, and you're right)
28 # <aj> urls?!
29 # <aj> promo videos?!
30 # <aj> where, where!?
31
32 ################################################################################
33
34 import commands, os, pwd, re, sys, time;
35 import apt_pkg;
36 import katie, utils;
37
38 ################################################################################
39
40 Cnf = None;
41 Options = None;
42 Katie = None;
43
44 re_taint_free = re.compile(r"^['/;\-\+\.\s\w]+$");
45
46 ################################################################################
47
48 def usage (exit_code=0):
49     print """Usage: amber ADV_NUMBER CHANGES_FILE[...]
50 Install CHANGES_FILE(s) as security advisory ADV_NUMBER
51
52   -h, --help                 show this help and exit
53   -n, --no-action            don't do anything
54
55 """
56     sys.exit(exit_code)
57
58 ################################################################################
59
60 def do_upload(changes_files):
61     file_list = "";
62     suites = {};
63     component_mapping = {};
64     for component in Cnf.SubTree("Amber::ComponentMappings").List():
65         component_mapping[component] = Cnf["Amber::ComponentMappings::%s" % (component)];
66     uploads = {}; # uploads[uri] = file_list;
67     package_list = {} # package_list[source_name][version];
68     for changes_file in changes_files:
69         changes_file = utils.validate_changes_file_arg(changes_file);
70         # Reset variables
71         components = {};
72         upload_uris = {};
73         file_list = [];
74         Katie.init_vars();
75         # Parse the .katie file for the .changes file
76         Katie.pkg.changes_file = changes_file;
77         Katie.update_vars();
78         files = Katie.pkg.files;
79         changes = Katie.pkg.changes;
80         dsc = Katie.pkg.dsc;
81         # Build the file list for this .changes file
82         for file in files.keys():
83             poolname = os.path.join(Cnf["Dir::Root"], Cnf["Dir::PoolRoot"],
84                                     utils.poolify(changes["source"], files[file]["component"]),
85                                     file);
86             file_list.append(poolname);
87             orig_component = files[file].get("original component", files[file]["component"]);
88             components[orig_component] = "";
89         file_list.append(changes_file);
90         # Determine the upload uri for this .changes file
91         for component in components.keys():
92             upload_uri = component_mapping.get(component);
93             if upload_uri:
94                 upload_uris[upload_uri] = "";
95         num_upload_uris = len(upload_uris.keys());
96         if num_upload_uris == 0:
97             utils.fubar("%s: No valid upload URI found from components (%s)."
98                         % (changes_file, ", ".join(components.keys())));
99         elif num_upload_uris > 1:
100             utils.fubar("%s: more than one upload URI (%s) from components (%s)."
101                         % (changes_file, ", ".join(upload_uris.keys()),
102                            ", ".join(components.keys())));
103         upload_uri = upload_uris.keys()[0];
104         # Update the file list for the upload uri
105         if not uploads.has_key(upload_uri):
106             uploads[upload_uri] = [];
107         uploads[upload_uri].extend(file_list);
108         # Remember the suites and source name/version
109         for suite in changes["distribution"].keys():
110             suites[suite] = "";
111         # Remember the source name and version
112         if changes["architecture"].has_key("source") and \
113            changes["distribution"].has_key("testing"):
114             if not package_list.has_key(dsc["source"]):
115                 package_list[dsc["source"]] = {};
116             package_list[dsc["source"]][dsc["version"]] = "";
117
118     if len(suites.keys()) == 1 and suites.has_key("oldstable"):
119         print "Advisory only for 'oldstable'; not uploading elsewhere.";
120         return;
121
122     if not Options["No-Action"]:
123         answer = yes_no("Upload to files to main archive (Y/n)?");
124         if answer != "y":
125             return;
126
127     for uri in uploads.keys():
128         (host, path) = uri.split(":");
129         file_list = " ".join(uploads[uri]);
130         print "Uploading files to %s..." % (host);
131         spawn("lftp -c 'open %s; cd %s; put %s'" % (host, path, file_list));
132
133     if not Options["No-Action"]:
134         filename = "%s/testing-processed" % (Cnf["Dir::Log"]);
135         file = utils.open_file(filename, 'a');
136         for source in package_list.keys():
137             for version in package_list[source].keys():
138                 file.write(" ".join([source, version])+'\n');
139         file.close();
140
141 ######################################################################
142 # This function was originally written by aj and NIHishly merged into
143 # amber by me.
144
145 def make_advisory(advisory_nr, changes_files):
146     adv_packages = [];
147     updated_pkgs = {};  # updated_pkgs[distro][arch][file] = {path,md5,size}
148
149     for arg in changes_files:
150         arg = utils.validate_changes_file_arg(arg);
151         Katie.pkg.changes_file = arg;
152         Katie.init_vars();
153         Katie.update_vars();
154
155         src = Katie.pkg.changes["source"];
156         if src not in adv_packages:
157             adv_packages += [src];
158
159         suites = Katie.pkg.changes["distribution"].keys();
160         for suite in suites:
161             if not updated_pkgs.has_key(suite):
162                 updated_pkgs[suite] = {};
163
164         files = Katie.pkg.files;
165         for file in files.keys():
166             arch = files[file]["architecture"];
167             md5 = files[file]["md5sum"];
168             size = files[file]["size"];
169             poolname = Cnf["Dir::PoolRoot"] + \
170                 utils.poolify(src, files[file]["component"]);
171             if arch == "source" and file.endswith(".dsc"):
172                 dscpoolname = poolname;
173             for suite in suites:
174                 if not updated_pkgs[suite].has_key(arch):
175                     updated_pkgs[suite][arch] = {}
176                 updated_pkgs[suite][arch][file] = {
177                     "md5": md5, "size": size,
178                     "poolname": poolname };
179
180         dsc_files = Katie.pkg.dsc_files;
181         for file in dsc_files.keys():
182             arch = "source"
183             if not dsc_files[file].has_key("files id"):
184                 continue;
185
186             # otherwise, it's already in the pool and needs to be
187             # listed specially
188             md5 = dsc_files[file]["md5sum"];
189             size = dsc_files[file]["size"];
190             for suite in suites:
191                 if not updated_pkgs[suite].has_key(arch):
192                     updated_pkgs[suite][arch] = {};
193                 updated_pkgs[suite][arch][file] = {
194                     "md5": md5, "size": size,
195                     "poolname": dscpoolname };
196
197     if os.environ.has_key("SUDO_UID"):
198         whoami = long(os.environ["SUDO_UID"]);
199     else:
200         whoami = os.getuid();
201     whoamifull = pwd.getpwuid(whoami);
202     username = whoamifull[4].split(",")[0];
203
204     Subst = {
205         "__ADVISORY__": advisory_nr,
206         "__WHOAMI__": username,
207         "__DATE__": time.strftime("%B %d, %Y", time.gmtime(time.time())),
208         "__PACKAGE__": ", ".join(adv_packages),
209         "__KATIE_ADDRESS__": Cnf["Dinstall::MyEmailAddress"]
210         };
211
212     if Cnf.has_key("Dinstall::Bcc"):
213         Subst["__BCC__"] = "Bcc: %s" % (Cnf["Dinstall::Bcc"]);
214
215     adv = "";
216     archive = Cnf["Archive::%s::PrimaryMirror" % (utils.where_am_i())];
217     for suite in updated_pkgs.keys():
218         suite_header = "%s %s (%s)" % (Cnf["Dinstall::MyDistribution"],
219                                        Cnf["Suite::%s::Version" % suite], suite);
220         adv += "%s\n%s\n\n" % (suite_header, "-"*len(suite_header));
221
222         arches = Cnf.ValueList("Suite::%s::Architectures" % suite);
223         if "source" in arches:
224             arches.remove("source");
225         if "all" in arches:
226             arches.remove("all");
227         arches.sort();
228
229         adv += "  %s was released for %s.\n\n" % (
230                 suite.capitalize(), utils.join_with_commas_and(arches));
231
232         for a in ["source", "all"] + arches:
233             if not updated_pkgs[suite].has_key(a):
234                 continue;
235
236             if a == "source":
237                 adv += "  Source archives:\n\n";
238             elif a == "all":
239                 adv += "  Architecture independent packages:\n\n";
240             else:
241                 adv += "  %s architecture (%s)\n\n" % (a,
242                         Cnf["Architectures::%s" % a]);
243
244             for file in updated_pkgs[suite][a].keys():
245                 adv += "    http://%s/%s%s\n" % (
246                                 archive, updated_pkgs[suite][a][file]["poolname"], file);
247                 adv += "      Size/MD5 checksum: %8s %s\n" % (
248                         updated_pkgs[suite][a][file]["size"],
249                         updated_pkgs[suite][a][file]["md5"]);
250             adv += "\n";
251     adv = adv.rstrip();
252
253     Subst["__ADVISORY_TEXT__"] = adv;
254
255     adv = utils.TemplateSubst(Subst, Cnf["Dir::Templates"]+"/amber.advisory");
256     if not Options["No-Action"]:
257         utils.send_mail (adv);
258     else:
259         print "[<Would send template advisory mail>]";
260
261 ######################################################################
262
263 def init():
264     global Cnf, Katie, Options;
265
266     apt_pkg.init();
267     Cnf = utils.get_conf();
268
269     Arguments = [('h', "help", "Amber::Options::Help"),
270                  ('n', "no-action", "Amber::Options::No-Action")];
271
272     for i in [ "help", "no-action" ]:
273         Cnf["Amber::Options::%s" % (i)] = "";
274
275     arguments = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
276     Options = Cnf.SubTree("Amber::Options")
277     Katie = katie.Katie(Cnf);
278
279     if Options["Help"]:
280         usage(0);
281
282     if not arguments:
283         usage(1);
284
285     advisory_number = arguments[0];
286     changes_files = arguments[1:];
287     if advisory_number.endswith(".changes"):
288         utils.warn("first argument must be the advisory number.");
289         usage(1);
290     for file in changes_files:
291         file = utils.validate_changes_file_arg(file);
292     return (advisory_number, changes_files);
293
294 ######################################################################
295
296 def yes_no(prompt):
297     while 1:
298         answer = utils.our_raw_input(prompt+" ").lower();
299         if answer == "y" or answer == "n":
300             break;
301         else:
302             print "Invalid answer; please try again.";
303     return answer;
304
305 ######################################################################
306
307 def spawn(command):
308     if not re_taint_free.match(command):
309         utils.fubar("Invalid character in \"%s\"." % (command));
310
311     if Options["No-Action"]:
312         print "[%s]" % (command);
313     else:
314         (result, output) = commands.getstatusoutput(command);
315         if (result != 0):
316             utils.fubar("Invocation of '%s' failed:\n%s\n" % (command, output), result);
317
318 ######################################################################
319
320
321 def main():
322     (advisory_number, changes_files) = init();
323
324     if not Options["No-Action"]:
325         print "About to install the following files: "
326         for file in changes_files:
327             print "  %s" % (file);
328         answer = yes_no("Continue (Y/n)?");
329         if answer == "n":
330             sys.exit(0);
331
332     os.chdir(Cnf["Dir::Queue::Accepted"]);
333     print "Installing packages into the archive...";
334     spawn("%s/kelly -pa %s" % (Cnf["Dir::Katie"], " ".join(changes_files)));
335     os.chdir(Cnf["Dir::Katie"]);
336     print "Updating file lists for apt-ftparchive...";
337     spawn("./jenna");
338     print "Updating Packages and Sources files...";
339     spawn("apt-ftparchive generate %s" % (utils.which_apt_conf_file()));
340     print "Updating Release files...";
341     spawn("./ziyi");
342
343     if not Options["No-Action"]:
344         os.chdir(Cnf["Dir::Queue::Done"]);
345     else:
346         os.chdir(Cnf["Dir::Queue::Accepted"]);
347     print "Generating template advisory...";
348     make_advisory(advisory_number, changes_files);
349
350     do_upload(changes_files);
351
352 ################################################################################
353
354 if __name__ == '__main__':
355     main();
356
357 ################################################################################