<s> import json <EOL> import logging <EOL> import os <EOL> from awscli . clidriver import CLIOperationCaller <EOL> from awscli . customizations . emr import constants <EOL> from awscli . customizations . emr import exceptions <EOL> from botocore . exceptions import WaiterError , NoCredentialsError <EOL> from botocore import xform_name <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def parse_tags ( raw_tags_list ) : <EOL> tags_dict_list = [ ] <EOL> if raw_tags_list : <EOL> for tag in raw_tags_list : <EOL> if tag . find ( '<STR_LIT:=>' ) == - <NUM_LIT:1> : <EOL> key , value = tag , '<STR_LIT>' <EOL> else : <EOL> key , value = tag . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> tags_dict_list . append ( { '<STR_LIT>' : key , '<STR_LIT>' : value } ) <EOL> return tags_dict_list <EOL> def parse_key_value_string ( key_value_string ) : <EOL> key_value_list = [ ] <EOL> if key_value_string is not None : <EOL> raw_key_value_list = key_value_string . split ( '<STR_LIT:U+002C>' ) <EOL> for kv in raw_key_value_list : <EOL> if kv . find ( '<STR_LIT:=>' ) == - <NUM_LIT:1> : <EOL> key , value = kv , '<STR_LIT>' <EOL> else : <EOL> key , value = kv . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> key_value_list . append ( { '<STR_LIT>' : key , '<STR_LIT>' : value } ) <EOL> return key_value_list <EOL> else : <EOL> return None <EOL> def apply_boolean_options ( <EOL> true_option , true_option_name , false_option , false_option_name ) : <EOL> if true_option and false_option : <EOL> error_message = '<STR_LIT>' + true_option_name + '<STR_LIT>' + false_option_name + '<STR_LIT>' <EOL> raise ValueError ( error_message ) <EOL> elif true_option : <EOL> return True <EOL> else : <EOL> return False <EOL> def apply ( params , key , value ) : <EOL> if value : <EOL> params [ key ] = value <EOL> return params <EOL> def apply_dict ( params , key , value ) : <EOL> if value : <EOL> params [ key ] = value <EOL> return params <EOL> def apply_params ( src_params , src_key , dest_params , dest_key ) : <EOL> if src_key in src_params . keys ( ) and src_params [ src_key ] : <EOL> dest_params [ dest_key ] = src_params [ src_key ] <EOL> return dest_params <EOL> def build_step ( <EOL> jar , name = '<STR_LIT>' , <EOL> action_on_failure = constants . DEFAULT_FAILURE_ACTION , <EOL> args = None , <EOL> main_class = None , <EOL> properties = None ) : <EOL> check_required_field ( <EOL> structure = '<STR_LIT>' , name = '<STR_LIT>' , value = jar ) <EOL> step = { } <EOL> apply_dict ( step , '<STR_LIT:Name>' , name ) <EOL> apply_dict ( step , '<STR_LIT>' , action_on_failure ) <EOL> jar_config = { } <EOL> jar_config [ '<STR_LIT>' ] = jar <EOL> apply_dict ( jar_config , '<STR_LIT>' , args ) <EOL> apply_dict ( jar_config , '<STR_LIT>' , main_class ) <EOL> apply_dict ( jar_config , '<STR_LIT>' , properties ) <EOL> step [ '<STR_LIT>' ] = jar_config <EOL> return step <EOL> def build_bootstrap_action ( <EOL> path , <EOL> name = '<STR_LIT>' , <EOL> args = None ) : <EOL> if path is None : <EOL> raise exceptions . MissingParametersError ( <EOL> object_name = '<STR_LIT>' , missing = '<STR_LIT>' ) <EOL> ba_config = { } <EOL> apply_dict ( ba_config , '<STR_LIT:Name>' , name ) <EOL> script_config = { } <EOL> apply_dict ( script_config , '<STR_LIT>' , args ) <EOL> script_config [ '<STR_LIT>' ] = path <EOL> apply_dict ( ba_config , '<STR_LIT>' , script_config ) <EOL> return ba_config <EOL> def build_s3_link ( relative_path = '<STR_LIT>' , region = '<STR_LIT>' ) : <EOL> if region is None : <EOL> region = '<STR_LIT>' <EOL> return '<STR_LIT>' . format ( region , relative_path ) <EOL> def get_script_runner ( region = '<STR_LIT>' ) : <EOL> if region is None : <EOL> region = '<STR_LIT>' <EOL> return build_s3_link ( <EOL> relative_path = constants . SCRIPT_RUNNER_PATH , region = region ) <EOL> def check_required_field ( structure , name , value ) : <EOL> if not value : <EOL> raise exceptions . MissingParametersError ( <EOL> object_name = structure , missing = name ) <EOL> def check_empty_string_list ( name , value ) : <EOL> if not value or ( len ( value ) == <NUM_LIT:1> and value [ <NUM_LIT:0> ] . strip ( ) == "<STR_LIT>" ) : <EOL> raise exceptions . EmptyListError ( param = name ) <EOL> def call ( session , operation_name , parameters , region_name = None , <EOL> endpoint_url = None , verify = None ) : <EOL> if session . get_credentials ( ) is None : <EOL> raise NoCredentialsError ( ) <EOL> client = session . create_client ( <EOL> '<STR_LIT>' , region_name = region_name , endpoint_url = endpoint_url , <EOL> verify = verify ) <EOL> LOG . debug ( '<STR_LIT>' + str ( operation_name ) ) <EOL> return getattr ( client , operation_name ) ( ** parameters ) <EOL> def get_example_file ( command ) : <EOL> return open ( '<STR_LIT>' + command + '<STR_LIT>' ) <EOL> def dict_to_string ( dict , indent = <NUM_LIT:2> ) : <EOL> return json . dumps ( dict , indent = indent ) <EOL> def get_client ( session , parsed_globals ) : <EOL> return session . create_client ( <EOL> '<STR_LIT>' , <EOL> region_name = get_region ( session , parsed_globals ) , <EOL> endpoint_url = parsed_globals . endpoint_url , <EOL> verify = parsed_globals . verify_ssl ) <EOL> def get_cluster_state ( session , parsed_globals , cluster_id ) : <EOL> client = get_client ( session , parsed_globals ) <EOL> data = client . describe_cluster ( ClusterId = cluster_id ) <EOL> return data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def find_master_dns ( session , parsed_globals , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( session , parsed_globals ) <EOL> data = client . describe_cluster ( ClusterId = cluster_id ) <EOL> return data [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def which ( program ) : <EOL> for path in os . environ [ "<STR_LIT>" ] . split ( os . pathsep ) : <EOL> path = path . strip ( '<STR_LIT:">' ) <EOL> exe_file = os . path . join ( path , program ) <EOL> if os . path . isfile ( exe_file ) and os . access ( exe_file , os . X_OK ) : <EOL> return exe_file <EOL> return None <EOL> def call_and_display_response ( session , operation_name , parameters , <EOL> parsed_globals ) : <EOL> cli_operation_caller = CLIOperationCaller ( session ) <EOL> cli_operation_caller . invoke ( <EOL> '<STR_LIT>' , operation_name , <EOL> parameters , parsed_globals ) <EOL> def display_response ( session , operation_name , result , parsed_globals ) : <EOL> cli_operation_caller = CLIOperationCaller ( session ) <EOL> cli_operation_caller . _display_response ( <EOL> operation_name , result , parsed_globals ) <EOL> def get_region ( session , parsed_globals ) : <EOL> region = parsed_globals . region <EOL> if region is None : <EOL> region = session . get_config_variable ( '<STR_LIT>' ) <EOL> return region <EOL> def join ( values , separator = '<STR_LIT:U+002C>' , lastSeparator = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> values = [ str ( x ) for x in values ] <EOL> if len ( values ) < <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> elif len ( values ) is <NUM_LIT:1> : <EOL> return values [ <NUM_LIT:0> ] <EOL> else : <EOL> separator = '<STR_LIT>' % separator <EOL> return '<STR_LIT:U+0020>' . join ( [ separator . join ( values [ : - <NUM_LIT:1> ] ) , <EOL> lastSeparator , values [ - <NUM_LIT:1> ] ] ) <EOL> def split_to_key_value ( string ) : <EOL> if string . find ( '<STR_LIT:=>' ) == - <NUM_LIT:1> : <EOL> return string , '<STR_LIT>' <EOL> else : <EOL> return string . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> def get_cluster ( cluster_id , session , region , <EOL> endpoint_url , verify_ssl ) : <EOL> describe_cluster_params = { '<STR_LIT>' : cluster_id } <EOL> describe_cluster_response = call ( <EOL> session , '<STR_LIT>' , describe_cluster_params , <EOL> region , endpoint_url , <EOL> verify_ssl ) <EOL> if describe_cluster_response is not None : <EOL> return describe_cluster_response . get ( '<STR_LIT>' ) <EOL> def get_release_label ( cluster_id , session , region , <EOL> endpoint_url , verify_ssl ) : <EOL> cluster = get_cluster ( cluster_id , session , region , <EOL> endpoint_url , verify_ssl ) <EOL> if cluster is not None : <EOL> return cluster . get ( '<STR_LIT>' ) </s>
