1 # Copyright (C) 2009 W. Trevor King <wking@drexel.edu>
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.
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.
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
22 BLOCKED_BY_TAG="BLOCKED-BY:"
24 class BrokenLink (Exception):
25 def __init__(self, blocked_bug, blocking_bug, blocks=True):
27 msg = "Missing link: %s blocks %s" \
28 % (blocking_bug.uuid, blocked_bug.uuid)
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
37 def execute(args, manipulate_encodings=True):
39 >>> from libbe import utility
40 >>> bd = bugdir.SimpleBugDir()
43 >>> execute(["a", "b"], manipulate_encodings=False)
46 >>> execute(["a"], manipulate_encodings=False)
49 >>> execute(["--show-status", "a"], manipulate_encodings=False) # doctest: +NORMALIZE_WHITESPACE
52 >>> execute(["b", "a"], manipulate_encodings=False)
57 >>> execute(["--show-status", "a"], manipulate_encodings=False) # doctest: +NORMALIZE_WHITESPACE
62 >>> execute(["-r", "b", "a"], manipulate_encodings=False)
65 >>> execute(["-r", "a", "b"], manipulate_encodings=False)
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})
74 if options.repair == True:
76 raise cmdutil.UsageError("No arguments with --repair calls.")
78 raise cmdutil.UsageError("Please a bug id.")
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.")
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
92 print "Fixed the following links:"
93 print "\n".join(["%s |-- %s" % (blockee.uuid, blocker.uuid)
94 for blockee,blocker in fixed])
97 bugA = cmdutil.bug_from_shortname(bd, args[0])
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))
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)
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])
127 print '\n'.join([bug.uuid for bug in blocked_by])
128 blocks = get_blocks(bd, bugA)
130 print "%s blocks:" % bugA.uuid
131 if options.show_status == True:
132 print '\n'.join(["%s\t%s" % (bug.uuid, bug.status)
135 print '\n'.join([bug.uuid for bug in blocks])
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")
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).
157 To search for bugs blocked by a particular bug, try
158 $ be list --extra-strings BLOCKED-BY:<your-bug-uuid>
160 In repair mode, add the missing direction to any one-way links.
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
168 return get_parser().help_str() + longhelp
170 # internal helper functions
172 def _generate_blocks_string(blocked_bug):
173 return "%s%s" % (BLOCKS_TAG, blocked_bug.uuid)
175 def _generate_blocked_by_string(blocking_bug):
176 return "%s%s" % (BLOCKED_BY_TAG, blocking_bug.uuid)
178 def _parse_blocks_string(string):
179 assert string.startswith(BLOCKS_TAG)
180 return string[len(BLOCKS_TAG):]
182 def _parse_blocked_by_string(string):
183 assert string.startswith(BLOCKED_BY_TAG)
184 return string[len(BLOCKED_BY_TAG):]
186 def _add_remove_extra_string(bug, string, add):
187 estrs = bug.extra_strings
190 else: # remove the string
192 bug.extra_strings = estrs # reassign to notice change
194 def _get_blocks(bug):
196 for line in bug.extra_strings:
197 if line.startswith(BLOCKS_TAG):
198 uuids.append(_parse_blocks_string(line))
201 def _get_blocked_by(bug):
203 for line in bug.extra_strings:
204 if line.startswith(BLOCKED_BY_TAG):
205 uuids.append(_parse_blocked_by_string(line))
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)
216 # functions exposed to other modules
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)
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)
230 def get_blocks(bugdir, bug):
232 Return a list of bugs that the given bug blocks.
235 for uuid in _get_blocks(bug):
236 blocks.append(bugdir.bug_from_uuid(uuid))
239 def get_blocked_by(bugdir, bug):
241 Return a list of bugs blocking the given bug blocks.
244 for uuid in _get_blocked_by(bug):
245 blocked_by.append(bugdir.bug_from_uuid(uuid))
248 def check_dependencies(bugdir, repair_broken_links=False):
250 Check that links are bi-directional for all bugs in bugdir.
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)
263 [(Bug(uuid='a'), Bug(uuid='b'))]
266 >>> good,repaired,broken = check_dependencies(bd, repair_broken_links=True)
272 [(Bug(uuid='a'), Bug(uuid='b'))]
276 if bugdir.sync_with_disk == True:
277 bugdir.load_all_bugs()
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))
291 broken_links.append((bug, blocker))
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))
303 broken_links.append((blockee, bug))
305 good_links.append((blockee, bug))
306 return (good_links, fixed_links, broken_links)
308 class DependencyTree (object):
310 Note: should probably be DependencyDiGraph.
312 def __init__(self, bugdir, root_bug, depth_limit=0):
314 self.root_bug = root_bug
315 self.depth_limit = depth_limit
316 def _build_tree(self, child_fn):
318 root.bug = self.root_bug
321 while len(stack) > 0:
323 if self.depth_limit > 0 and node.depth == self.depth_limit:
325 for bug in child_fn(self.bugdir, node.bug):
328 child.depth = node.depth+1
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