3 # Handles NEW and BYHAND packages
4 # Copyright (C) 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: lisa,v 1.11 2002-05-18 23:55:07 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.11 $";
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 a_changes = utils.parse_changes(a);
149 b_changes = utils.parse_changes(b);
153 utils.cc_fix_changes (a_changes);
154 utils.cc_fix_changes (b_changes);
156 # Sort by 'have note';
157 a_has_note = a_changes.get("lisa note");
158 b_has_note = b_changes.get("lisa note");
159 if a_has_note and not b_has_note:
161 elif b_has_note and not a_has_note:
164 # Sort by 'have source'
165 a_has_source = a_changes["architecture"].get("source");
166 b_has_source = b_changes["architecture"].get("source");
167 if a_has_source and not b_has_source:
169 elif b_has_source and not a_has_source:
173 a_ctime = os.stat(a)[stat.ST_CTIME];
174 b_ctime = os.stat(b)[stat.ST_CTIME];
175 q = cmp (a_ctime, b_ctime);
179 # Sort by source name
180 a_source = a_changes.get("source");
181 b_source = b_changes.get("source");
182 q = cmp (a_source, b_source);
186 # Sort by source version
187 a_version = a_changes.get("version");
188 b_version = b_changes.get("version");
189 q = apt_pkg.VersionCompare(a_version, b_version);
193 # Fall back to sort by filename
196 ################################################################################
198 class Section_Completer:
201 q = projectB.query("SELECT section FROM section");
202 for i in q.getresult():
203 self.sections.append(i[0]);
205 def complete(self, text, state):
209 for word in self.sections:
211 self.matches.append(word);
213 return self.matches[state]
217 ############################################################
219 class Priority_Completer:
221 self.priorities = [];
222 q = projectB.query("SELECT priority FROM priority");
223 for i in q.getresult():
224 self.priorities.append(i[0]);
226 def complete(self, text, state):
230 for word in self.priorities:
232 self.matches.append(word);
234 return self.matches[state]
238 ################################################################################
240 def check_valid (new):
241 for pkg in new.keys():
242 section = new[pkg]["section"];
243 priority = new[pkg]["priority"];
244 type = new[pkg]["type"];
245 new[pkg]["section id"] = db_access.get_section_id(section);
246 new[pkg]["priority id"] = db_access.get_priority_id(new[pkg]["priority"]);
248 if (section == "debian-installer" and type != "udeb") or \
249 (section != "debian-installer" and type == "udeb"):
250 new[pkg]["section id"] = -1;
251 if (priority == "source" and type != "dsc") or \
252 (priority != "source" and type == "dsc"):
253 new[pkg]["priority id"] = -1;
255 ################################################################################
257 def print_new (new, indexed, file=sys.stdout):
261 for pkg in new.keys():
263 section = new[pkg]["section"];
264 priority = new[pkg]["priority"];
265 if new[pkg]["section id"] == -1:
266 section = section + "[!]";
268 if new[pkg]["priority id"] == -1:
269 priority = priority + "[!]";
272 line = "(%s): %-20s %-20s %-20s" % (index, pkg, priority, section);
274 line = "%-20s %-20s %-20s" % (pkg, priority, section);
275 line = string.strip(line)+'\n';
277 note = Katie.pkg.changes.get("lisa note");
284 ################################################################################
288 if f.has_key("dbtype"):
290 elif f["type"] == "orig.tar.gz" or f["type"] == "tar.gz" or f["type"] == "diff.gz" or f["type"] == "dsc":
293 utils.fubar("invalid type (%s) for new. Dazed, confused and sure as heck not continuing." % (type));
295 # Validate the override type
296 type_id = db_access.get_override_type_id(type);
298 utils.fubar("invalid type (%s) for new. Say wha?" % (type));
302 ################################################################################
304 def index_range (index):
308 return "1-%s" % (index);
310 ################################################################################
311 ################################################################################
314 # Write the current data to a temporary file
315 temp_filename = tempfile.mktemp();
316 fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
318 temp_file = utils.open_file(temp_filename, 'w');
319 print_new (new, 0, temp_file);
321 # Spawn an editor on that file
322 editor = os.environ.get("EDITOR","vi")
323 result = os.system("%s %s" % (editor, temp_filename))
325 utils.fubar ("%s invocation failed for %s." % (editor, temp_filename), result)
326 # Read the edited data back in
327 temp_file = utils.open_file(temp_filename);
328 lines = temp_file.readlines();
330 os.unlink(temp_filename);
333 line = string.strip(line[:-1]);
336 s = string.split(line);
337 # Pad the list if necessary
338 s[len(s):3] = [None] * (3-len(s));
339 (pkg, priority, section) = s[:3];
340 if not new.has_key(pkg):
341 utils.warn("Ignoring unknown package '%s'" % (pkg));
343 # Strip off any invalid markers, print_new will readd them.
344 if section[-3:] == "[!]":
345 section = section[:-3];
346 if priority[-3:] == "[!]":
347 priority = priority[:-3];
348 for file in new[pkg]["files"]:
349 Katie.pkg.files[file]["section"] = section;
350 Katie.pkg.files[file]["priority"] = priority;
351 new[pkg]["section"] = section;
352 new[pkg]["priority"] = priority;
354 ################################################################################
356 def edit_index (new, index):
357 priority = new[index]["priority"]
358 section = new[index]["section"]
359 type = new[index]["type"];
362 print string.join([index, priority, section], '\t');
366 prompt = "[B]oth, Priority, Section, Done ? ";
368 prompt = "[S]ection, Done ? ";
369 edit_priority = edit_section = 0;
371 while string.find(prompt, answer) == -1:
372 answer = utils.our_raw_input(prompt);
373 m = katie.re_default_answer.match(prompt)
376 answer = string.upper(answer[:1])
383 edit_priority = edit_section = 1;
389 readline.set_completer(Priorities.complete);
391 while not got_priority:
392 new_priority = string.strip(utils.our_raw_input("New priority: "));
393 if Priorities.priorities.count(new_priority) == 0:
394 print "E: '%s' is not a valid priority, try again." % (new_priority);
397 priority = new_priority;
401 readline.set_completer(Sections.complete);
403 while not got_section:
404 new_section = string.strip(utils.our_raw_input("New section: "));
405 if Sections.sections.count(new_section) == 0:
406 print "E: '%s' is not a valid section, try again." % (new_section);
409 section = new_section;
411 # Reset the readline completer
412 readline.set_completer(None);
414 for file in new[index]["files"]:
415 Katie.pkg.files[file]["section"] = section;
416 Katie.pkg.files[file]["priority"] = priority;
417 new[index]["priority"] = priority;
418 new[index]["section"] = section;
421 ################################################################################
423 def edit_overrides (new):
432 new_index[index] = i;
434 prompt = "(%s) edit override <n>, Editor, Done ? " % (index_range(index));
437 while not got_answer:
438 answer = utils.our_raw_input(prompt);
439 answer = string.upper(answer[:1]);
440 if answer == "E" or answer == "D":
442 elif katie.re_isanum.match (answer):
443 answer = int(answer);
444 if (answer < 1) or (answer > index):
445 print "%s is not a valid index (%s). Please retry." % (index_range(index), answer);
454 edit_index (new, new_index[answer]);
458 ################################################################################
461 # Write the current data to a temporary file
462 temp_filename = tempfile.mktemp();
463 fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
465 temp_file = utils.open_file(temp_filename, 'w');
466 temp_file.write(note);
468 editor = os.environ.get("EDITOR","vi")
471 os.system("%s %s" % (editor, temp_filename))
472 temp_file = utils.open_file(temp_filename);
473 note = string.rstrip(temp_file.read());
476 print utils.prefix_multi_line_string(note," ");
477 prompt = "[D]one, Edit, Abandon, Quit ?"
479 while string.find(prompt, answer) == -1:
480 answer = utils.our_raw_input(prompt);
481 m = katie.re_default_answer.search(prompt);
484 answer = string.upper(answer[:1]);
485 os.unlink(temp_filename);
490 Katie.pkg.changes["lisa note"] = note;
491 Katie.dump_vars(Cnf["Dir::Queue::New"]);
493 ################################################################################
497 less_fd = os.popen("less -", 'w', 0);
498 stdout_fd = sys.stdout;
500 sys.stdout = less_fd;
501 fernanda.display_changes(Katie.pkg.changes_file);
502 files = Katie.pkg.files;
503 for file in files.keys():
504 if files[file].has_key("new"):
505 type = files[file]["type"];
507 fernanda.check_deb(file);
509 fernanda.check_dsc(file);
511 sys.stdout = stdout_fd;
513 if errno.errorcode[e.errno] == 'EPIPE':
514 utils.warn("[fernanda] Caught EPIPE; skipping.");
518 except KeyboardInterrupt:
519 utils.warn("[fernanda] Caught C-c; skipping.");
522 ################################################################################
524 ## FIXME: horribly Debian specific
526 def do_bxa_notification():
527 files = Katie.pkg.files;
529 for file in files.keys():
530 if files[file]["type"] == "deb":
531 control = apt_pkg.ParseSection(apt_inst.debExtractControl(utils.open_file(file)));
532 summary = summary + "\n";
533 summary = summary + "Package: %s\n" % (control.Find("Package"));
534 summary = summary + "Description: %s\n" % (control.Find("Description"));
535 Katie.Subst["__BINARY_DESCRIPTIONS__"] = summary;
536 bxa_mail = utils.TemplateSubst(Katie.Subst,Cnf["Dir::Templates"]+"/lisa.bxa_notification");
537 utils.send_mail(bxa_mail,"");
539 ################################################################################
541 def add_overrides (new):
542 changes = Katie.pkg.changes;
543 files = Katie.pkg.files;
545 projectB.query("BEGIN WORK");
546 for suite in changes["suite"].keys():
547 suite_id = db_access.get_suite_id(suite);
548 for pkg in new.keys():
549 component_id = db_access.get_component_id(new[pkg]["component"]);
550 type_id = db_access.get_override_type_id(new[pkg]["type"]);
551 priority_id = new[pkg]["priority id"];
552 section_id = new[pkg]["section id"];
553 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));
554 for file in new[pkg]["files"]:
555 if files[file].has_key("new"):
556 del files[file]["new"];
559 projectB.query("COMMIT WORK");
561 if Cnf.FindB("Dinstall::BXANotify"):
562 do_bxa_notification();
564 ################################################################################
568 files = Katie.pkg.files;
569 changes = Katie.pkg.changes;
571 # Make a copy of distribution we can happily trample on
572 changes["suite"] = copy.copy(changes["distribution"]);
574 # Fix up the list of target suites
575 for suite in changes["suite"].keys():
576 override = Cnf.Find("Suite::%s::OverrideSuite" % (suite));
578 del changes["suite"][suite];
579 changes["suite"][override] = 1;
581 for suite in changes["suite"].keys():
582 suite_id = db_access.get_suite_id(suite);
584 utils.fubar("%s has invalid suite '%s' (possibly overriden). say wha?" % (changes, suite));
586 # The main NEW processing loop
589 # Find out what's new
590 new = determine_new(changes, files);
596 if Options["No-Action"] or Options["Automatic"]:
599 (broken, note) = print_new(new, 0);
602 prompt = "Add overrides, ";
604 print "W: [!] marked entries must be fixed before package can be processed.";
606 print "W: note must be removed before package can be processed.";
607 prompt = prompt + "Remove note, ";
608 prompt = prompt + "Edit overrides, Check, Manual reject, Note edit, [S]kip, Quit ?";
610 while string.find(prompt, answer) == -1:
611 answer = utils.our_raw_input(prompt);
612 m = katie.re_default_answer.search(prompt);
615 answer = string.upper(answer[:1])
618 done = add_overrides (new);
622 new = edit_overrides (new);
624 aborted = Katie.do_reject(1, Options["Manual-Reject"]);
626 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
629 edit_note(changes.get("lisa note", ""));
631 confirm = string.lower(utils.our_raw_input("Really clear note (y/N)? "));
633 del changes["lisa note"];
639 ################################################################################
640 ################################################################################
641 ################################################################################
643 def usage (exit_code=0):
644 print """Usage: lisa [OPTION]... [CHANGES]...
645 -a, --automatic automatic run
646 -h, --help show this help and exit.
647 -m, --manual-reject=MSG manual reject with `msg'
648 -n, --no-action don't do anything
649 -V, --version display the version number and exit"""
652 ################################################################################
655 global Cnf, Options, Logger, Katie, projectB, Sections, Priorities;
657 Cnf = utils.get_conf();
659 Arguments = [('a',"automatic","Lisa::Options::Automatic"),
660 ('h',"help","Lisa::Options::Help"),
661 ('m',"manual-reject","Lisa::Options::Manual-Reject", "HasArg"),
662 ('n',"no-action","Lisa::Options::No-Action"),
663 ('V',"version","Lisa::Options::Version")];
665 for i in ["automatic", "help", "manual-reject", "no-action", "version"]:
666 if not Cnf.has_key("Lisa::Options::%s" % (i)):
667 Cnf["Lisa::Options::%s" % (i)] = "";
669 changes_files = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
670 Options = Cnf.SubTree("Lisa::Options")
675 if Options["Version"]:
676 print "lisa %s" % (lisa_version);
679 Katie = katie.Katie(Cnf);
681 if not Options["No-Action"]:
682 Logger = Katie.Logger = logging.Logger(Cnf, "lisa");
684 projectB = Katie.projectB;
686 Sections = Section_Completer();
687 Priorities = Priority_Completer();
688 readline.parse_and_bind("tab: complete");
690 return changes_files;
692 ################################################################################
697 files = Katie.pkg.files;
701 for file in files.keys():
702 if files[file]["type"] == "byhand":
703 if os.path.exists(file):
704 print "W: %s still present; please process byhand components and try again." % (file);
710 if Options["No-Action"]:
713 if Options["Automatic"] and not Options["No-Action"]:
715 prompt = "[A]ccept, Manual reject, Skip, Quit ?";
717 prompt = "Manual reject, [S]kip, Quit ?";
719 while string.find(prompt, answer) == -1:
720 answer = utils.our_raw_input(prompt);
721 m = katie.re_default_answer.search(prompt);
724 answer = string.upper(answer[:1]);
731 Katie.do_reject(1, Options["Manual-Reject"]);
732 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
739 ################################################################################
743 if not Options["No-Action"]:
744 (summary, short_summary) = Katie.build_summaries();
745 Katie.accept(summary, short_summary);
746 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
748 def check_status(files):
750 for file in files.keys():
751 if files[file]["type"] == "byhand":
753 elif files[file].has_key("new"):
755 return (new, byhand);
757 def do_pkg(changes_file):
758 Katie.pkg.changes_file = changes_file;
761 Katie.update_subst();
762 files = Katie.pkg.files;
764 (new, byhand) = check_status(files);
770 (new, byhand) = check_status(files);
772 if not new and not byhand:
775 ################################################################################
778 accept_count = Katie.accept_count;
779 accept_bytes = Katie.accept_bytes;
785 sys.stderr.write("Accepted %d package %s, %s.\n" % (accept_count, sets, utils.size_type(int(accept_bytes))));
786 Logger.log(["total",accept_count,accept_bytes]);
788 if not Options["No-Action"]:
791 ################################################################################
794 changes_files = init();
795 changes_files.sort(changes_compare);
797 # Kill me now? **FIXME**
798 Cnf["Dinstall::Options::No-Mail"] = "";
799 bcc = "X-Katie: %s" % (lisa_version);
800 if Cnf.has_key("Dinstall::Bcc"):
801 Katie.Subst["__BCC__"] = bcc + "\nBcc: %s" % (Cnf["Dinstall::Bcc"]);
803 Katie.Subst["__BCC__"] = bcc;
805 for changes_file in changes_files:
806 changes_file = utils.validate_changes_file_arg(changes_file, 0);
809 print "\n" + changes_file;
810 do_pkg (changes_file);
814 ################################################################################
816 if __name__ == '__main__':