]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Move deletion from dependency graph to __delitem__
authorZane Bitter <zbitter@redhat.com>
Tue, 28 May 2013 08:16:37 +0000 (10:16 +0200)
committerZane Bitter <zbitter@redhat.com>
Tue, 28 May 2013 08:16:37 +0000 (10:16 +0200)
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

heat/engine/dependencies.py

index 9e29ac305ed6b2b28ff16495bc9e5eb66c2f901c..f8f6e6272007b7e6deefbd1501c72672e57a7d63 100644 (file)
@@ -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):