Go back to lazy bug loading to get execution speed back up.
[be.git] / libbe / diff.py
1 # Copyright (C) 2005 Aaron Bentley and Panometrics, Inc.
2 # <abentley@panoramicfeedback.com>
3 #
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.
8 #
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.
13 #
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
21 import doctest
22
23 def diff(old_bugdir, new_bugdir):
24     added = []
25     removed = []
26     modified = []
27     for uuid in old_bugdir.list_uuids():
28         old_bug = old_bugdir.bug_from_uuid(uuid)
29         try:
30             new_bug = new_bugdir.bug_from_uuid(uuid)
31             if old_bug != new_bug:
32                 modified.append((old_bug, new_bug))
33         except KeyError:
34             removed.append(old_bug)
35     for uuid in new_bugdir.list_uuids():
36         if not old_bugdir.bug_map.has_key(new_bug.uuid):
37             new_bug = new_bugdir.bug_from_uuid(uuid)
38             added.append(new_bug)
39     return (removed, modified, added)
40
41 def diff_report(diff_data, bug_dir):
42     (removed, modified, added) = diff_data
43     def modified_cmp(left, right):
44         return cmp_severity(left[1], right[1])
45
46     added.sort(cmp_severity)
47     removed.sort(cmp_severity)
48     modified.sort(modified_cmp)
49
50     if len(added) > 0:
51         print "New bug reports:"
52         for bug in added:
53             print bug.string(shortlist=True)
54
55     if len(modified) > 0:
56         printed = False
57         for old_bug, new_bug in modified:
58             change_str = bug_changes(old_bug, new_bug, bug_dir)
59             if change_str is None:
60                 continue
61             if not printed:
62                 printed = True
63                 print "Modified bug reports:"
64             print change_str
65
66     if len(removed) > 0: 
67         print "Removed bug reports:"
68         for bug in removed:
69             print bug.string(shortlist=True)
70    
71 def change_lines(old, new, attributes):
72     change_list = []    
73     for attr in attributes:
74         old_attr = getattr(old, attr)
75         new_attr = getattr(new, attr)
76         if old_attr != new_attr:
77             change_list.append((attr, old_attr, new_attr))
78     if len(change_list) >= 0:
79         return change_list
80     else:
81         return None
82
83 def bug_changes(old, new, bugs):
84     change_list = change_lines(old, new, ("time", "creator", "severity",
85     "target", "summary", "status", "assigned"))
86
87     old.load_comments()
88     old_comment_ids = [c.uuid for c in old.comment_root.traverse()]
89     new.load_comments()
90     new_comment_ids = [c.uuid for c in new.comment_root.traverse()]
91     change_strings = ["%s: %s -> %s" % f for f in change_list]
92     for comment_id in new_comment_ids:
93         if comment_id not in old_comment_ids:
94             summary = comment_summary(new.comment_root.comment_from_uuid(comment_id), "new")
95             change_strings.append(summary)
96     for comment_id in old_comment_ids:
97         if comment_id not in new_comment_ids:
98             summary = comment_summary(new.comment.root.comment_from_uuid(comment_id), "removed")
99             change_strings.append(summary)
100
101     if len(change_strings) == 0:
102         return None
103     return "%s\n  %s" % (new.string(shortlist=True),
104                          "  \n".join(change_strings))
105
106
107 def comment_summary(comment, status):
108     return "%8s comment from %s on %s" % (status, comment.From, 
109                                           time_to_str(comment.time))
110
111 suite = doctest.DocTestSuite()