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