<s> """<STR_LIT>""" <EOL> import os <EOL> import sets <EOL> from oslo_config import cfg <EOL> from st2common import config <EOL> from st2common . util . monkey_patch import monkey_patch <EOL> from st2common . persistence . rule import Rule <EOL> from st2common . service_setup import db_setup <EOL> try : <EOL> from graphviz import Digraph <EOL> except ImportError : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise ImportError ( msg ) <EOL> def do_register_cli_opts ( opts , ignore_errors = False ) : <EOL> for opt in opts : <EOL> try : <EOL> cfg . CONF . register_cli_opt ( opt ) <EOL> except : <EOL> if not ignore_errors : <EOL> raise <EOL> class RuleLink ( object ) : <EOL> def __init__ ( self , source_action_ref , rule_ref , dest_action_ref ) : <EOL> self . _source_action_ref = source_action_ref <EOL> self . _rule_ref = rule_ref <EOL> self . _dest_action_ref = dest_action_ref <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _source_action_ref , self . _rule_ref , self . _dest_action_ref ) <EOL> class LinksAnalyzer ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _rule_link_by_action_ref = { } <EOL> self . _rules = { } <EOL> def analyze ( self , root_action_ref , link_tigger_ref ) : <EOL> rules = Rule . query ( trigger = link_tigger_ref , enabled = True ) <EOL> for rule in rules : <EOL> source_action_ref = self . _get_source_action_ref ( rule ) <EOL> if not source_action_ref : <EOL> print '<STR_LIT>' % rule . ref <EOL> continue <EOL> rule_links = self . _rules . get ( source_action_ref , None ) <EOL> if rule_links is None : <EOL> rule_links = [ ] <EOL> self . _rules [ source_action_ref ] = rule_links <EOL> rule_links . append ( RuleLink ( source_action_ref = source_action_ref , rule_ref = rule . ref , <EOL> dest_action_ref = rule . action . ref ) ) <EOL> analyzed = self . _do_analyze ( action_ref = root_action_ref ) <EOL> for ( depth , rule_link ) in analyzed : <EOL> print '<STR_LIT>' % ( '<STR_LIT:U+0020>' * depth , rule_link ) <EOL> return analyzed <EOL> def _get_source_action_ref ( self , rule ) : <EOL> criteria = rule . criteria <EOL> source_action_ref = criteria . get ( '<STR_LIT>' , None ) <EOL> if not source_action_ref : <EOL> source_action_ref = criteria . get ( '<STR_LIT>' , None ) <EOL> return source_action_ref [ '<STR_LIT>' ] if source_action_ref else None <EOL> def _do_analyze ( self , action_ref , rule_links = None , processed = None , depth = <NUM_LIT:0> ) : <EOL> if processed is None : <EOL> processed = sets . Set ( ) <EOL> if rule_links is None : <EOL> rule_links = [ ] <EOL> processed . add ( action_ref ) <EOL> for rule_link in self . _rules . get ( action_ref , [ ] ) : <EOL> rule_links . append ( ( depth , rule_link ) ) <EOL> if rule_link . _dest_action_ref in processed : <EOL> continue <EOL> self . _do_analyze ( rule_link . _dest_action_ref , rule_links = rule_links , <EOL> processed = processed , depth = depth + <NUM_LIT:1> ) <EOL> return rule_links <EOL> class Grapher ( object ) : <EOL> def generate_graph ( self , rule_links , out_file ) : <EOL> graph_label = '<STR_LIT>' <EOL> graph_attr = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:t>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:label>' : graph_label <EOL> } <EOL> node_attr = { } <EOL> dot = Digraph ( comment = '<STR_LIT>' , <EOL> node_attr = node_attr , graph_attr = graph_attr , format = '<STR_LIT>' ) <EOL> nodes = sets . Set ( ) <EOL> for _ , rule_link in rule_links : <EOL> print rule_link . _source_action_ref <EOL> if rule_link . _source_action_ref not in nodes : <EOL> nodes . add ( rule_link . _source_action_ref ) <EOL> dot . node ( rule_link . _source_action_ref , rule_link . _source_action_ref ) <EOL> if rule_link . _dest_action_ref not in nodes : <EOL> nodes . add ( rule_link . _dest_action_ref ) <EOL> dot . node ( rule_link . _dest_action_ref , rule_link . _dest_action_ref ) <EOL> dot . edge ( rule_link . _source_action_ref , rule_link . _dest_action_ref , constraint = '<STR_LIT:true>' , <EOL> label = rule_link . _rule_ref ) <EOL> output_path = os . path . join ( os . getcwd ( ) , out_file ) <EOL> dot . format = '<STR_LIT>' <EOL> dot . render ( output_path ) <EOL> def main ( ) : <EOL> monkey_patch ( ) <EOL> cli_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> ] <EOL> do_register_cli_opts ( cli_opts ) <EOL> config . parse_args ( ) <EOL> db_setup ( ) <EOL> rule_links = LinksAnalyzer ( ) . analyze ( cfg . CONF . action_ref , cfg . CONF . link_trigger_ref ) <EOL> Grapher ( ) . generate_graph ( rule_links , cfg . CONF . out_file ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
