1 # Copyright (C) 2005 Aaron Bentley and Panometrics, Inc.
2 # <abentley@panoramicfeedback.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """Compare two bug trees"""
18 from libbe import cmdutil, bugdir
19 from libbe.utility import time_to_str
20 from libbe.bug import cmp_severity
22 def diff(old_tree, new_tree):
23 old_bug_map = old_tree.bug_map()
24 new_bug_map = new_tree.bug_map()
28 for old_bug in old_bug_map.itervalues():
29 new_bug = new_bug_map.get(old_bug.uuid)
31 removed.append(old_bug)
33 if old_bug != new_bug:
34 modified.append((old_bug, new_bug))
35 for new_bug in new_bug_map.itervalues():
36 if not old_bug_map.has_key(new_bug.uuid):
38 return (removed, modified, added)
41 def reference_diff(bugdir, spec=None):
42 return diff(bugdir.get_reference_bugdir(spec), bugdir)
44 def diff_report(diff_data, bug_dir):
45 (removed, modified, added) = diff_data
46 bugs = list(bug_dir.list())
47 def modified_cmp(left, right):
48 return cmp_severity(left[1], right[1])
50 added.sort(cmp_severity)
51 removed.sort(cmp_severity)
52 modified.sort(modified_cmp)
55 print "New bug reports:"
57 print bug.string(shortlist=True)
61 for old_bug, new_bug in modified:
62 change_str = bug_changes(old_bug, new_bug, bugs)
63 if change_str is None:
67 print "Modified bug reports:"
71 print "Removed bug reports:"
73 print bug.string(bugs, shortlist=True)
75 def change_lines(old, new, attributes):
77 for attr in attributes:
78 old_attr = getattr(old, attr)
79 new_attr = getattr(new, attr)
80 if old_attr != new_attr:
81 change_list.append((attr, old_attr, new_attr))
82 if len(change_list) >= 0:
87 def bug_changes(old, new, bugs):
88 change_list = change_lines(old, new, ("time", "creator", "severity",
89 "target", "summary", "status", "assigned"))
91 old_comment_ids = list(old.iter_comment_ids())
92 new_comment_ids = list(new.iter_comment_ids())
93 change_strings = ["%s: %s -> %s" % f for f in change_list]
94 for comment_id in new_comment_ids:
95 if comment_id not in old_comment_ids:
96 summary = comment_summary(new.get_comment(comment_id), "new")
97 change_strings.append(summary)
98 for comment_id in old_comment_ids:
99 if comment_id not in new_comment_ids:
100 summary = comment_summary(new.get_comment(comment_id), "removed")
101 change_strings.append(summary)
103 if len(change_strings) == 0:
105 return "%s%s\n" % (new.string(bugs, shortlist=True),
106 "\n".join(change_strings))
109 def comment_summary(comment, status):
110 return "%8s comment from %s on %s" % (status, comment.From,
111 time_to_str(comment.time))