Merged with Trevor's -rr branch
[be.git] / becommands / depend.py
1 # Copyright (C) 2009 W. Trevor King <wking@drexel.edu>
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 """Add/remove bug dependencies"""
17 from libbe import cmdutil, bugdir, tree
18 import os, copy
19 __desc__ = __doc__
20
21 BLOCKS_TAG="BLOCKS:"
22 BLOCKED_BY_TAG="BLOCKED-BY:"
23
24 class BrokenLink (Exception):
25     def __init__(self, blocked_bug, blocking_bug, blocks=True):
26         if blocks == True:
27             msg = "Missing link: %s blocks %s" \
28                 % (blocking_bug.uuid, blocked_bug.uuid)
29         else:
30             msg = "Missing link: %s blocked by %s" \
31                 % (blocked_bug.uuid, blocking_bug.uuid)
32         Exception.__init__(self, msg)
33         self.blocked_bug = blocked_bug
34         self.blocking_bug = blocking_bug
35
36
37 def execute(args, manipulate_encodings=True):
38     """
39     >>> from libbe import utility
40     >>> bd = bugdir.SimpleBugDir()
41     >>> bd.save()
42     >>> os.chdir(bd.root)
43     >>> execute(["a", "b"], manipulate_encodings=False)
44     a blocked by:
45     b
46     >>> execute(["a"], manipulate_encodings=False)
47     a blocked by:
48     b
49     >>> execute(["--show-status", "a"], manipulate_encodings=False) # doctest: +NORMALIZE_WHITESPACE
50     a blocked by:
51     b closed
52     >>> execute(["b", "a"], manipulate_encodings=False)
53     b blocked by:
54     a
55     b blocks:
56     a
57     >>> execute(["--show-status", "a"], manipulate_encodings=False) # doctest: +NORMALIZE_WHITESPACE
58     a blocked by:
59     b closed
60     a blocks:
61     b closed
62     >>> execute(["-r", "b", "a"], manipulate_encodings=False)
63     b blocks:
64     a
65     >>> execute(["-r", "a", "b"], manipulate_encodings=False)
66     >>> bd.cleanup()
67     """
68     parser = get_parser()
69     options, args = parser.parse_args(args)
70     cmdutil.default_complete(options, args, parser,
71                              bugid_args={0: lambda bug : bug.active==True,
72                                          1: lambda bug : bug.active==True})
73
74     if options.repair == True:
75         if len(args) > 0:
76             raise cmdutil.UsageError("No arguments with --repair calls.")
77     elif len(args) < 1:
78         raise cmdutil.UsageError("Please a bug id.")
79     elif len(args) > 2:
80         help()
81         raise cmdutil.UsageError("Too many arguments.")
82     elif len(args) == 2 and options.tree_depth != None:
83         raise cmdutil.UsageError("Only one bug id used in tree mode.")
84         
85
86     bd = bugdir.BugDir(from_disk=True,
87                        manipulate_encodings=manipulate_encodings)
88     if options.repair == True:
89         good,fixed,broken = check_dependencies(bd, repair_broken_links=True)
90         assert len(broken) == 0, broken
91         if len(fixed) > 0:
92             print "Fixed the following links:"
93             print "\n".join(["%s |-- %s" % (blockee.uuid, blocker.uuid)
94                              for blockee,blocker in fixed])
95         return 0
96
97     bugA = cmdutil.bug_from_shortname(bd, args[0])
98
99     if options.tree_depth != None:
100         dtree = DependencyTree(bd, bugA, options.tree_depth)
101         if len(dtree.blocked_by_tree()) > 0:
102             print "%s blocked by:" % bugA.uuid
103             for depth,node in dtree.blocked_by_tree().thread():
104                 if depth == 0: continue
105                 print "%s%s" % (" "*(depth), node.bug.string(shortlist=True))
106         if len(dtree.blocks_tree()) > 0:
107             print "%s blocks:" % bugA.uuid
108             for depth,node in dtree.blocks_tree().thread():
109                 if depth == 0: continue
110                 print "%s%s" % (" "*(depth), node.bug.string(shortlist=True))
111         return 0
112
113     if len(args) == 2:
114         bugB = cmdutil.bug_from_shortname(bd, args[1])
115         if options.remove == True:
116             remove_block(bugA, bugB)
117         else: # add the dependency
118             add_block(bugA, bugB)
119
120     blocked_by = get_blocked_by(bd, bugA)
121     if len(blocked_by) > 0:
122         print "%s blocked by:" % bugA.uuid
123         if options.show_status == True:
124             print '\n'.join(["%s\t%s" % (bug.uuid, bug.status)
125                              for bug in blocked_by])
126         else:
127             print '\n'.join([bug.uuid for bug in blocked_by])
128     blocks = get_blocks(bd, bugA)
129     if len(blocks) > 0:
130         print "%s blocks:" % bugA.uuid
131         if options.show_status == True:
132             print '\n'.join(["%s\t%s" % (bug.uuid, bug.status)
133                              for bug in blocks])
134         else:
135             print '\n'.join([bug.uuid for bug in blocks])
136
137 def get_parser():
138     parser = cmdutil.CmdOptionParser("be depend BUG-ID [BUG-ID]\nor:    be depend --repair")
139     parser.add_option("-r", "--remove", action="store_true",
140                       dest="remove", default=False,
141                       help="Remove dependency (instead of adding it)")
142     parser.add_option("-s", "--show-status", action="store_true",
143                       dest="show_status", default=False,
144                       help="Show status of blocking bugs")
145     parser.add_option("-t", "--tree-depth", metavar="DEPTH", default=None,
146                       type="int", dest="tree_depth",
147                       help="Print dependency tree rooted at BUG-ID with DEPTH levels of both blockers and blockees.  Set DEPTH <= 0 to disable the depth limit.")
148     parser.add_option("--repair", action="store_true",
149                       dest="repair", default=False,
150                       help="Check for and repair one-way links")
151     return parser
152
153 longhelp="""
154 Set a dependency with the second bug (B) blocking the first bug (A).
155 If bug B is not specified, just print a list of bugs blocking (A).
156
157 To search for bugs blocked by a particular bug, try
158   $ be list --extra-strings BLOCKED-BY:<your-bug-uuid>
159
160 In repair mode, add the missing direction to any one-way links.
161
162 The "|--" symbol in the repair-mode output is inspired by the
163 "negative feedback" arrow common in biochemistry.  See, for example
164   http://www.nature.com/nature/journal/v456/n7223/images/nature07513-f5.0.jpg
165 """
166
167 def help():
168     return get_parser().help_str() + longhelp
169
170 # internal helper functions
171
172 def _generate_blocks_string(blocked_bug):
173     return "%s%s" % (BLOCKS_TAG, blocked_bug.uuid)
174
175 def _generate_blocked_by_string(blocking_bug):
176     return "%s%s" % (BLOCKED_BY_TAG, blocking_bug.uuid)
177
178 def _parse_blocks_string(string):
179     assert string.startswith(BLOCKS_TAG)
180     return string[len(BLOCKS_TAG):]
181
182 def _parse_blocked_by_string(string):
183     assert string.startswith(BLOCKED_BY_TAG)
184     return string[len(BLOCKED_BY_TAG):]
185
186 def _add_remove_extra_string(bug, string, add):
187     estrs = bug.extra_strings
188     if add == True:
189         estrs.append(string)
190     else: # remove the string
191         estrs.remove(string)
192     bug.extra_strings = estrs # reassign to notice change
193
194 def _get_blocks(bug):
195     uuids = []
196     for line in bug.extra_strings:
197         if line.startswith(BLOCKS_TAG):
198             uuids.append(_parse_blocks_string(line))
199     return uuids
200
201 def _get_blocked_by(bug):
202     uuids = []
203     for line in bug.extra_strings:
204         if line.startswith(BLOCKED_BY_TAG):
205             uuids.append(_parse_blocked_by_string(line))
206     return uuids
207
208 def _repair_one_way_link(blocked_bug, blocking_bug, blocks=None):
209     if blocks == True: # add blocks link
210         blocks_string = _generate_blocks_string(blocked_bug)
211         _add_remove_extra_string(blocking_bug, blocks_string, add=True)
212     else: # add blocked by link
213         blocked_by_string = _generate_blocked_by_string(blocking_bug)
214         _add_remove_extra_string(blocked_bug, blocked_by_string, add=True)
215
216 # functions exposed to other modules
217
218 def add_block(blocked_bug, blocking_bug):
219     blocked_by_string = _generate_blocked_by_string(blocking_bug)
220     _add_remove_extra_string(blocked_bug, blocked_by_string, add=True)
221     blocks_string = _generate_blocks_string(blocked_bug)
222     _add_remove_extra_string(blocking_bug, blocks_string, add=True)
223
224 def remove_block(blocked_bug, blocking_bug):
225     blocked_by_string = _generate_blocked_by_string(blocking_bug)
226     _add_remove_extra_string(blocked_bug, blocked_by_string, add=False)
227     blocks_string = _generate_blocks_string(blocked_bug)
228     _add_remove_extra_string(blocking_bug, blocks_string, add=False)
229
230 def get_blocks(bugdir, bug):
231     """
232     Return a list of bugs that the given bug blocks.
233     """
234     blocks = []
235     for uuid in _get_blocks(bug):
236         blocks.append(bugdir.bug_from_uuid(uuid))
237     return blocks
238
239 def get_blocked_by(bugdir, bug):
240     """
241     Return a list of bugs blocking the given bug blocks.
242     """
243     blocked_by = []
244     for uuid in _get_blocked_by(bug):
245         blocked_by.append(bugdir.bug_from_uuid(uuid))
246     return blocked_by
247
248 def check_dependencies(bugdir, repair_broken_links=False):
249     """
250     Check that links are bi-directional for all bugs in bugdir.
251
252     >>> bd = bugdir.SimpleBugDir(sync_with_disk=False)
253     >>> a = bd.bug_from_uuid("a")
254     >>> b = bd.bug_from_uuid("b")
255     >>> blocked_by_string = _generate_blocked_by_string(b)
256     >>> _add_remove_extra_string(a, blocked_by_string, add=True)
257     >>> good,repaired,broken = check_dependencies(bd, repair_broken_links=False)
258     >>> good
259     []
260     >>> repaired
261     []
262     >>> broken
263     [(Bug(uuid='a'), Bug(uuid='b'))]
264     >>> _get_blocks(b)
265     []
266     >>> good,repaired,broken = check_dependencies(bd, repair_broken_links=True)
267     >>> _get_blocks(b)
268     ['a']
269     >>> good
270     []
271     >>> repaired
272     [(Bug(uuid='a'), Bug(uuid='b'))]
273     >>> broken
274     []
275     """
276     if bugdir.sync_with_disk == True:
277         bugdir.load_all_bugs()
278     good_links = []
279     fixed_links = []
280     broken_links = []
281     for bug in bugdir:
282         for blocker in get_blocked_by(bugdir, bug):
283             blocks = get_blocks(bugdir, blocker)
284             if (bug, blocks) in good_links+fixed_links+broken_links:
285                 continue # already checked that link
286             if bug not in blocks:
287                 if repair_broken_links == True:
288                     _repair_one_way_link(bug, blocker, blocks=True)
289                     fixed_links.append((bug, blocker))
290                 else:
291                     broken_links.append((bug, blocker))
292             else:
293                 good_links.append((bug, blocker))
294         for blockee in get_blocks(bugdir, bug):
295             blocked_by = get_blocked_by(bugdir, blockee)
296             if (blockee, bug) in good_links+fixed_links+broken_links:
297                 continue # already checked that link
298             if bug not in blocked_by:
299                 if repair_broken_links == True:
300                     _repair_one_way_link(blockee, bug, blocks=False)
301                     fixed_links.append((blockee, bug))
302                 else:
303                     broken_links.append((blockee, bug))
304             else:
305                 good_links.append((blockee, bug))
306     return (good_links, fixed_links, broken_links)
307
308 class DependencyTree (object):
309     """
310     Note: should probably be DependencyDiGraph.
311     """
312     def __init__(self, bugdir, root_bug, depth_limit=0):
313         self.bugdir = bugdir
314         self.root_bug = root_bug
315         self.depth_limit = depth_limit
316     def _build_tree(self, child_fn):
317         root = tree.Tree()
318         root.bug = self.root_bug
319         root.depth = 0
320         stack = [root]
321         while len(stack) > 0:
322             node = stack.pop()
323             if self.depth_limit > 0 and node.depth == self.depth_limit:
324                 continue
325             for bug in child_fn(self.bugdir, node.bug):
326                 child = tree.Tree()
327                 child.bug = bug
328                 child.depth = node.depth+1
329                 node.append(child)
330                 stack.append(child)
331         return root
332     def blocks_tree(self):
333         if not hasattr(self, "_blocks_tree"):
334             self._blocks_tree = self._build_tree(get_blocks)
335         return self._blocks_tree
336     def blocked_by_tree(self):
337         if not hasattr(self, "_blocked_by_tree"):
338             self._blocked_by_tree = self._build_tree(get_blocked_by)
339         return self._blocked_by_tree