3 # Handles NEW and BYHAND packages
4 # Copyright (C) 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: lisa,v 1.13 2002-05-19 19:55:56 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.13 $";
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 note', 'have source', by ctime, by source name, by
140 # source version number, and finally by filename
142 def changes_compare (a, b):
144 Katie.pkg.changes_file = a;
147 a_changes = copy.copy(Katie.pkg.changes);
152 Katie.pkg.changes_file = b;
155 b_changes = copy.copy(Katie.pkg.changes);
159 # Sort by 'have note';
160 a_has_note = a_changes.get("lisa note");
161 b_has_note = b_changes.get("lisa note");
162 if a_has_note and not b_has_note:
164 elif b_has_note and not a_has_note:
167 # Sort by 'have source'
168 a_has_source = a_changes["architecture"].get("source");
169 b_has_source = b_changes["architecture"].get("source");
170 if a_has_source and not b_has_source:
172 elif b_has_source and not a_has_source:
175 # Sort by ctime-per-source
176 a_source = a_changes.get("source");
177 b_source = b_changes.get("source");
178 if a_source != b_source:
179 a_ctime = os.stat(a)[stat.ST_CTIME];
180 b_ctime = os.stat(b)[stat.ST_CTIME];
181 q = cmp (a_ctime, b_ctime);
185 # Sort by source name
186 q = cmp (a_source, b_source);
190 # Sort by source version
191 a_version = a_changes.get("version");
192 b_version = b_changes.get("version");
193 q = apt_pkg.VersionCompare(a_version, b_version);
197 # Fall back to sort by filename
200 ################################################################################
202 class Section_Completer:
205 q = projectB.query("SELECT section FROM section");
206 for i in q.getresult():
207 self.sections.append(i[0]);
209 def complete(self, text, state):
213 for word in self.sections:
215 self.matches.append(word);
217 return self.matches[state]
221 ############################################################
223 class Priority_Completer:
225 self.priorities = [];
226 q = projectB.query("SELECT priority FROM priority");
227 for i in q.getresult():
228 self.priorities.append(i[0]);
230 def complete(self, text, state):
234 for word in self.priorities:
236 self.matches.append(word);
238 return self.matches[state]
242 ################################################################################
244 def check_valid (new):
245 for pkg in new.keys():
246 section = new[pkg]["section"];
247 priority = new[pkg]["priority"];
248 type = new[pkg]["type"];
249 new[pkg]["section id"] = db_access.get_section_id(section);
250 new[pkg]["priority id"] = db_access.get_priority_id(new[pkg]["priority"]);
252 if (section == "debian-installer" and type != "udeb") or \
253 (section != "debian-installer" and type == "udeb"):
254 new[pkg]["section id"] = -1;
255 if (priority == "source" and type != "dsc") or \
256 (priority != "source" and type == "dsc"):
257 new[pkg]["priority id"] = -1;
259 ################################################################################
261 def print_new (new, indexed, file=sys.stdout):
265 for pkg in new.keys():
267 section = new[pkg]["section"];
268 priority = new[pkg]["priority"];
269 if new[pkg]["section id"] == -1:
270 section = section + "[!]";
272 if new[pkg]["priority id"] == -1:
273 priority = priority + "[!]";
276 line = "(%s): %-20s %-20s %-20s" % (index, pkg, priority, section);
278 line = "%-20s %-20s %-20s" % (pkg, priority, section);
279 line = string.strip(line)+'\n';
281 note = Katie.pkg.changes.get("lisa note");
288 ################################################################################
292 if f.has_key("dbtype"):
294 elif f["type"] == "orig.tar.gz" or f["type"] == "tar.gz" or f["type"] == "diff.gz" or f["type"] == "dsc":
297 utils.fubar("invalid type (%s) for new. Dazed, confused and sure as heck not continuing." % (type));
299 # Validate the override type
300 type_id = db_access.get_override_type_id(type);
302 utils.fubar("invalid type (%s) for new. Say wha?" % (type));
306 ################################################################################
308 def index_range (index):
312 return "1-%s" % (index);
314 ################################################################################
315 ################################################################################
318 # Write the current data to a temporary file
319 temp_filename = tempfile.mktemp();
320 fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
322 temp_file = utils.open_file(temp_filename, 'w');
323 print_new (new, 0, temp_file);
325 # Spawn an editor on that file
326 editor = os.environ.get("EDITOR","vi")
327 result = os.system("%s %s" % (editor, temp_filename))
329 utils.fubar ("%s invocation failed for %s." % (editor, temp_filename), result)
330 # Read the edited data back in
331 temp_file = utils.open_file(temp_filename);
332 lines = temp_file.readlines();
334 os.unlink(temp_filename);
337 line = string.strip(line[:-1]);
340 s = string.split(line);
341 # Pad the list if necessary
342 s[len(s):3] = [None] * (3-len(s));
343 (pkg, priority, section) = s[:3];
344 if not new.has_key(pkg):
345 utils.warn("Ignoring unknown package '%s'" % (pkg));
347 # Strip off any invalid markers, print_new will readd them.
348 if section[-3:] == "[!]":
349 section = section[:-3];
350 if priority[-3:] == "[!]":
351 priority = priority[:-3];
352 for file in new[pkg]["files"]:
353 Katie.pkg.files[file]["section"] = section;
354 Katie.pkg.files[file]["priority"] = priority;
355 new[pkg]["section"] = section;
356 new[pkg]["priority"] = priority;
358 ################################################################################
360 def edit_index (new, index):
361 priority = new[index]["priority"]
362 section = new[index]["section"]
363 type = new[index]["type"];
366 print string.join([index, priority, section], '\t');
370 prompt = "[B]oth, Priority, Section, Done ? ";
372 prompt = "[S]ection, Done ? ";
373 edit_priority = edit_section = 0;
375 while string.find(prompt, answer) == -1:
376 answer = utils.our_raw_input(prompt);
377 m = katie.re_default_answer.match(prompt)
380 answer = string.upper(answer[:1])
387 edit_priority = edit_section = 1;
393 readline.set_completer(Priorities.complete);
395 while not got_priority:
396 new_priority = string.strip(utils.our_raw_input("New priority: "));
397 if Priorities.priorities.count(new_priority) == 0:
398 print "E: '%s' is not a valid priority, try again." % (new_priority);
401 priority = new_priority;
405 readline.set_completer(Sections.complete);
407 while not got_section:
408 new_section = string.strip(utils.our_raw_input("New section: "));
409 if Sections.sections.count(new_section) == 0:
410 print "E: '%s' is not a valid section, try again." % (new_section);
413 section = new_section;
415 # Reset the readline completer
416 readline.set_completer(None);
418 for file in new[index]["files"]:
419 Katie.pkg.files[file]["section"] = section;
420 Katie.pkg.files[file]["priority"] = priority;
421 new[index]["priority"] = priority;
422 new[index]["section"] = section;
425 ################################################################################
427 def edit_overrides (new):
436 new_index[index] = i;
438 prompt = "(%s) edit override <n>, Editor, Done ? " % (index_range(index));
441 while not got_answer:
442 answer = utils.our_raw_input(prompt);
443 answer = string.upper(answer[:1]);
444 if answer == "E" or answer == "D":
446 elif katie.re_isanum.match (answer):
447 answer = int(answer);
448 if (answer < 1) or (answer > index):
449 print "%s is not a valid index (%s). Please retry." % (index_range(index), answer);
458 edit_index (new, new_index[answer]);
462 ################################################################################
465 # Write the current data to a temporary file
466 temp_filename = tempfile.mktemp();
467 fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
469 temp_file = utils.open_file(temp_filename, 'w');
470 temp_file.write(note);
472 editor = os.environ.get("EDITOR","vi")
475 os.system("%s %s" % (editor, temp_filename))
476 temp_file = utils.open_file(temp_filename);
477 note = string.rstrip(temp_file.read());
480 print utils.prefix_multi_line_string(note," ");
481 prompt = "[D]one, Edit, Abandon, Quit ?"
483 while string.find(prompt, answer) == -1:
484 answer = utils.our_raw_input(prompt);
485 m = katie.re_default_answer.search(prompt);
488 answer = string.upper(answer[:1]);
489 os.unlink(temp_filename);
494 Katie.pkg.changes["lisa note"] = note;
495 Katie.dump_vars(Cnf["Dir::Queue::New"]);
497 ################################################################################
501 less_fd = os.popen("less -", 'w', 0);
502 stdout_fd = sys.stdout;
504 sys.stdout = less_fd;
505 fernanda.display_changes(Katie.pkg.changes_file);
506 files = Katie.pkg.files;
507 for file in files.keys():
508 if files[file].has_key("new"):
509 type = files[file]["type"];
511 fernanda.check_deb(file);
513 fernanda.check_dsc(file);
515 sys.stdout = stdout_fd;
517 if errno.errorcode[e.errno] == 'EPIPE':
518 utils.warn("[fernanda] Caught EPIPE; skipping.");
522 except KeyboardInterrupt:
523 utils.warn("[fernanda] Caught C-c; skipping.");
526 ################################################################################
528 ## FIXME: horribly Debian specific
530 def do_bxa_notification():
531 files = Katie.pkg.files;
533 for file in files.keys():
534 if files[file]["type"] == "deb":
535 control = apt_pkg.ParseSection(apt_inst.debExtractControl(utils.open_file(file)));
536 summary = summary + "\n";
537 summary = summary + "Package: %s\n" % (control.Find("Package"));
538 summary = summary + "Description: %s\n" % (control.Find("Description"));
539 Katie.Subst["__BINARY_DESCRIPTIONS__"] = summary;
540 bxa_mail = utils.TemplateSubst(Katie.Subst,Cnf["Dir::Templates"]+"/lisa.bxa_notification");
541 utils.send_mail(bxa_mail,"");
543 ################################################################################
545 def add_overrides (new):
546 changes = Katie.pkg.changes;
547 files = Katie.pkg.files;
549 projectB.query("BEGIN WORK");
550 for suite in changes["suite"].keys():
551 suite_id = db_access.get_suite_id(suite);
552 for pkg in new.keys():
553 component_id = db_access.get_component_id(new[pkg]["component"]);
554 type_id = db_access.get_override_type_id(new[pkg]["type"]);
555 priority_id = new[pkg]["priority id"];
556 section_id = new[pkg]["section id"];
557 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));
558 for file in new[pkg]["files"]:
559 if files[file].has_key("new"):
560 del files[file]["new"];
563 projectB.query("COMMIT WORK");
565 if Cnf.FindB("Dinstall::BXANotify"):
566 do_bxa_notification();
568 ################################################################################
572 files = Katie.pkg.files;
573 changes = Katie.pkg.changes;
575 # Make a copy of distribution we can happily trample on
576 changes["suite"] = copy.copy(changes["distribution"]);
578 # Fix up the list of target suites
579 for suite in changes["suite"].keys():
580 override = Cnf.Find("Suite::%s::OverrideSuite" % (suite));
582 del changes["suite"][suite];
583 changes["suite"][override] = 1;
585 for suite in changes["suite"].keys():
586 suite_id = db_access.get_suite_id(suite);
588 utils.fubar("%s has invalid suite '%s' (possibly overriden). say wha?" % (changes, suite));
590 # The main NEW processing loop
593 # Find out what's new
594 new = determine_new(changes, files);
600 if Options["No-Action"] or Options["Automatic"]:
603 (broken, note) = print_new(new, 0);
606 if not broken and not note:
607 prompt = "Add overrides, ";
609 print "W: [!] marked entries must be fixed before package can be processed.";
611 print "W: note must be removed before package can be processed.";
612 prompt = prompt + "Remove note, ";
614 prompt = prompt + "Edit overrides, Check, Manual reject, Note edit, [S]kip, Quit ?";
616 while string.find(prompt, answer) == -1:
617 answer = utils.our_raw_input(prompt);
618 m = katie.re_default_answer.search(prompt);
621 answer = string.upper(answer[:1])
624 done = add_overrides (new);
628 new = edit_overrides (new);
630 aborted = Katie.do_reject(1, Options["Manual-Reject"]);
632 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
635 edit_note(changes.get("lisa note", ""));
637 confirm = string.lower(utils.our_raw_input("Really clear note (y/N)? "));
639 del changes["lisa note"];
645 ################################################################################
646 ################################################################################
647 ################################################################################
649 def usage (exit_code=0):
650 print """Usage: lisa [OPTION]... [CHANGES]...
651 -a, --automatic automatic run
652 -h, --help show this help and exit.
653 -m, --manual-reject=MSG manual reject with `msg'
654 -n, --no-action don't do anything
655 -V, --version display the version number and exit"""
658 ################################################################################
661 global Cnf, Options, Logger, Katie, projectB, Sections, Priorities;
663 Cnf = utils.get_conf();
665 Arguments = [('a',"automatic","Lisa::Options::Automatic"),
666 ('h',"help","Lisa::Options::Help"),
667 ('m',"manual-reject","Lisa::Options::Manual-Reject", "HasArg"),
668 ('n',"no-action","Lisa::Options::No-Action"),
669 ('V',"version","Lisa::Options::Version")];
671 for i in ["automatic", "help", "manual-reject", "no-action", "version"]:
672 if not Cnf.has_key("Lisa::Options::%s" % (i)):
673 Cnf["Lisa::Options::%s" % (i)] = "";
675 changes_files = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
676 Options = Cnf.SubTree("Lisa::Options")
681 if Options["Version"]:
682 print "lisa %s" % (lisa_version);
685 Katie = katie.Katie(Cnf);
687 if not Options["No-Action"]:
688 Logger = Katie.Logger = logging.Logger(Cnf, "lisa");
690 projectB = Katie.projectB;
692 Sections = Section_Completer();
693 Priorities = Priority_Completer();
694 readline.parse_and_bind("tab: complete");
696 return changes_files;
698 ################################################################################
703 files = Katie.pkg.files;
707 for file in files.keys():
708 if files[file]["type"] == "byhand":
709 if os.path.exists(file):
710 print "W: %s still present; please process byhand components and try again." % (file);
716 if Options["No-Action"]:
719 if Options["Automatic"] and not Options["No-Action"]:
721 prompt = "[A]ccept, Manual reject, Skip, Quit ?";
723 prompt = "Manual reject, [S]kip, Quit ?";
725 while string.find(prompt, answer) == -1:
726 answer = utils.our_raw_input(prompt);
727 m = katie.re_default_answer.search(prompt);
730 answer = string.upper(answer[:1]);
737 Katie.do_reject(1, Options["Manual-Reject"]);
738 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
745 ################################################################################
749 if not Options["No-Action"]:
750 (summary, short_summary) = Katie.build_summaries();
751 Katie.accept(summary, short_summary);
752 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
754 def check_status(files):
756 for file in files.keys():
757 if files[file]["type"] == "byhand":
759 elif files[file].has_key("new"):
761 return (new, byhand);
763 def do_pkg(changes_file):
764 Katie.pkg.changes_file = changes_file;
767 Katie.update_subst();
768 files = Katie.pkg.files;
770 (new, byhand) = check_status(files);
776 (new, byhand) = check_status(files);
778 if not new and not byhand:
781 ################################################################################
784 accept_count = Katie.accept_count;
785 accept_bytes = Katie.accept_bytes;
791 sys.stderr.write("Accepted %d package %s, %s.\n" % (accept_count, sets, utils.size_type(int(accept_bytes))));
792 Logger.log(["total",accept_count,accept_bytes]);
794 if not Options["No-Action"]:
797 ################################################################################
800 changes_files = init();
801 if len(changes_files) > 50:
802 sys.stderr.write("Sorting changes...\n");
803 changes_files.sort(changes_compare);
805 # Kill me now? **FIXME**
806 Cnf["Dinstall::Options::No-Mail"] = "";
807 bcc = "X-Katie: %s" % (lisa_version);
808 if Cnf.has_key("Dinstall::Bcc"):
809 Katie.Subst["__BCC__"] = bcc + "\nBcc: %s" % (Cnf["Dinstall::Bcc"]);
811 Katie.Subst["__BCC__"] = bcc;
813 for changes_file in changes_files:
814 changes_file = utils.validate_changes_file_arg(changes_file, 0);
817 print "\n" + changes_file;
818 do_pkg (changes_file);
822 ################################################################################
824 if __name__ == '__main__':