<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import re <EOL> import pprint <EOL> import time <EOL> from salt . exceptions import CommandExecutionError <EOL> import salt . ext . six as six <EOL> log = logging . getLogger ( __name__ ) <EOL> def run_command ( jboss_config , command , fail_on_error = True ) : <EOL> '''<STR_LIT>''' <EOL> cli_command_result = __call_cli ( jboss_config , command ) <EOL> if cli_command_result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> cli_command_result [ '<STR_LIT:success>' ] = True <EOL> else : <EOL> if fail_on_error : <EOL> raise CommandExecutionError ( '''<STR_LIT>''' . format ( ** cli_command_result ) ) <EOL> else : <EOL> cli_command_result [ '<STR_LIT:success>' ] = False <EOL> return cli_command_result <EOL> def run_operation ( jboss_config , operation , fail_on_error = True , retries = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> cli_command_result = __call_cli ( jboss_config , operation , retries ) <EOL> if cli_command_result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> if _is_cli_output ( cli_command_result [ '<STR_LIT>' ] ) : <EOL> cli_result = _parse ( cli_command_result [ '<STR_LIT>' ] ) <EOL> cli_result [ '<STR_LIT:success>' ] = cli_result [ '<STR_LIT>' ] == '<STR_LIT:success>' <EOL> else : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( cli_command_result [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> if _is_cli_output ( cli_command_result [ '<STR_LIT>' ] ) : <EOL> cli_result = _parse ( cli_command_result [ '<STR_LIT>' ] ) <EOL> cli_result [ '<STR_LIT:success>' ] = False <EOL> match = re . search ( r'<STR_LIT>' , cli_result [ '<STR_LIT>' ] ) <EOL> cli_result [ '<STR_LIT>' ] = match . group ( <NUM_LIT:1> ) <EOL> cli_result [ '<STR_LIT>' ] = cli_command_result [ '<STR_LIT>' ] <EOL> else : <EOL> if fail_on_error : <EOL> raise CommandExecutionError ( '''<STR_LIT>''' . format ( ** cli_command_result ) ) <EOL> else : <EOL> cli_result = { <EOL> '<STR_LIT:success>' : False , <EOL> '<STR_LIT>' : cli_command_result [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : cli_command_result [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : cli_command_result [ '<STR_LIT>' ] <EOL> } <EOL> return cli_result <EOL> def __call_cli ( jboss_config , command , retries = <NUM_LIT:1> ) : <EOL> command_segments = [ <EOL> jboss_config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( jboss_config [ '<STR_LIT>' ] ) <EOL> ] <EOL> if '<STR_LIT>' in six . iterkeys ( jboss_config ) : <EOL> command_segments . append ( '<STR_LIT>' . format ( jboss_config [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in six . iterkeys ( jboss_config ) : <EOL> command_segments . append ( '<STR_LIT>' . format ( jboss_config [ '<STR_LIT>' ] ) ) <EOL> command_segments . append ( '<STR_LIT>' . format ( __escape_command ( command ) ) ) <EOL> cli_script = '<STR_LIT:U+0020>' . join ( command_segments ) <EOL> cli_command_result = __salt__ [ '<STR_LIT>' ] ( cli_script ) <EOL> log . debug ( '<STR_LIT>' , str ( cli_command_result ) ) <EOL> log . debug ( '<STR_LIT>' , cli_command_result [ '<STR_LIT>' ] ) <EOL> log . debug ( '<STR_LIT>' , cli_command_result [ '<STR_LIT>' ] ) <EOL> log . debug ( '<STR_LIT>' , cli_command_result [ '<STR_LIT>' ] ) <EOL> if cli_command_result [ '<STR_LIT>' ] == <NUM_LIT> : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( cli_path = jboss_config [ '<STR_LIT>' ] ) ) <EOL> if cli_command_result [ '<STR_LIT>' ] == <NUM_LIT:1> and '<STR_LIT>' in cli_command_result [ '<STR_LIT>' ] : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( ** cli_command_result ) ) <EOL> if cli_command_result [ '<STR_LIT>' ] == <NUM_LIT:1> and '<STR_LIT>' in cli_command_result [ '<STR_LIT>' ] and retries > <NUM_LIT:0> : <EOL> log . debug ( '<STR_LIT>' , retries ) <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> return __call_cli ( jboss_config , command , retries - <NUM_LIT:1> ) <EOL> return cli_command_result <EOL> def __escape_command ( command ) : <EOL> '''<STR_LIT>''' <EOL> result = command . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) <EOL> result = result . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> return result <EOL> def _is_cli_output ( text ) : <EOL> cli_re = re . compile ( r"<STR_LIT>" , re . DOTALL ) <EOL> if cli_re . search ( text ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def _parse ( cli_output ) : <EOL> tokens = __tokenize ( cli_output ) <EOL> result = __process_tokens ( tokens ) <EOL> log . debug ( "<STR_LIT>" + pprint . pformat ( result ) ) <EOL> return result <EOL> def __process_tokens ( tokens ) : <EOL> result , token_no = __process_tokens_internal ( tokens ) <EOL> return result <EOL> def __process_tokens_internal ( tokens , start_at = <NUM_LIT:0> ) : <EOL> if __is_dict_start ( tokens [ start_at ] ) and start_at == <NUM_LIT:0> : <EOL> return __process_tokens_internal ( tokens , start_at = <NUM_LIT:1> ) <EOL> log . debug ( "<STR_LIT>" + str ( start_at ) ) <EOL> token_no = start_at <EOL> result = { } <EOL> current_key = None <EOL> while token_no < len ( tokens ) : <EOL> token = tokens [ token_no ] <EOL> log . debug ( "<STR_LIT>" , token_no , token ) <EOL> if __is_quoted_string ( token ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> if current_key is None : <EOL> current_key = __get_quoted_string ( token ) <EOL> log . debug ( "<STR_LIT>" , current_key ) <EOL> else : <EOL> result [ current_key ] = __get_quoted_string ( token ) <EOL> log . debug ( "<STR_LIT>" , current_key , result [ current_key ] ) <EOL> current_key = None <EOL> elif __is_datatype ( token ) : <EOL> log . debug ( "<STR_LIT>" , token ) <EOL> result [ current_key ] = __get_datatype ( token ) <EOL> log . debug ( "<STR_LIT>" , current_key , str ( result [ current_key ] ) ) <EOL> current_key = None <EOL> elif __is_boolean ( token ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> result [ current_key ] = __get_boolean ( token ) <EOL> log . debug ( "<STR_LIT>" , current_key , str ( result [ current_key ] ) ) <EOL> current_key = None <EOL> elif __is_int ( token ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> result [ current_key ] = __get_int ( token ) <EOL> log . debug ( "<STR_LIT>" , current_key , str ( result [ current_key ] ) ) <EOL> current_key = None <EOL> elif __is_long ( token ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> result [ current_key ] = __get_long ( token ) <EOL> log . debug ( "<STR_LIT>" , current_key , str ( result [ current_key ] ) ) <EOL> current_key = None <EOL> elif __is_undefined ( token ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> log . debug ( "<STR_LIT>" , current_key ) <EOL> result [ current_key ] = None <EOL> current_key = None <EOL> elif __is_dict_start ( token ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> dict_value , token_no = __process_tokens_internal ( tokens , start_at = token_no + <NUM_LIT:1> ) <EOL> log . debug ( "<STR_LIT>" , dict_value ) <EOL> result [ current_key ] = dict_value <EOL> log . debug ( "<STR_LIT>" , current_key , str ( result [ current_key ] ) ) <EOL> current_key = None <EOL> elif __is_dict_end ( token ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> return result , token_no <EOL> elif __is_assignment ( token ) : <EOL> log . debug ( "<STR_LIT>" ) <EOL> is_assignment = True <EOL> else : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( token ) ) <EOL> token_no = token_no + <NUM_LIT:1> <EOL> def __tokenize ( cli_output ) : <EOL> tokens_re = re . compile ( r'<STR_LIT>' , re . DOTALL ) <EOL> tokens = tokens_re . findall ( cli_output ) <EOL> log . debug ( "<STR_LIT>" , str ( tokens ) ) <EOL> return tokens <EOL> def __is_dict_start ( token ) : <EOL> return token == '<STR_LIT:{>' <EOL> def __is_dict_end ( token ) : <EOL> return token == '<STR_LIT:}>' <EOL> def __is_boolean ( token ) : <EOL> return token == '<STR_LIT:true>' or token == '<STR_LIT:false>' <EOL> def __get_boolean ( token ) : <EOL> return token == '<STR_LIT:true>' <EOL> def __is_int ( token ) : <EOL> return token . isdigit ( ) <EOL> def __get_int ( token ) : <EOL> return int ( token ) <EOL> def __is_long ( token ) : <EOL> return token [ <NUM_LIT:0> : - <NUM_LIT:1> ] . isdigit ( ) and token [ - <NUM_LIT:1> ] == '<STR_LIT:L>' <EOL> def __get_long ( token ) : <EOL> if six . PY2 : <EOL> return long ( token [ <NUM_LIT:0> : - <NUM_LIT:1> ] ) <EOL> else : <EOL> return int ( token [ <NUM_LIT:0> : - <NUM_LIT:1> ] ) <EOL> def __is_datatype ( token ) : <EOL> return token in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __get_datatype ( token ) : <EOL> return token <EOL> def __is_undefined ( token ) : <EOL> return token == '<STR_LIT>' <EOL> def __is_quoted_string ( token ) : <EOL> return token [ <NUM_LIT:0> ] == '<STR_LIT:">' and token [ - <NUM_LIT:1> ] == '<STR_LIT:">' <EOL> def __get_quoted_string ( token ) : <EOL> result = token [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> result = result . replace ( '<STR_LIT>' , '<STR_LIT:\\>' ) <EOL> return result <EOL> def __is_assignment ( token ) : <EOL> return token == '<STR_LIT>' </s>
