3 # Handles NEW and BYHAND packages
4 # Copyright (C) 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: lisa,v 1.12 2002-05-19 02:00:48 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.12 $";
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:
172 # Sort by ctime-per-source
173 a_source = a_changes.get("source");
174 b_source = b_changes.get("source");
175 if a_source != b_source:
176 a_ctime = os.stat(a)[stat.ST_CTIME];
177 b_ctime = os.stat(b)[stat.ST_CTIME];
178 q = cmp (a_ctime, b_ctime);
182 # Sort by source name
183 q = cmp (a_source, b_source);
187 # Sort by source version
188 a_version = a_changes.get("version");
189 b_version = b_changes.get("version");
190 q = apt_pkg.VersionCompare(a_version, b_version);
194 # Fall back to sort by filename
197 ################################################################################
199 class Section_Completer:
202 q = projectB.query("SELECT section FROM section");
203 for i in q.getresult():
204 self.sections.append(i[0]);
206 def complete(self, text, state):
210 for word in self.sections:
212 self.matches.append(word);
214 return self.matches[state]
218 ############################################################
220 class Priority_Completer:
222 self.priorities = [];
223 q = projectB.query("SELECT priority FROM priority");
224 for i in q.getresult():
225 self.priorities.append(i[0]);
227 def complete(self, text, state):
231 for word in self.priorities:
233 self.matches.append(word);
235 return self.matches[state]
239 ################################################################################
241 def check_valid (new):
242 for pkg in new.keys():
243 section = new[pkg]["section"];
244 priority = new[pkg]["priority"];
245 type = new[pkg]["type"];
246 new[pkg]["section id"] = db_access.get_section_id(section);
247 new[pkg]["priority id"] = db_access.get_priority_id(new[pkg]["priority"]);
249 if (section == "debian-installer" and type != "udeb") or \
250 (section != "debian-installer" and type == "udeb"):
251 new[pkg]["section id"] = -1;
252 if (priority == "source" and type != "dsc") or \
253 (priority != "source" and type == "dsc"):
254 new[pkg]["priority id"] = -1;
256 ################################################################################
258 def print_new (new, indexed, file=sys.stdout):
262 for pkg in new.keys():
264 section = new[pkg]["section"];
265 priority = new[pkg]["priority"];
266 if new[pkg]["section id"] == -1:
267 section = section + "[!]";
269 if new[pkg]["priority id"] == -1:
270 priority = priority + "[!]";
273 line = "(%s): %-20s %-20s %-20s" % (index, pkg, priority, section);
275 line = "%-20s %-20s %-20s" % (pkg, priority, section);
276 line = string.strip(line)+'\n';
278 note = Katie.pkg.changes.get("lisa note");
285 ################################################################################
289 if f.has_key("dbtype"):
291 elif f["type"] == "orig.tar.gz" or f["type"] == "tar.gz" or f["type"] == "diff.gz" or f["type"] == "dsc":
294 utils.fubar("invalid type (%s) for new. Dazed, confused and sure as heck not continuing." % (type));
296 # Validate the override type
297 type_id = db_access.get_override_type_id(type);
299 utils.fubar("invalid type (%s) for new. Say wha?" % (type));
303 ################################################################################
305 def index_range (index):
309 return "1-%s" % (index);
311 ################################################################################
312 ################################################################################
315 # Write the current data to a temporary file
316 temp_filename = tempfile.mktemp();
317 fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
319 temp_file = utils.open_file(temp_filename, 'w');
320 print_new (new, 0, temp_file);
322 # Spawn an editor on that file
323 editor = os.environ.get("EDITOR","vi")
324 result = os.system("%s %s" % (editor, temp_filename))
326 utils.fubar ("%s invocation failed for %s." % (editor, temp_filename), result)
327 # Read the edited data back in
328 temp_file = utils.open_file(temp_filename);
329 lines = temp_file.readlines();
331 os.unlink(temp_filename);
334 line = string.strip(line[:-1]);
337 s = string.split(line);
338 # Pad the list if necessary
339 s[len(s):3] = [None] * (3-len(s));
340 (pkg, priority, section) = s[:3];
341 if not new.has_key(pkg):
342 utils.warn("Ignoring unknown package '%s'" % (pkg));
344 # Strip off any invalid markers, print_new will readd them.
345 if section[-3:] == "[!]":
346 section = section[:-3];
347 if priority[-3:] == "[!]":
348 priority = priority[:-3];
349 for file in new[pkg]["files"]:
350 Katie.pkg.files[file]["section"] = section;
351 Katie.pkg.files[file]["priority"] = priority;
352 new[pkg]["section"] = section;
353 new[pkg]["priority"] = priority;
355 ################################################################################
357 def edit_index (new, index):
358 priority = new[index]["priority"]
359 section = new[index]["section"]
360 type = new[index]["type"];
363 print string.join([index, priority, section], '\t');
367 prompt = "[B]oth, Priority, Section, Done ? ";
369 prompt = "[S]ection, Done ? ";
370 edit_priority = edit_section = 0;
372 while string.find(prompt, answer) == -1:
373 answer = utils.our_raw_input(prompt);
374 m = katie.re_default_answer.match(prompt)
377 answer = string.upper(answer[:1])
384 edit_priority = edit_section = 1;
390 readline.set_completer(Priorities.complete);
392 while not got_priority:
393 new_priority = string.strip(utils.our_raw_input("New priority: "));
394 if Priorities.priorities.count(new_priority) == 0:
395 print "E: '%s' is not a valid priority, try again." % (new_priority);
398 priority = new_priority;
402 readline.set_completer(Sections.complete);
404 while not got_section:
405 new_section = string.strip(utils.our_raw_input("New section: "));
406 if Sections.sections.count(new_section) == 0:
407 print "E: '%s' is not a valid section, try again." % (new_section);
410 section = new_section;
412 # Reset the readline completer
413 readline.set_completer(None);
415 for file in new[index]["files"]:
416 Katie.pkg.files[file]["section"] = section;
417 Katie.pkg.files[file]["priority"] = priority;
418 new[index]["priority"] = priority;
419 new[index]["section"] = section;
422 ################################################################################
424 def edit_overrides (new):
433 new_index[index] = i;
435 prompt = "(%s) edit override <n>, Editor, Done ? " % (index_range(index));
438 while not got_answer:
439 answer = utils.our_raw_input(prompt);
440 answer = string.upper(answer[:1]);
441 if answer == "E" or answer == "D":
443 elif katie.re_isanum.match (answer):
444 answer = int(answer);
445 if (answer < 1) or (answer > index):
446 print "%s is not a valid index (%s). Please retry." % (index_range(index), answer);
455 edit_index (new, new_index[answer]);
459 ################################################################################
462 # Write the current data to a temporary file
463 temp_filename = tempfile.mktemp();
464 fd = os.open(temp_filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700);
466 temp_file = utils.open_file(temp_filename, 'w');
467 temp_file.write(note);
469 editor = os.environ.get("EDITOR","vi")
472 os.system("%s %s" % (editor, temp_filename))
473 temp_file = utils.open_file(temp_filename);
474 note = string.rstrip(temp_file.read());
477 print utils.prefix_multi_line_string(note," ");
478 prompt = "[D]one, Edit, Abandon, Quit ?"
480 while string.find(prompt, answer) == -1:
481 answer = utils.our_raw_input(prompt);
482 m = katie.re_default_answer.search(prompt);
485 answer = string.upper(answer[:1]);
486 os.unlink(temp_filename);
491 Katie.pkg.changes["lisa note"] = note;
492 Katie.dump_vars(Cnf["Dir::Queue::New"]);
494 ################################################################################
498 less_fd = os.popen("less -", 'w', 0);
499 stdout_fd = sys.stdout;
501 sys.stdout = less_fd;
502 fernanda.display_changes(Katie.pkg.changes_file);
503 files = Katie.pkg.files;
504 for file in files.keys():
505 if files[file].has_key("new"):
506 type = files[file]["type"];
508 fernanda.check_deb(file);
510 fernanda.check_dsc(file);
512 sys.stdout = stdout_fd;
514 if errno.errorcode[e.errno] == 'EPIPE':
515 utils.warn("[fernanda] Caught EPIPE; skipping.");
519 except KeyboardInterrupt:
520 utils.warn("[fernanda] Caught C-c; skipping.");
523 ################################################################################
525 ## FIXME: horribly Debian specific
527 def do_bxa_notification():
528 files = Katie.pkg.files;
530 for file in files.keys():
531 if files[file]["type"] == "deb":
532 control = apt_pkg.ParseSection(apt_inst.debExtractControl(utils.open_file(file)));
533 summary = summary + "\n";
534 summary = summary + "Package: %s\n" % (control.Find("Package"));
535 summary = summary + "Description: %s\n" % (control.Find("Description"));
536 Katie.Subst["__BINARY_DESCRIPTIONS__"] = summary;
537 bxa_mail = utils.TemplateSubst(Katie.Subst,Cnf["Dir::Templates"]+"/lisa.bxa_notification");
538 utils.send_mail(bxa_mail,"");
540 ################################################################################
542 def add_overrides (new):
543 changes = Katie.pkg.changes;
544 files = Katie.pkg.files;
546 projectB.query("BEGIN WORK");
547 for suite in changes["suite"].keys():
548 suite_id = db_access.get_suite_id(suite);
549 for pkg in new.keys():
550 component_id = db_access.get_component_id(new[pkg]["component"]);
551 type_id = db_access.get_override_type_id(new[pkg]["type"]);
552 priority_id = new[pkg]["priority id"];
553 section_id = new[pkg]["section id"];
554 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));
555 for file in new[pkg]["files"]:
556 if files[file].has_key("new"):
557 del files[file]["new"];
560 projectB.query("COMMIT WORK");
562 if Cnf.FindB("Dinstall::BXANotify"):
563 do_bxa_notification();
565 ################################################################################
569 files = Katie.pkg.files;
570 changes = Katie.pkg.changes;
572 # Make a copy of distribution we can happily trample on
573 changes["suite"] = copy.copy(changes["distribution"]);
575 # Fix up the list of target suites
576 for suite in changes["suite"].keys():
577 override = Cnf.Find("Suite::%s::OverrideSuite" % (suite));
579 del changes["suite"][suite];
580 changes["suite"][override] = 1;
582 for suite in changes["suite"].keys():
583 suite_id = db_access.get_suite_id(suite);
585 utils.fubar("%s has invalid suite '%s' (possibly overriden). say wha?" % (changes, suite));
587 # The main NEW processing loop
590 # Find out what's new
591 new = determine_new(changes, files);
597 if Options["No-Action"] or Options["Automatic"]:
600 (broken, note) = print_new(new, 0);
603 if not broken and not note:
604 prompt = "Add overrides, ";
606 print "W: [!] marked entries must be fixed before package can be processed.";
608 print "W: note must be removed before package can be processed.";
609 prompt = prompt + "Remove note, ";
611 prompt = prompt + "Edit overrides, Check, Manual reject, Note edit, [S]kip, Quit ?";
613 while string.find(prompt, answer) == -1:
614 answer = utils.our_raw_input(prompt);
615 m = katie.re_default_answer.search(prompt);
618 answer = string.upper(answer[:1])
621 done = add_overrides (new);
625 new = edit_overrides (new);
627 aborted = Katie.do_reject(1, Options["Manual-Reject"]);
629 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
632 edit_note(changes.get("lisa note", ""));
634 confirm = string.lower(utils.our_raw_input("Really clear note (y/N)? "));
636 del changes["lisa note"];
642 ################################################################################
643 ################################################################################
644 ################################################################################
646 def usage (exit_code=0):
647 print """Usage: lisa [OPTION]... [CHANGES]...
648 -a, --automatic automatic run
649 -h, --help show this help and exit.
650 -m, --manual-reject=MSG manual reject with `msg'
651 -n, --no-action don't do anything
652 -V, --version display the version number and exit"""
655 ################################################################################
658 global Cnf, Options, Logger, Katie, projectB, Sections, Priorities;
660 Cnf = utils.get_conf();
662 Arguments = [('a',"automatic","Lisa::Options::Automatic"),
663 ('h',"help","Lisa::Options::Help"),
664 ('m',"manual-reject","Lisa::Options::Manual-Reject", "HasArg"),
665 ('n',"no-action","Lisa::Options::No-Action"),
666 ('V',"version","Lisa::Options::Version")];
668 for i in ["automatic", "help", "manual-reject", "no-action", "version"]:
669 if not Cnf.has_key("Lisa::Options::%s" % (i)):
670 Cnf["Lisa::Options::%s" % (i)] = "";
672 changes_files = apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
673 Options = Cnf.SubTree("Lisa::Options")
678 if Options["Version"]:
679 print "lisa %s" % (lisa_version);
682 Katie = katie.Katie(Cnf);
684 if not Options["No-Action"]:
685 Logger = Katie.Logger = logging.Logger(Cnf, "lisa");
687 projectB = Katie.projectB;
689 Sections = Section_Completer();
690 Priorities = Priority_Completer();
691 readline.parse_and_bind("tab: complete");
693 return changes_files;
695 ################################################################################
700 files = Katie.pkg.files;
704 for file in files.keys():
705 if files[file]["type"] == "byhand":
706 if os.path.exists(file):
707 print "W: %s still present; please process byhand components and try again." % (file);
713 if Options["No-Action"]:
716 if Options["Automatic"] and not Options["No-Action"]:
718 prompt = "[A]ccept, Manual reject, Skip, Quit ?";
720 prompt = "Manual reject, [S]kip, Quit ?";
722 while string.find(prompt, answer) == -1:
723 answer = utils.our_raw_input(prompt);
724 m = katie.re_default_answer.search(prompt);
727 answer = string.upper(answer[:1]);
734 Katie.do_reject(1, Options["Manual-Reject"]);
735 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
742 ################################################################################
746 if not Options["No-Action"]:
747 (summary, short_summary) = Katie.build_summaries();
748 Katie.accept(summary, short_summary);
749 os.unlink(Katie.pkg.changes_file[:-8]+".katie");
751 def check_status(files):
753 for file in files.keys():
754 if files[file]["type"] == "byhand":
756 elif files[file].has_key("new"):
758 return (new, byhand);
760 def do_pkg(changes_file):
761 Katie.pkg.changes_file = changes_file;
764 Katie.update_subst();
765 files = Katie.pkg.files;
767 (new, byhand) = check_status(files);
773 (new, byhand) = check_status(files);
775 if not new and not byhand:
778 ################################################################################
781 accept_count = Katie.accept_count;
782 accept_bytes = Katie.accept_bytes;
788 sys.stderr.write("Accepted %d package %s, %s.\n" % (accept_count, sets, utils.size_type(int(accept_bytes))));
789 Logger.log(["total",accept_count,accept_bytes]);
791 if not Options["No-Action"]:
794 ################################################################################
797 changes_files = init();
798 changes_files.sort(changes_compare);
800 # Kill me now? **FIXME**
801 Cnf["Dinstall::Options::No-Mail"] = "";
802 bcc = "X-Katie: %s" % (lisa_version);
803 if Cnf.has_key("Dinstall::Bcc"):
804 Katie.Subst["__BCC__"] = bcc + "\nBcc: %s" % (Cnf["Dinstall::Bcc"]);
806 Katie.Subst["__BCC__"] = bcc;
808 for changes_file in changes_files:
809 changes_file = utils.validate_changes_file_arg(changes_file, 0);
812 print "\n" + changes_file;
813 do_pkg (changes_file);
817 ################################################################################
819 if __name__ == '__main__':