Explicit rcs.cleanup() in bugdir test.
[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 old_bug in old_bugdir:
28         new_bug = new_bugdir.bug_map.get(old_bug.uuid)
29         if new_bug is None :
30             removed.append(old_bug)
31         else:
32             if old_bug != new_bug:
33                 modified.append((old_bug, new_bug))
34     for new_bug in new_bugdir:
35         if not old_bugdir.bug_map.has_key(new_bug.uuid):
36             added.append(new_bug)
37     return (removed, modified, added)
38
39 def diff_report(diff_data, bug_dir):
40     (removed, modified, added) = diff_data
41     def modified_cmp(left, right):
42         return cmp_severity(left[1], right[1])
43
44     added.sort(cmp_severity)
45     removed.sort(cmp_severity)
46     modified.sort(modified_cmp)
47
48     if len(added) > 0:
49         print "New bug reports:"
50         for bug in added:
51             print bug.string(shortlist=True)
52
53     if len(modified) > 0:
54         printed = False
55         for old_bug, new_bug in modified:
56             change_str = bug_changes(old_bug, new_bug, bug_dir)
57             if change_str is None:
58                 continue
59             if not printed:
60                 printed = True
61                 print "Modified bug reports:"
62             print change_str
63
64     if len(removed) > 0: 
65         print "Removed bug reports:"
66         for bug in removed:
67             print bug.string(shortlist=True)
68    
69 def change_lines(old, new, attributes):
70     change_list = []    
71     for attr in attributes:
72         old_attr = getattr(old, attr)
73         new_attr = getattr(new, attr)
74         if old_attr != new_attr:
75             change_list.append((attr, old_attr, new_attr))
76     if len(change_list) >= 0:
77         return change_list
78     else:
79         return None
80
81 def bug_changes(old, new, bugs):
82     change_list = change_lines(old, new, ("time", "creator", "severity",
83     "target", "summary", "status", "assigned"))
84
85     old_comment_ids = [c.uuid for c in old.comment_root.traverse()]
86     new_comment_ids = [c.uuid for c in new.comment_root.traverse()]
87     change_strings = ["%s: %s -> %s" % f for f in change_list]
88     for comment_id in new_comment_ids:
89         if comment_id not in old_comment_ids:
90             summary = comment_summary(new.comment_root.comment_from_uuid(comment_id), "new")
91             change_strings.append(summary)
92     for comment_id in old_comment_ids:
93         if comment_id not in new_comment_ids:
94             summary = comment_summary(new.comment.root.comment_from_uuid(comment_id), "removed")
95             change_strings.append(summary)
96
97     if len(change_strings) == 0:
98         return None
99     return "%s\n  %s" % (new.string(shortlist=True),
100                          "  \n".join(change_strings))
101
102
103 def comment_summary(comment, status):
104     return "%8s comment from %s on %s" % (status, comment.From, 
105                                           time_to_str(comment.time))
106
107 suite = doctest.DocTestSuite()