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