From: Zane Bitter Date: Tue, 28 May 2013 08:16:37 +0000 (+0200) Subject: Move deletion from dependency graph to __delitem__ X-Git-Tag: 2014.1~542^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=1f0a9bc2d1558747f8817d9cd48f0eaaa12a808c;p=openstack-build%2Fheat-build.git Move deletion from dependency graph to __delitem__ Graph objects are mutable, and will in future be used for operations other than a topological sort, so abstract out the code for deleting backlinks to a node from the other nodes that have dependencies on it when it is removed. Change-Id: I86b81303ed2dffd969a2db10535761f108f4808a --- diff --git a/heat/engine/dependencies.py b/heat/engine/dependencies.py index 9e29ac30..f8f6e627 100644 --- a/heat/engine/dependencies.py +++ b/heat/engine/dependencies.py @@ -110,6 +110,15 @@ class Graph(collections.defaultdict): '''Return a copy of the graph with the edges reversed.''' return Graph(self.map(lambda n: n.reverse_copy())) + def __delitem__(self, key): + '''Delete the node given by the specified key from the graph.''' + node = self[key] + + for src in node.required_by(): + self[src] -= key + + return super(Graph, self).__delitem__(key) + def __str__(self): '''Convert the graph to a human-readable string.''' pairs = ('%s: %s' % (str(k), str(v)) for k, v in self.iteritems()) @@ -122,23 +131,16 @@ class Graph(collections.defaultdict): This is a destructive operation for the graph. ''' - def next_leaf(): - for leaf, node in graph.iteritems(): - if not node: - return leaf, node - - # There are nodes remaining, but no more leaves: a cycle - raise CircularDependencyException(cycle=str(graph)) - for iteration in xrange(len(graph)): - leaf, node = next_leaf() - yield leaf - - # Remove the node and all edges connected to it before - # continuing to look for more leaves - for src in node.required_by(): - graph[src] -= leaf - del graph[leaf] + for key, node in graph.iteritems(): + if not node: + yield key + del graph[key] + break + else: + # There are nodes remaining, but none without + # dependencies: a cycle + raise CircularDependencyException(cycle=str(graph)) class Dependencies(object):