3 # Handles NEW and BYHAND packages
4 # Copyright (C) 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: lisa,v 1.10 2002-05-10 00:24:23 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 # 23:12|<aj> I will not hush!
25 # 23:12|<aj> Where there is injustice in the world, I shall be there!
26 # 23:13|<aj> I shall not be silenced!
27 # 23:13|<aj> The world shall know!
28 # 23:13|<aj> The world *must* know!
29 # 23:13|<elmo> oh dear, he's gone back to powerpuff girls... ;-)
30 # 23:13|<aj> yay powerpuff girls!!
31 # 23:13|<aj> buttercup's my favourite, who's yours?
32 # 23:14|<aj> you're backing away from the keyboard right now aren't you?
33 # 23:14|<aj> *AREN'T YOU*?!
34 # 23:15|<aj> I will not be treated like this.
35 # 23:15|<aj> I shall have my revenge.
36 # 23:15|<aj> I SHALL!!!
38 ################################################################################
43 # We don't check error codes very thoroughly; the old 'trust jennifer'
44 # chess nut... db_access calls in particular
49 # Handle multiple different section/priorities (?)
50 # hardcoded debianness (debian-installer, source priority etc.) (?)
51 # Slang/ncurses interface (?)
52 # write changed sections/priority back to katie for later processing (?)
54 ################################################################################
56 import copy, errno, os, readline, string, stat, sys, tempfile;
57 import apt_pkg, apt_inst;
58 import db_access, fernanda, katie, logging, utils;
61 lisa_version = "$Revision: 1.10 $";
72 ################################################################################
73 ################################################################################
74 ################################################################################
76 def determine_new (changes, files):
79 # Build up a list of potentially new things
80 for file in files.keys():
82 # Skip byhand elements
83 if f["type"] == "byhand":
86 priority = f["priority"];
87 section = f["section"];
89 if section == "non-US/main":
92 component = f["component"];
96 if not new.has_key(pkg):
98 new[pkg]["priority"] = priority;
99 new[pkg]["section"] = section;
100 new[pkg]["type"] = type;
101 new[pkg]["component"] = component;
102 new[pkg]["files"] = [];
104 old_type = new[pkg]["type"];
106 # source gets trumped by deb or udeb
107 if old_type == "dsc":
108 new[pkg]["priority"] = priority;
109 new[pkg]["section"] = section;
110 new[pkg]["type"] = type;
111 new[pkg]["component"] = component;
112 new[pkg]["files"].append(file);
113 if f.has_key("othercomponents"):
114 new[pkg]["othercomponents"] = f["othercomponents"];
116 for suite in changes["suite"].keys():
117 suite_id = db_access.get_suite_id(suite);
118 for pkg in new.keys():
119 component_id = db_access.get_component_id(new[pkg]["component"]);
120 type_id = db_access.get_override_type_id(new[pkg]["type"]);
121 q = projectB.query("SELECT package FROM override WHERE package = '%s' AND suite = %s AND component = %s AND type = %s" % (pkg, suite_id, component_id, type_id));
124 for file in new[pkg]["files"]:
125 if files[file].has_key("new"):
126 del files[file]["new"];
129 if changes["suite"].has_key("stable"):
130 print "WARNING: overrides will be added for stable!";
131 for pkg in new.keys():
132 if new[pkg].has_key("othercomponents"):
133 print "WARNING: %s already present in %s distribution." % (pkg, new[pkg]["othercomponents"]);
137 ################################################################################
139 # Sort by 'have source', by ctime, by source name, by source version number, by filename
141 def changes_compare_by_time (a, b):
143 a_changes = utils.parse_changes(a, 0)
148 b_changes = utils.parse_changes(b, 0)
152 utils.cc_fix_changes (a_changes);
153 utils.cc_fix_changes (b_changes);
155 # Sort by 'have source'
157 a_has_source = a_changes["architecture"].get("source")
158 b_has_source = b_changes["architecture"].get("source")
159 if a_has_source and not b_has_source:
161 elif b_has_source and not a_has_source:
165 a_ctime = os.stat(a)[stat.ST_CTIME];
166 b_ctime = os.stat(b)[stat.ST_CTIME];
167 q = cmp (a_ctime, b_ctime);
171 # Sort by source name
173 a_source = a_changes.get("source");
174 b_source = b_changes.get("source");
175 q = cmp (a_source, b_source);
179 # Sort by source version
181 a_version = a_changes.get("version");
182 b_version = b_changes.get("version");
183 q = apt_pkg.VersionCompare(a_version, b_version);
187 # Fall back to sort by filename
191 ################################################################################
193 class Section_Completer:
196 q = projectB.query("SELECT section FROM section");
197 for i in q.getresult():
198 self.sections.append(i[0]);
200 def complete(self, text, state):
204 for word in self.sections:
206 self.matches.append(word);
208 return self.matches[state]
212 ############################################################
214 class Priority_Completer:
216 self.priorities = [];
217 q = projectB.query("SELECT priority FROM priority");
218 for i in q.getresult():
219 self.priorities.append(i[0]);
221 def complete(self, text, state):
225 for word in self.priorities:
227 self.matches.append(word);
229 return self.matches[state]
233 ################################################################################
235 def check_valid (new):
236 for pkg in new.keys():
237 section = new[pkg]["section"];
238 priority = new[pkg]["priority"];
239 type = new[pkg]["type"];
240 new[pkg]["section id"] = db_access.get_section_id(section);
241 new[pkg]["priority id"] = db_access.get_priority_id(new[pkg]["priority"]);
243 if (section == "debian-installer" and type != "udeb") or \
244 (section != "debian-installer" and type == "udeb"):
245 new[pkg]["section id"] = -1;
246 if (priority == "source" and type != "dsc") or \
247 (priority != "source" and type == "dsc"):
248 new[pkg]["priority id"] = -1;
250 ################################################################################
252 def print_new (new, indexed, file=sys.stdout):
256 for pkg in new.keys():
258 section = new[pkg]["section"];
259 priority = new[pkg]["priority"];
260 if new[pkg]["section id"] == -1:
261 section = section + "[!]";
263 if new[pkg]["priority id"] == -1:
264 priority = priority + "[!]";
267 line = "(%s): %-20s %-20s %-20s" % (index, pkg, priority, section);
269 line = "%-20s %-20s %-20s" % (pkg, priority, section);
270 line = string.strip(line)+'\n';
274 ################################################################################
278 if f.has_key("dbtype"):
280 elif f["type"] == "orig.tar.gz" or f["type"] == "tar.gz" or f["type"] == "diff.gz" or f["type"] == "dsc":
283 utils.fubar("invalid type (%s) for new. Dazed, confused and sure as heck not continuing." % (type));
285 # Validate the override type
286 type_id = db_access.get_override_type_id(type);
288 utils.fubar("invalid type (%s) for new. Say wha?" % (type));
292 ################################################################################
294 def index_range (index):
298 return "1-%s" % (index);
300 ################################################################################
301 ################################################################################
303 def spawn_editor (new):
304 # Write the current data to a temporary file
305 temp_filename = tempfile.mktemp();
306 fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
308 temp_file = utils.open_file(temp_filename, 'w');
309 print_new (new, 0, temp_file);
311 # Spawn an editor on that file
312 editor = os.environ.get("EDITOR","vi")
313 result = os.system("%s %s" % (editor, temp_filename))
315 utils.fubar ("vi invocation failed for `%s'!" % (temp_filename), result)
316 # Read the (edited) data back in
317 file = utils.open_file(temp_filename);
318 for line in file.readlines():
319 line = string.strip(line[:-1]);
322 s = string.split(line);
323 # Pad the list if necessary
324 s[len(s):3] = [None] * (3-len(s));
325 (pkg, priority, section) = s[:3];
326 if not new.has_key(pkg):
327 utils.warn("Ignoring unknown package '%s'" % (pkg));
329 # Strip off any invalid markers, print_new will readd them.
330 if section[-3:] == "[!]":
331 section = section[:-3];
332 if priority[-3:] == "[!]":
333 priority = priority[:-3];
334 for file in new[pkg]["files"]:
335 Katie.pkg.files[file]["section"] = section;
336 Katie.pkg.files[file]["priority"] = priority;
337 new[pkg]["section"] = section;
338 new[pkg]["priority"] = priority;
339 os.unlink(temp_filename);
341 ################################################################################
343 def edit_index (new, index):
344 priority = new[index]["priority"]
345 section = new[index]["section"]
346 type = new[index]["type"];
349 print string.join([index, priority, section], '\t');
353 prompt = "[B]oth, Priority, Section, Done ? ";
355 prompt = "[S]ection, Done ? ";
356 edit_priority = edit_section = 0;
358 while string.find(prompt, answer) == -1:
359 answer = utils.our_raw_input(prompt);
360 m = katie.re_default_answer.match(prompt)
363 answer = string.upper(answer[:1])
370 edit_priority = edit_section = 1;
376 readline.set_completer(Priorities.complete);
378 while not got_priority:
379 new_priority = string.strip(utils.our_raw_input("New priority: "));
380 if Priorities.priorities.count(new_priority) == 0:
381 print "E: '%s' is not a valid priority, try again." % (new_priority);
384 priority = new_priority;
388 readline.set_completer(Sections.complete);
390 while not got_section:
391 new_section = string.strip(utils.our_raw_input("New section: "));
392 if Sections.sections.count(new_section) == 0:
393 print "E: '%s' is not a valid section, try again." % (new_section);
396 section = new_section;
398 # Reset the readline completer
399 readline.set_completer(None);
401 for file in new[index]["files"]:
402 Katie.pkg.files[file]["section"] = section;
403 Katie.pkg.files[file]["priority"] = priority;
404 new[index]["priority"] = priority;
405 new[index]["section"] = section;
408 ################################################################################
410 def edit_overrides (new):
419 new_index[index] = i;
421 prompt = "(%s) edit override <n>, Editor, Done ? " % (index_range(index));
424 while not got_answer:
425 answer = utils.our_raw_input(prompt);
426 answer = string.upper(answer[:1]);
427 if answer == "E" or answer == "D":
429 elif katie.re_isanum.match (answer):
430 answer = int(answer);
431 if (answer < 1) or (answer > index):
432 print "%s is not a valid index (%s). Please retry." % (index_range(index), answer);
441 edit_index (new, new_index[answer]);
445 ################################################################################
449 less_fd = os.popen("less -", 'w', 0);
450 stdout_fd = sys.stdout;
452 sys.stdout = less_fd;
453 fernanda.display_changes(Katie.pkg.changes_file);
454 files = Katie.pkg.files;
455 for file in files.keys():
456 if files[file].has_key("new"):
457 type = files[file]["type"];
459 fernanda.check_deb(file);
461 fernanda.check_dsc(file);
463 sys.stdout = stdout_fd;
465 if errno.errorcode[e.errno] == 'EPIPE':
466 utils.warn("[fernanda] Caught EPIPE; skipping.");
470 except KeyboardInterrupt:
471 utils.warn("[fernanda] Caught C-c; skipping.");
474 ################################################################################
476 ## FIXME: horribly Debian specific
478 def do_bxa_notification():
479 files = Katie.pkg.files;
481 for file in files.keys():
482 if files[file]["type"] == "deb":
483 control = apt_pkg.ParseSection(apt_inst.debExtractControl(utils.open_file(file)));
484 summary = summary + "\n";
485 summary = summary + "Package: %s\n" % (control.Find("Package"));
486 summary = summary + "Description: %s\n" % (control.Find("Description"));
487 Katie.Subst["__BINARY_DESCRIPTIONS__"] = summary;
488 bxa_mail = utils.TemplateSubst(Katie.Subst,Cnf["Dir::Templates"]+"/lisa.bxa_notification");
489 utils.send_mail(bxa_mail,"");
491 ################################################################################
493 def add_overrides (new):
494 changes = Katie.pkg.changes;
495 files = Katie.pkg.files;
497 projectB.query("BEGIN WORK");
498 for suite in changes["suite"].keys():
499 suite_id = db_access.get_suite_id(suite);
500 for pkg in new.keys():
501 component_id = db_access.get_component_id(new[pkg]["component"]);
502 type_id = db_access.get_override_type_id(new[pkg]["type"]);
503 priority_id = new[pkg]["priority id"];
504 section_id = new[pkg]["section id"];
505 projectB.query("INSERT INTO override (suite, component, type, package, priority, section) VALUES (%s, %s, %s, '%s', %s, %s)" % (suite_id, component_id, type_id, pkg, priority_id, section_id));
506 for file in new[pkg]["files"]:
507 if files[file].has_key("new"):
508 del files[file]["new"];
511 projectB.query("COMMIT WORK");
513 if Cnf.FindB("Dinstall::BXANotify"):
514 do_bxa_notification();
516 ################################################################################
520 files = Katie.pkg.files;
521 changes = Katie.pkg.changes;
523 # Make a copy of distribution we can happily trample on
524 changes["suite"] = copy.copy(changes["distribution"]);
526 # Fix up the list of target suites
527 for suite in changes["suite"].keys():
528 override = Cnf.Find("Suite::%s::OverrideSuite" % (suite));
530 del changes["suite"][suite];
531 changes["suite"][override] = 1;
533 for suite in changes["suite"].keys():
534 suite_id = db_access.get_suite_id(suite);
536 utils.fubar("%s has invalid suite '%s' (possibly overriden). say wha?" % (changes, suite));
538 # The main NEW processing loop
541 # Find out what's new
542 new = determine_new(changes, files);
548 if Options["No-Action"] or Options["Automatic"]:
551 broken = print_new(new, 0);
554 prompt = "Add overrides, ";
556 print "W: [!] marked entries must be fixed before package can be processed.";
559 prompt = prompt + "Edit overrides, Check, Manual reject, [S]kip, Quit ?";
561 while string.find(prompt, answer) == -1:
562 answer = utils.our_raw_input(prompt);
563 m = katie.re_default_answer.search(prompt);
566 answer = string.upper(answer[:1])
569 done = add_overrides (new);
573 new = edit_overrides (new);
575 aborted = Katie.do_reject(1, Options["Manual-Reject"]);
577 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
584 ################################################################################
585 ################################################################################
586 ################################################################################
588 def usage (exit_code=0):
589 print """Usage: lisa [OPTION]... [CHANGES]...
590 -a, --automatic automatic run
591 -h, --help show this help and exit.
592 -m, --manual-reject=MSG manual reject with `msg'
593 -n, --no-action don't do anything
594 -s, --sort=TYPE sort type ('time' or 'normal')
595 -V, --version display the version number and exit"""
598 ################################################################################
601 global Cnf, Options, Logger, Katie, projectB, Sections, Priorities;
603 Cnf = utils.get_conf();
605 Arguments = [('a',"automatic","Lisa::Options::Automatic"),
606 ('h',"help","Lisa::Options::Help"),
607 ('m',"manual-reject","Lisa::Options::Manual-Reject", "HasArg"),
608 ('n',"no-action","Lisa::Options::No-Action"),
609 ('s',"sort","Lisa::Options::Sort","HasArg"),
610 ('V',"version","Lisa::Options::Version")];
612 for i in ["automatic", "help", "manual-reject", "no-action", "version"]:
613 if not Cnf.has_key("Lisa::Options::%s" % (i)):
614 Cnf["Lisa::Options::%s" % (i)] = "";
615 if not Cnf.has_key("Lisa::Options::Sort"):
616 Cnf["Lisa::Options::Sort"] = "time";
618 changes_files = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
619 Options = Cnf.SubTree("Lisa::Options")
624 if Options["Version"]:
625 print "lisa %s" % (lisa_version);
628 if Options["Sort"] != "time" and Options["Sort"] != "normal":
629 utils.fubar("Unrecognised sort type '%s'. (Recognised sort types are: time and normal)" % (Options["Sort"]));
631 Katie = katie.Katie(Cnf);
633 if not Options["No-Action"]:
634 Logger = Katie.Logger = logging.Logger(Cnf, "lisa");
636 projectB = Katie.projectB;
638 Sections = Section_Completer();
639 Priorities = Priority_Completer();
640 readline.parse_and_bind("tab: complete");
642 return changes_files;
644 ################################################################################
649 files = Katie.pkg.files;
653 for file in files.keys():
654 if files[file]["type"] == "byhand":
655 if os.path.exists(file):
656 print "W: %s still present; please process byhand components and try again." % (file);
662 if Options["No-Action"]:
665 if Options["Automatic"] and not Options["No-Action"]:
667 prompt = "[A]ccept, Manual reject, Skip, Quit ?";
669 prompt = "Manual reject, [S]kip, Quit ?";
671 while string.find(prompt, answer) == -1:
672 answer = utils.our_raw_input(prompt);
673 m = katie.re_default_answer.search(prompt);
676 answer = string.upper(answer[:1]);
683 Katie.do_reject(1, Options["Manual-Reject"]);
684 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
691 ################################################################################
695 if not Options["No-Action"]:
696 (summary, short_summary) = Katie.build_summaries();
697 Katie.accept(summary, short_summary);
698 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
700 def check_status(files):
702 for file in files.keys():
703 if files[file]["type"] == "byhand":
705 elif files[file].has_key("new"):
707 return (new, byhand);
709 def do_pkg(changes_file):
710 Katie.pkg.changes_file = changes_file;
713 Katie.update_subst();
714 files = Katie.pkg.files;
716 (new, byhand) = check_status(files);
722 (new, byhand) = check_status(files);
724 if not new and not byhand:
727 ################################################################################
730 accept_count = Katie.accept_count;
731 accept_bytes = Katie.accept_bytes;
737 sys.stderr.write("Accepted %d package %s, %s.\n" % (accept_count, sets, utils.size_type(int(accept_bytes))));
738 Logger.log(["total",accept_count,accept_bytes]);
740 if not Options["No-Action"]:
743 ################################################################################
746 changes_files = init();
748 # Sort the changes files
749 if Options["Sort"] == "time":
750 changes_files.sort(changes_compare_by_time);
752 changes_files.sort(utils.changes_compare);
754 # Kill me now? **FIXME**
755 Cnf["Dinstall::Options::No-Mail"] = "";
756 bcc = "X-Katie: %s" % (lisa_version);
757 if Cnf.has_key("Dinstall::Bcc"):
758 Katie.Subst["__BCC__"] = bcc + "\nBcc: %s" % (Cnf["Dinstall::Bcc"]);
760 Katie.Subst["__BCC__"] = bcc;
762 for changes_file in changes_files:
763 changes_file = utils.validate_changes_file_arg(changes_file, 0);
766 print "\n" + changes_file;
767 do_pkg (changes_file);
771 ################################################################################
773 if __name__ == '__main__':