#NEWLINE# def calculate_rule_sequence( self , target_rule_paths ) : #NEWLINE# #INDENT# "Calculates an ordered sequence of rules terminating with the given\n    target rules.\n\n    By passing multiple target names it's possible to build a combined sequence\n    that ensures all the given targets are included with no duplicate\n    dependencies.\n\n    Args:\n      target_rule_paths: A list of target rule paths to include in the\n          sequence, or a single target rule path.\n\n    Returns:\n      An ordered list of Rule instances including all of the given target rules\n      and their dependencies.\n\n    Raises:\n      KeyError: One of the given rules was not found.\n    " #NEWLINE# if isinstance ( target_rule_paths , str ) : #NEWLINE# #INDENT# target_rule_paths = [ target_rule_paths ] #NEWLINE# #UNINDENT# self . _ensure_rules_present ( target_rule_paths ) #NEWLINE# reverse_graph = self . graph . reverse ( ) #NEWLINE# sequence_graph = nx . DiGraph ( ) #NEWLINE# def _add_rule_node_dependencies( rule_node ) : #NEWLINE# #INDENT# if sequence_graph . has_node ( rule_node ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# sequence_graph . add_node ( rule_node ) #NEWLINE# for out_edge in reverse_graph . out_edges_iter ( [ rule_node ] ) : #NEWLINE# #INDENT# out_rule_node = out_edge [ 1 ] #NEWLINE# if ( not sequence_graph . has_node ( out_rule_node ) ) : #NEWLINE# #INDENT# _add_rule_node_dependencies ( out_rule_node ) #NEWLINE# #UNINDENT# sequence_graph . add_edge ( rule_node , out_rule_node ) #NEWLINE# #UNINDENT# #UNINDENT# for rule_path in target_rule_paths : #NEWLINE# #INDENT# rule = self . project . resolve_rule ( rule_path ) #NEWLINE# assert reversed_sequence_graph #NEWLINE# assert ( rule . path in self . rule_nodes ) #NEWLINE# _add_rule_node_dependencies ( rule . path ) #NEWLINE# #UNINDENT# reversed_sequence_graph = sequence_graph . reverse ( ) #NEWLINE# rule_sequence = [ ] #NEWLINE# for rule_node in nx . topological_sort ( reversed_sequence_graph ) : #NEWLINE# #INDENT# rule_sequence . append ( self . rule_nodes [ rule_node ] ) #NEWLINE# #UNINDENT# return rule_sequence