Updated copyright blurbs and AUTHORS and included script for future updates
[be.git] / libbe / diff.py
1 # Copyright (C) 2005-2009 Aaron Bentley and Panometrics, Inc.
2 #                         W. Trevor King <wking@drexel.edu>
3 # <abentley@panoramicfeedback.com>
4 #
5 #    This program is free software; you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation; either version 2 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program; if not, write to the Free Software
17 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 """Compare two bug trees"""
19 from libbe import cmdutil, bugdir, bug
20 from libbe.utility import time_to_str
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.has_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 bug.cmp_severity(left[1], right[1])
45
46     added.sort(bug.cmp_severity)
47     removed.sort(bug.cmp_severity)
48     modified.sort(modified_cmp)
49     lines = []
50     
51     if len(added) > 0:
52         lines.append("New bug reports:")
53         for bg in added:
54             lines.extend(bg.string(shortlist=True).splitlines())
55         lines.append("")
56
57     if len(modified) > 0:
58         printed = False
59         for old_bug, new_bug in modified:
60             change_str = bug_changes(old_bug, new_bug, bug_dir)
61             if change_str is None:
62                 continue
63             if not printed:
64                 printed = True
65                 lines.append("Modified bug reports:")
66             lines.extend(change_str.splitlines())
67         if printed == True:
68             lines.append("")
69
70     if len(removed) > 0:
71         lines.append("Removed bug reports:")
72         for bg in removed:
73             lines.extend(bg.string(shortlist=True).splitlines())
74         lines.append("")
75     
76     return '\n'.join(lines)
77
78 def change_lines(old, new, attributes):
79     change_list = []    
80     for attr in attributes:
81         old_attr = getattr(old, attr)
82         new_attr = getattr(new, attr)
83         if old_attr != new_attr:
84             change_list.append((attr, old_attr, new_attr))
85     if len(change_list) >= 0:
86         return change_list
87     else:
88         return None
89
90 def bug_changes(old, new, bugs):
91     change_list = change_lines(old, new, ("time", "creator", "severity",
92     "target", "summary", "status", "assigned"))
93
94     old_comment_ids = [c.uuid for c in old.comments()]
95     new_comment_ids = [c.uuid for c in new.comments()]
96     change_strings = ["%s: %s -> %s" % f for f in change_list]
97     for comment_id in new_comment_ids:
98         if comment_id not in old_comment_ids:
99             summary = comment_summary(new.comment_from_uuid(comment_id), "new")
100             change_strings.append(summary)
101     for comment_id in old_comment_ids:
102         if comment_id not in new_comment_ids:
103             summary = comment_summary(new.comment_from_uuid(comment_id),
104                                       "removed")
105             change_strings.append(summary)
106
107     if len(change_strings) == 0:
108         return None
109     return "%s\n  %s" % (new.string(shortlist=True),
110                          "  \n".join(change_strings))
111
112
113 def comment_summary(comment, status):
114     return "%8s comment from %s on %s" % (status, comment.From, 
115                                           time_to_str(comment.time))
116
117 suite = doctest.DocTestSuite()