<s> import os <EOL> import ast <EOL> import copy <EOL> import json <EOL> import logging <EOL> import textwrap <EOL> import calendar <EOL> import time <EOL> import six <EOL> import sys <EOL> from os . path import join as pjoin <EOL> from st2client import models <EOL> from st2client . commands import resource <EOL> from st2client . commands . resource import add_auth_token_to_kwargs_from_cli <EOL> from st2client . exceptions . operations import OperationFailureException <EOL> from st2client . formatters import table <EOL> from st2client . formatters import execution as execution_formatter <EOL> from st2client . utils import jsutil <EOL> from st2client . utils . date import format_isodate_for_user_timezone <EOL> from st2client . utils . date import parse as parse_isotime <EOL> from st2client . utils . color import format_status <EOL> LOG = logging . getLogger ( __name__ ) <EOL> LIVEACTION_STATUS_REQUESTED = '<STR_LIT>' <EOL> LIVEACTION_STATUS_SCHEDULED = '<STR_LIT>' <EOL> LIVEACTION_STATUS_DELAYED = '<STR_LIT>' <EOL> LIVEACTION_STATUS_RUNNING = '<STR_LIT>' <EOL> LIVEACTION_STATUS_SUCCEEDED = '<STR_LIT>' <EOL> LIVEACTION_STATUS_FAILED = '<STR_LIT>' <EOL> LIVEACTION_STATUS_TIMED_OUT = '<STR_LIT>' <EOL> LIVEACTION_STATUS_ABANDONED = '<STR_LIT>' <EOL> LIVEACTION_STATUS_CANCELING = '<STR_LIT>' <EOL> LIVEACTION_STATUS_CANCELED = '<STR_LIT>' <EOL> LIVEACTION_COMPLETED_STATES = [ <EOL> LIVEACTION_STATUS_SUCCEEDED , <EOL> LIVEACTION_STATUS_FAILED , <EOL> LIVEACTION_STATUS_TIMED_OUT , <EOL> LIVEACTION_STATUS_CANCELED , <EOL> LIVEACTION_STATUS_ABANDONED <EOL> ] <EOL> PARAMETERS_TO_MASK = [ <EOL> '<STR_LIT:password>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> ENV_VARS_BLACKLIST = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT:user>' , <EOL> '<STR_LIT:path>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> WORKFLOW_RUNNER_TYPES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def format_parameters ( value ) : <EOL> if not isinstance ( value , dict ) : <EOL> return value <EOL> for param_name , _ in value . items ( ) : <EOL> if param_name in PARAMETERS_TO_MASK : <EOL> value [ param_name ] = '<STR_LIT>' <EOL> return value <EOL> WF_PREFIX = '<STR_LIT>' <EOL> NON_WF_PREFIX = '<STR_LIT:U+0020>' <EOL> INDENT_CHAR = '<STR_LIT:U+0020>' <EOL> def format_wf_instances ( instances ) : <EOL> """<STR_LIT>""" <EOL> has_wf = False <EOL> for instance in instances : <EOL> if not getattr ( instance , '<STR_LIT>' , None ) : <EOL> continue <EOL> else : <EOL> has_wf = True <EOL> break <EOL> if not has_wf : <EOL> return instances <EOL> for instance in instances : <EOL> if getattr ( instance , '<STR_LIT>' , None ) : <EOL> instance . id = WF_PREFIX + instance . id <EOL> else : <EOL> instance . id = NON_WF_PREFIX + instance . id <EOL> return instances <EOL> def format_execution_statuses ( instances ) : <EOL> result = [ ] <EOL> for instance in instances : <EOL> instance = format_execution_status ( instance ) <EOL> result . append ( instance ) <EOL> return result <EOL> def format_execution_status ( instance ) : <EOL> """<STR_LIT>""" <EOL> start_timestamp = getattr ( instance , '<STR_LIT>' , None ) <EOL> end_timestamp = getattr ( instance , '<STR_LIT>' , None ) <EOL> if instance . status == LIVEACTION_STATUS_RUNNING and start_timestamp : <EOL> start_timestamp = instance . start_timestamp <EOL> start_timestamp = parse_isotime ( start_timestamp ) <EOL> start_timestamp = calendar . timegm ( start_timestamp . timetuple ( ) ) <EOL> now = int ( time . time ( ) ) <EOL> elapsed_seconds = ( now - start_timestamp ) <EOL> instance . status = '<STR_LIT>' % ( instance . status , elapsed_seconds ) <EOL> elif instance . status in LIVEACTION_COMPLETED_STATES and start_timestamp and end_timestamp : <EOL> start_timestamp = parse_isotime ( start_timestamp ) <EOL> start_timestamp = calendar . timegm ( start_timestamp . timetuple ( ) ) <EOL> end_timestamp = parse_isotime ( end_timestamp ) <EOL> end_timestamp = calendar . timegm ( end_timestamp . timetuple ( ) ) <EOL> elapsed_seconds = ( end_timestamp - start_timestamp ) <EOL> instance . status = '<STR_LIT>' % ( instance . status , elapsed_seconds ) <EOL> return instance <EOL> class ActionBranch ( resource . ResourceBranch ) : <EOL> def __init__ ( self , description , app , subparsers , parent_parser = None ) : <EOL> super ( ActionBranch , self ) . __init__ ( <EOL> models . Action , description , app , subparsers , <EOL> parent_parser = parent_parser , <EOL> commands = { <EOL> '<STR_LIT:list>' : ActionListCommand , <EOL> '<STR_LIT>' : ActionGetCommand , <EOL> '<STR_LIT>' : ActionUpdateCommand , <EOL> '<STR_LIT>' : ActionDeleteCommand <EOL> } ) <EOL> self . commands [ '<STR_LIT>' ] = ActionEnableCommand ( self . resource , self . app , self . subparsers ) <EOL> self . commands [ '<STR_LIT>' ] = ActionDisableCommand ( self . resource , self . app , self . subparsers ) <EOL> self . commands [ '<STR_LIT>' ] = ActionRunCommand ( <EOL> self . resource , self . app , self . subparsers , <EOL> add_help = False ) <EOL> class ActionListCommand ( resource . ContentPackResourceListCommand ) : <EOL> display_attributes = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:description>' ] <EOL> class ActionGetCommand ( resource . ContentPackResourceGetCommand ) : <EOL> display_attributes = [ '<STR_LIT:all>' ] <EOL> attribute_display_order = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT:description>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class ActionUpdateCommand ( resource . ContentPackResourceUpdateCommand ) : <EOL> pass <EOL> class ActionEnableCommand ( resource . ContentPackResourceEnableCommand ) : <EOL> display_attributes = [ '<STR_LIT:all>' ] <EOL> attribute_display_order = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT:description>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class ActionDisableCommand ( resource . ContentPackResourceDisableCommand ) : <EOL> display_attributes = [ '<STR_LIT:all>' ] <EOL> attribute_display_order = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT:description>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class ActionDeleteCommand ( resource . ContentPackResourceDeleteCommand ) : <EOL> pass <EOL> class ActionRunCommandMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> display_attributes = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:result>' ] <EOL> attribute_display_order = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:result>' ] <EOL> attribute_transform_functions = { <EOL> '<STR_LIT>' : format_isodate_for_user_timezone , <EOL> '<STR_LIT>' : format_isodate_for_user_timezone , <EOL> '<STR_LIT>' : format_parameters , <EOL> '<STR_LIT:status>' : format_status <EOL> } <EOL> poll_interval = <NUM_LIT:2> <EOL> def get_resource ( self , ref_or_id , ** kwargs ) : <EOL> return self . get_resource_by_ref_or_id ( ref_or_id = ref_or_id , ** kwargs ) <EOL> @ add_auth_token_to_kwargs_from_cli <EOL> def run_and_print ( self , args , ** kwargs ) : <EOL> if self . _print_help ( args , ** kwargs ) : <EOL> return <EOL> execution = self . run ( args , ** kwargs ) <EOL> if args . async : <EOL> self . print_output ( '<STR_LIT>' % <EOL> ( execution . id ) , six . text_type ) <EOL> else : <EOL> self . _print_execution_details ( execution = execution , args = args , ** kwargs ) <EOL> if execution . status == '<STR_LIT>' : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def _add_common_options ( self ) : <EOL> root_arg_grp = self . parser . add_mutually_exclusive_group ( ) <EOL> task_list_arg_grp = root_arg_grp . add_argument_group ( ) <EOL> task_list_arg_grp . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> task_list_arg_grp . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> task_list_arg_grp . add_argument ( '<STR_LIT>' , type = int , default = - <NUM_LIT:1> , <EOL> help = '<STR_LIT>' ) <EOL> task_list_arg_grp . add_argument ( '<STR_LIT>' , '<STR_LIT>' , nargs = '<STR_LIT:+>' , type = int , default = None , <EOL> help = '<STR_LIT>' ) <EOL> execution_details_arg_grp = root_arg_grp . add_mutually_exclusive_group ( ) <EOL> detail_arg_grp = execution_details_arg_grp . add_mutually_exclusive_group ( ) <EOL> detail_arg_grp . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:+>' , <EOL> default = [ '<STR_LIT:id>' , '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT:result>' ] , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> detail_arg_grp . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> result_arg_grp = execution_details_arg_grp . add_mutually_exclusive_group ( ) <EOL> result_arg_grp . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return root_arg_grp <EOL> def _print_execution_details ( self , execution , args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> runner_type = execution . action . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> is_workflow_action = runner_type in WORKFLOW_RUNNER_TYPES <EOL> show_tasks = getattr ( args , '<STR_LIT>' , False ) <EOL> raw = getattr ( args , '<STR_LIT>' , False ) <EOL> detail = getattr ( args , '<STR_LIT>' , False ) <EOL> key = getattr ( args , '<STR_LIT:key>' , None ) <EOL> attr = getattr ( args , '<STR_LIT>' , [ ] ) <EOL> if show_tasks and not is_workflow_action : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not raw and not detail and ( show_tasks or is_workflow_action ) : <EOL> self . _run_and_print_child_task_list ( execution = execution , args = args , ** kwargs ) <EOL> else : <EOL> instance = execution <EOL> if detail : <EOL> formatter = table . PropertyValueTable <EOL> else : <EOL> formatter = execution_formatter . ExecutionResult <EOL> if detail : <EOL> options = { '<STR_LIT>' : copy . copy ( self . display_attributes ) } <EOL> elif key : <EOL> options = { '<STR_LIT>' : [ '<STR_LIT>' % ( key ) ] , '<STR_LIT:key>' : key } <EOL> else : <EOL> options = { '<STR_LIT>' : attr } <EOL> options [ '<STR_LIT>' ] = args . json <EOL> options [ '<STR_LIT>' ] = self . attribute_transform_functions <EOL> self . print_output ( instance , formatter , ** options ) <EOL> def _run_and_print_child_task_list ( self , execution , args , ** kwargs ) : <EOL> action_exec_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> instance = execution <EOL> options = { '<STR_LIT>' : [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] } <EOL> options [ '<STR_LIT>' ] = args . json <EOL> options [ '<STR_LIT>' ] = self . attribute_transform_functions <EOL> formatter = execution_formatter . ExecutionResult <EOL> kwargs [ '<STR_LIT>' ] = args . depth <EOL> child_instances = action_exec_mgr . get_property ( execution . id , '<STR_LIT>' , ** kwargs ) <EOL> child_instances = self . _format_child_instances ( child_instances , execution . id ) <EOL> child_instances = format_execution_statuses ( child_instances ) <EOL> if not child_instances : <EOL> options [ '<STR_LIT>' ] . append ( '<STR_LIT:result>' ) <EOL> if instance . status == '<STR_LIT>' : <EOL> status_index = options [ '<STR_LIT>' ] . index ( '<STR_LIT:status>' ) <EOL> if isinstance ( instance . result , dict ) : <EOL> tasks = instance . result . get ( '<STR_LIT>' , [ ] ) <EOL> else : <EOL> tasks = [ ] <EOL> top_level_error , top_level_traceback = self . _get_top_level_error ( live_action = instance ) <EOL> if len ( tasks ) >= <NUM_LIT:1> : <EOL> task_error , task_traceback = self . _get_task_error ( task = tasks [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> task_error , task_traceback = None , None <EOL> if top_level_error : <EOL> instance . error = top_level_error <EOL> instance . traceback = top_level_traceback <EOL> instance . result = '<STR_LIT>' <EOL> options [ '<STR_LIT>' ] . insert ( status_index + <NUM_LIT:1> , '<STR_LIT:error>' ) <EOL> options [ '<STR_LIT>' ] . insert ( status_index + <NUM_LIT:2> , '<STR_LIT>' ) <EOL> elif task_error : <EOL> instance . error = task_error <EOL> instance . traceback = task_traceback <EOL> instance . result = '<STR_LIT>' <EOL> instance . failed_on = tasks [ - <NUM_LIT:1> ] . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> options [ '<STR_LIT>' ] . insert ( status_index + <NUM_LIT:1> , '<STR_LIT:error>' ) <EOL> options [ '<STR_LIT>' ] . insert ( status_index + <NUM_LIT:2> , '<STR_LIT>' ) <EOL> options [ '<STR_LIT>' ] . insert ( status_index + <NUM_LIT:3> , '<STR_LIT>' ) <EOL> self . print_output ( instance , formatter , ** options ) <EOL> if child_instances : <EOL> self . print_output ( child_instances , table . MultiColumnTable , <EOL> attributes = [ '<STR_LIT:id>' , '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT:action>' , '<STR_LIT>' ] , <EOL> widths = args . width , json = args . json , <EOL> attribute_transform_functions = self . attribute_transform_functions ) <EOL> def _get_execution_result ( self , execution , action_exec_mgr , args , ** kwargs ) : <EOL> pending_statuses = [ <EOL> LIVEACTION_STATUS_REQUESTED , <EOL> LIVEACTION_STATUS_SCHEDULED , <EOL> LIVEACTION_STATUS_RUNNING , <EOL> LIVEACTION_STATUS_CANCELING <EOL> ] <EOL> if not args . async : <EOL> while execution . status in pending_statuses : <EOL> time . sleep ( self . poll_interval ) <EOL> if not args . json : <EOL> sys . stdout . write ( '<STR_LIT:.>' ) <EOL> sys . stdout . flush ( ) <EOL> execution = action_exec_mgr . get_by_id ( execution . id , ** kwargs ) <EOL> sys . stdout . write ( '<STR_LIT:\n>' ) <EOL> if execution . status == LIVEACTION_STATUS_CANCELED : <EOL> return execution <EOL> return execution <EOL> def _get_top_level_error ( self , live_action ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( live_action . result , dict ) : <EOL> error = live_action . result . get ( '<STR_LIT:error>' , None ) <EOL> traceback = live_action . result . get ( '<STR_LIT>' , None ) <EOL> else : <EOL> error = "<STR_LIT>" <EOL> traceback = "<STR_LIT>" <EOL> return error , traceback <EOL> def _get_task_error ( self , task ) : <EOL> """<STR_LIT>""" <EOL> if not task : <EOL> return None , None <EOL> result = task [ '<STR_LIT:result>' ] <EOL> if isinstance ( result , dict ) : <EOL> stderr = result . get ( '<STR_LIT>' , None ) <EOL> error = result . get ( '<STR_LIT:error>' , None ) <EOL> traceback = result . get ( '<STR_LIT>' , None ) <EOL> error = error if error else stderr <EOL> else : <EOL> stderr = None <EOL> error = None <EOL> traceback = None <EOL> return error , traceback <EOL> def _get_action_parameters_from_args ( self , action , runner , args ) : <EOL> """<STR_LIT>""" <EOL> action_ref_or_id = action . ref <EOL> def read_file ( file_path ) : <EOL> if not os . path . exists ( file_path ) : <EOL> raise ValueError ( '<STR_LIT>' % ( file_path ) ) <EOL> if not os . path . isfile ( file_path ) : <EOL> raise ValueError ( '<STR_LIT>' % ( file_path ) ) <EOL> with open ( file_path , '<STR_LIT:rb>' ) as fp : <EOL> content = fp . read ( ) <EOL> return content <EOL> def transform_object ( value ) : <EOL> if value . startswith ( '<STR_LIT:{>' ) : <EOL> result = value = json . loads ( value ) <EOL> else : <EOL> pairs = value . split ( '<STR_LIT:U+002C>' ) <EOL> result = { } <EOL> for pair in pairs : <EOL> split = pair . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( split ) != <NUM_LIT:2> : <EOL> continue <EOL> key , value = split <EOL> result [ key ] = value <EOL> return result <EOL> transformer = { <EOL> '<STR_LIT>' : ( lambda cs_x : [ v . strip ( ) for v in cs_x . split ( '<STR_LIT:U+002C>' ) ] ) , <EOL> '<STR_LIT>' : ( lambda x : ast . literal_eval ( x . capitalize ( ) ) ) , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : float , <EOL> '<STR_LIT:object>' : transform_object , <EOL> '<STR_LIT:string>' : str <EOL> } <EOL> def normalize ( name , value ) : <EOL> if name in runner . runner_parameters : <EOL> param = runner . runner_parameters [ name ] <EOL> if '<STR_LIT:type>' in param and param [ '<STR_LIT:type>' ] in transformer : <EOL> return transformer [ param [ '<STR_LIT:type>' ] ] ( value ) <EOL> if name in action . parameters : <EOL> param = action . parameters [ name ] <EOL> if '<STR_LIT:type>' in param and param [ '<STR_LIT:type>' ] in transformer : <EOL> return transformer [ param [ '<STR_LIT:type>' ] ] ( value ) <EOL> return value <EOL> result = { } <EOL> if not args . parameters : <EOL> return result <EOL> for idx in range ( len ( args . parameters ) ) : <EOL> arg = args . parameters [ idx ] <EOL> if '<STR_LIT:=>' in arg : <EOL> k , v = arg . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if k . startswith ( '<STR_LIT:@>' ) : <EOL> k = k [ <NUM_LIT:1> : ] <EOL> is_file = True <EOL> else : <EOL> is_file = False <EOL> try : <EOL> if is_file : <EOL> file_path = os . path . normpath ( pjoin ( os . getcwd ( ) , v ) ) <EOL> file_name = os . path . basename ( file_path ) <EOL> content = read_file ( file_path = file_path ) <EOL> if action_ref_or_id == '<STR_LIT>' : <EOL> result [ '<STR_LIT>' ] = file_name <EOL> result [ '<STR_LIT>' ] = content <EOL> else : <EOL> result [ k ] = content <EOL> else : <EOL> result [ k ] = normalize ( k , v ) <EOL> except Exception as e : <EOL> if '<STR_LIT>' in str ( e ) : <EOL> message = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise ValueError ( message ) <EOL> else : <EOL> raise e <EOL> else : <EOL> result [ '<STR_LIT>' ] = '<STR_LIT:U+0020>' . join ( args . parameters [ idx : ] ) <EOL> break <EOL> if '<STR_LIT>' in result : <EOL> if '<STR_LIT>' not in result : <EOL> result [ '<STR_LIT>' ] = '<STR_LIT:POST>' <EOL> if '<STR_LIT>' not in result : <EOL> result [ '<STR_LIT>' ] = result [ '<STR_LIT>' ] <EOL> del result [ '<STR_LIT>' ] <EOL> if args . inherit_env : <EOL> result [ '<STR_LIT>' ] = self . _get_inherited_env_vars ( ) <EOL> return result <EOL> @ add_auth_token_to_kwargs_from_cli <EOL> def _print_help ( self , args , ** kwargs ) : <EOL> action_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> action_exec_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> if args . help : <EOL> action_ref_or_id = getattr ( args , '<STR_LIT>' , None ) <EOL> action_exec_id = getattr ( args , '<STR_LIT:id>' , None ) <EOL> if action_exec_id and not action_ref_or_id : <EOL> action_exec = action_exec_mgr . get_by_id ( action_exec_id , ** kwargs ) <EOL> args . ref_or_id = action_exec . action <EOL> if action_ref_or_id : <EOL> try : <EOL> action = action_mgr . get_by_ref_or_id ( args . ref_or_id , ** kwargs ) <EOL> if not action : <EOL> raise resource . ResourceNotFoundError ( '<STR_LIT>' , args . ref_or_id ) <EOL> runner_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> runner = runner_mgr . get_by_name ( action . runner_type , ** kwargs ) <EOL> parameters , required , optional , _ = self . _get_params_types ( runner , <EOL> action ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( textwrap . fill ( action . description ) ) <EOL> print ( '<STR_LIT>' ) <EOL> if required : <EOL> required = self . _sort_parameters ( parameters = parameters , <EOL> names = required ) <EOL> print ( '<STR_LIT>' ) <EOL> [ self . _print_param ( name , parameters . get ( name ) ) <EOL> for name in required ] <EOL> if optional : <EOL> optional = self . _sort_parameters ( parameters = parameters , <EOL> names = optional ) <EOL> print ( '<STR_LIT>' ) <EOL> [ self . _print_param ( name , parameters . get ( name ) ) <EOL> for name in optional ] <EOL> except resource . ResourceNotFoundError : <EOL> print ( ( '<STR_LIT>' % args . ref_or_id ) + <EOL> '<STR_LIT>' ) <EOL> except Exception as e : <EOL> print ( '<STR_LIT>' % <EOL> ( args . ref_or_id , e ) ) <EOL> else : <EOL> self . parser . print_help ( ) <EOL> return True <EOL> return False <EOL> @ staticmethod <EOL> def _print_param ( name , schema ) : <EOL> if not schema : <EOL> raise ValueError ( '<STR_LIT>' % ( name ) ) <EOL> wrapper = textwrap . TextWrapper ( width = <NUM_LIT> ) <EOL> wrapper . initial_indent = '<STR_LIT:U+0020>' * <NUM_LIT:4> <EOL> wrapper . subsequent_indent = wrapper . initial_indent <EOL> print ( wrapper . fill ( name ) ) <EOL> wrapper . initial_indent = '<STR_LIT:U+0020>' * <NUM_LIT:8> <EOL> wrapper . subsequent_indent = wrapper . initial_indent <EOL> if '<STR_LIT:description>' in schema and schema [ '<STR_LIT:description>' ] : <EOL> print ( wrapper . fill ( schema [ '<STR_LIT:description>' ] ) ) <EOL> if '<STR_LIT:type>' in schema and schema [ '<STR_LIT:type>' ] : <EOL> print ( wrapper . fill ( '<STR_LIT>' % schema [ '<STR_LIT:type>' ] ) ) <EOL> if '<STR_LIT>' in schema and schema [ '<STR_LIT>' ] : <EOL> print ( wrapper . fill ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( schema [ '<STR_LIT>' ] ) ) ) <EOL> if '<STR_LIT:default>' in schema and schema [ '<STR_LIT:default>' ] is not None : <EOL> print ( wrapper . fill ( '<STR_LIT>' % schema [ '<STR_LIT:default>' ] ) ) <EOL> print ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def _get_params_types ( runner , action ) : <EOL> runner_params = runner . runner_parameters <EOL> action_params = action . parameters <EOL> parameters = copy . copy ( runner_params ) <EOL> parameters . update ( copy . copy ( action_params ) ) <EOL> required = set ( [ k for k , v in six . iteritems ( parameters ) if v . get ( '<STR_LIT>' ) ] ) <EOL> def is_immutable ( runner_param_meta , action_param_meta ) : <EOL> if runner_param_meta . get ( '<STR_LIT>' , False ) : <EOL> return True <EOL> else : <EOL> return action_param_meta . get ( '<STR_LIT>' , False ) <EOL> immutable = set ( ) <EOL> for param in parameters . keys ( ) : <EOL> if is_immutable ( runner_params . get ( param , { } ) , <EOL> action_params . get ( param , { } ) ) : <EOL> immutable . add ( param ) <EOL> required = required - immutable <EOL> optional = set ( parameters . keys ( ) ) - required - immutable <EOL> return parameters , required , optional , immutable <EOL> def _format_child_instances ( self , children , parent_id ) : <EOL> '''<STR_LIT>''' <EOL> children = format_wf_instances ( children ) <EOL> depth = { parent_id : <NUM_LIT:0> } <EOL> result = [ ] <EOL> for child in children : <EOL> if child . parent not in depth : <EOL> parent = None <EOL> for instance in children : <EOL> if WF_PREFIX in instance . id : <EOL> instance_id = instance . id [ instance . id . index ( WF_PREFIX ) + len ( WF_PREFIX ) : ] <EOL> else : <EOL> instance_id = instance . id <EOL> if instance_id == child . parent : <EOL> parent = instance <EOL> if parent and parent . parent and parent . parent in depth : <EOL> depth [ child . parent ] = depth [ parent . parent ] + <NUM_LIT:1> <EOL> else : <EOL> depth [ child . parent ] = <NUM_LIT:0> <EOL> child . id = INDENT_CHAR * depth [ child . parent ] + child . id <EOL> result . append ( self . _format_for_common_representation ( child ) ) <EOL> return result <EOL> def _format_for_common_representation ( self , task ) : <EOL> '''<STR_LIT>''' <EOL> context = getattr ( task , '<STR_LIT>' , None ) <EOL> if context and '<STR_LIT>' in context : <EOL> task_name_key = '<STR_LIT>' <EOL> elif context and '<STR_LIT>' in context : <EOL> task_name_key = '<STR_LIT>' <EOL> return models . action . LiveAction ( ** { <EOL> '<STR_LIT:id>' : task . id , <EOL> '<STR_LIT:status>' : task . status , <EOL> '<STR_LIT>' : jsutil . get_value ( vars ( task ) , task_name_key ) , <EOL> '<STR_LIT:action>' : task . action . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : task . start_timestamp , <EOL> '<STR_LIT>' : getattr ( task , '<STR_LIT>' , None ) <EOL> } ) <EOL> def _sort_parameters ( self , parameters , names ) : <EOL> """<STR_LIT>""" <EOL> sorted_parameters = sorted ( names , key = lambda name : <EOL> self . _get_parameter_sort_value ( <EOL> parameters = parameters , <EOL> name = name ) ) <EOL> return sorted_parameters <EOL> def _get_parameter_sort_value ( self , parameters , name ) : <EOL> """<STR_LIT>""" <EOL> parameter = parameters . get ( name , None ) <EOL> if not parameter : <EOL> return None <EOL> sort_value = parameter . get ( '<STR_LIT>' , name ) <EOL> return sort_value <EOL> def _get_inherited_env_vars ( self ) : <EOL> env_vars = os . environ . copy ( ) <EOL> for var_name in ENV_VARS_BLACKLIST : <EOL> if var_name . lower ( ) in env_vars : <EOL> del env_vars [ var_name . lower ( ) ] <EOL> if var_name . upper ( ) in env_vars : <EOL> del env_vars [ var_name . upper ( ) ] <EOL> return env_vars <EOL> class ActionRunCommand ( ActionRunCommandMixin , resource . ResourceCommand ) : <EOL> def __init__ ( self , resource , * args , ** kwargs ) : <EOL> super ( ActionRunCommand , self ) . __init__ ( <EOL> resource , kwargs . pop ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> * args , ** kwargs ) <EOL> self . parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> self . _add_common_options ( ) <EOL> if self . name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , required = False ) <EOL> self . parser . add_argument ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , required = False ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if self . name == '<STR_LIT>' : <EOL> self . parser . set_defaults ( async = False ) <EOL> else : <EOL> self . parser . set_defaults ( async = True ) <EOL> @ add_auth_token_to_kwargs_from_cli <EOL> def run ( self , args , ** kwargs ) : <EOL> if not args . ref_or_id : <EOL> self . parser . error ( '<STR_LIT>' ) <EOL> action = self . get_resource ( args . ref_or_id , ** kwargs ) <EOL> if not action : <EOL> raise resource . ResourceNotFoundError ( '<STR_LIT>' <EOL> % ( args . ref_or_id ) ) <EOL> runner_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> runner = runner_mgr . get_by_name ( action . runner_type , ** kwargs ) <EOL> if not runner : <EOL> raise resource . ResourceNotFoundError ( '<STR_LIT>' <EOL> % ( action . runner_type , action . name ) ) <EOL> action_ref = '<STR_LIT:.>' . join ( [ action . pack , action . name ] ) <EOL> action_parameters = self . _get_action_parameters_from_args ( action = action , runner = runner , <EOL> args = args ) <EOL> execution = models . LiveAction ( ) <EOL> execution . action = action_ref <EOL> execution . parameters = action_parameters <EOL> if not args . trace_id and args . trace_tag : <EOL> execution . context = { '<STR_LIT>' : { '<STR_LIT>' : args . trace_tag } } <EOL> if args . trace_id : <EOL> execution . context = { '<STR_LIT>' : { '<STR_LIT>' : args . trace_id } } <EOL> action_exec_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> execution = action_exec_mgr . create ( execution , ** kwargs ) <EOL> execution = self . _get_execution_result ( execution = execution , <EOL> action_exec_mgr = action_exec_mgr , <EOL> args = args , ** kwargs ) <EOL> return execution <EOL> class ActionExecutionBranch ( resource . ResourceBranch ) : <EOL> def __init__ ( self , description , app , subparsers , parent_parser = None ) : <EOL> super ( ActionExecutionBranch , self ) . __init__ ( <EOL> models . LiveAction , description , app , subparsers , <EOL> parent_parser = parent_parser , read_only = True , <EOL> commands = { '<STR_LIT:list>' : ActionExecutionListCommand , <EOL> '<STR_LIT>' : ActionExecutionGetCommand } ) <EOL> self . commands [ '<STR_LIT>' ] = ActionExecutionReRunCommand ( self . resource , self . app , <EOL> self . subparsers , add_help = False ) <EOL> self . commands [ '<STR_LIT>' ] = ActionExecutionCancelCommand ( self . resource , self . app , <EOL> self . subparsers , add_help = False ) <EOL> POSSIBLE_ACTION_STATUS_VALUES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ActionExecutionReadCommand ( resource . ResourceCommand ) : <EOL> """<STR_LIT>""" <EOL> def _get_exclude_attributes ( self , args ) : <EOL> """<STR_LIT>""" <EOL> exclude_attributes = [ ] <EOL> if '<STR_LIT:result>' not in args . attr : <EOL> exclude_attributes . append ( '<STR_LIT:result>' ) <EOL> if '<STR_LIT>' not in args . attr : <EOL> exclude_attributes . append ( '<STR_LIT>' ) <EOL> return exclude_attributes <EOL> class ActionExecutionListCommand ( ActionExecutionReadCommand ) : <EOL> display_attributes = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> attribute_transform_functions = { <EOL> '<STR_LIT>' : format_isodate_for_user_timezone , <EOL> '<STR_LIT>' : format_isodate_for_user_timezone , <EOL> '<STR_LIT>' : format_parameters , <EOL> '<STR_LIT:status>' : format_status <EOL> } <EOL> def __init__ ( self , resource , * args , ** kwargs ) : <EOL> super ( ActionExecutionListCommand , self ) . __init__ ( <EOL> resource , '<STR_LIT:list>' , '<STR_LIT>' % <EOL> resource . get_plural_display_name ( ) . lower ( ) , <EOL> * args , ** kwargs ) <EOL> self . group = self . parser . add_argument_group ( ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , dest = '<STR_LIT>' , <EOL> default = <NUM_LIT:50> , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> resource . get_plural_display_name ( ) . lower ( ) ) ) <EOL> self . group . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> self . group . add_argument ( '<STR_LIT>' , help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:.>' % POSSIBLE_ACTION_STATUS_VALUES ) ) <EOL> self . group . add_argument ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , dest = '<STR_LIT>' , <EOL> default = None , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , dest = '<STR_LIT>' , <EOL> default = None , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , nargs = '<STR_LIT:+>' , <EOL> default = self . display_attributes , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , nargs = '<STR_LIT:+>' , type = int , <EOL> default = None , <EOL> help = ( '<STR_LIT>' ) ) <EOL> @ add_auth_token_to_kwargs_from_cli <EOL> def run ( self , args , ** kwargs ) : <EOL> if args . action : <EOL> kwargs [ '<STR_LIT:action>' ] = args . action <EOL> if args . status : <EOL> kwargs [ '<STR_LIT:status>' ] = args . status <EOL> if args . trigger_instance : <EOL> kwargs [ '<STR_LIT>' ] = args . trigger_instance <EOL> if not args . showall : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:null>' <EOL> if args . timestamp_gt : <EOL> kwargs [ '<STR_LIT>' ] = args . timestamp_gt <EOL> if args . timestamp_lt : <EOL> kwargs [ '<STR_LIT>' ] = args . timestamp_lt <EOL> exclude_attributes = self . _get_exclude_attributes ( args = args ) <EOL> exclude_attributes = '<STR_LIT:U+002C>' . join ( exclude_attributes ) <EOL> kwargs [ '<STR_LIT>' ] = exclude_attributes <EOL> return self . manager . query ( limit = args . last , ** kwargs ) <EOL> def run_and_print ( self , args , ** kwargs ) : <EOL> instances = format_wf_instances ( self . run ( args , ** kwargs ) ) <EOL> if not args . json : <EOL> instances = format_execution_statuses ( instances ) <EOL> self . print_output ( reversed ( instances ) , table . MultiColumnTable , <EOL> attributes = args . attr , widths = args . width , <EOL> json = args . json , <EOL> attribute_transform_functions = self . attribute_transform_functions ) <EOL> class ActionExecutionGetCommand ( ActionRunCommandMixin , ActionExecutionReadCommand ) : <EOL> display_attributes = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:status>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:result>' , '<STR_LIT>' ] <EOL> def __init__ ( self , resource , * args , ** kwargs ) : <EOL> super ( ActionExecutionGetCommand , self ) . __init__ ( <EOL> resource , '<STR_LIT>' , <EOL> '<STR_LIT>' % resource . get_display_name ( ) . lower ( ) , <EOL> * args , ** kwargs ) <EOL> self . parser . add_argument ( '<STR_LIT:id>' , <EOL> help = ( '<STR_LIT>' % <EOL> resource . get_display_name ( ) . lower ( ) ) ) <EOL> self . _add_common_options ( ) <EOL> @ add_auth_token_to_kwargs_from_cli <EOL> def run ( self , args , ** kwargs ) : <EOL> exclude_attributes = self . _get_exclude_attributes ( args = args ) <EOL> exclude_attributes = '<STR_LIT:U+002C>' . join ( exclude_attributes ) <EOL> kwargs [ '<STR_LIT>' ] = { '<STR_LIT>' : exclude_attributes } <EOL> execution = self . get_resource_by_id ( id = args . id , ** kwargs ) <EOL> return execution <EOL> @ add_auth_token_to_kwargs_from_cli <EOL> def run_and_print ( self , args , ** kwargs ) : <EOL> try : <EOL> execution = self . run ( args , ** kwargs ) <EOL> if not args . json : <EOL> execution = format_execution_status ( execution ) <EOL> except resource . ResourceNotFoundError : <EOL> self . print_not_found ( args . id ) <EOL> raise OperationFailureException ( '<STR_LIT>' % ( args . id ) ) <EOL> return self . _print_execution_details ( execution = execution , args = args , ** kwargs ) <EOL> class ActionExecutionCancelCommand ( resource . ResourceCommand ) : <EOL> def __init__ ( self , resource , * args , ** kwargs ) : <EOL> super ( ActionExecutionCancelCommand , self ) . __init__ ( <EOL> resource , '<STR_LIT>' , '<STR_LIT>' % <EOL> resource . get_plural_display_name ( ) . lower ( ) , <EOL> * args , ** kwargs ) <EOL> self . parser . add_argument ( '<STR_LIT:id>' , <EOL> help = ( '<STR_LIT>' % <EOL> resource . get_display_name ( ) . lower ( ) ) ) <EOL> def run ( self , args , ** kwargs ) : <EOL> return self . manager . delete_by_id ( args . id ) <EOL> @ add_auth_token_to_kwargs_from_cli <EOL> def run_and_print ( self , args , ** kwargs ) : <EOL> response = self . run ( args , ** kwargs ) <EOL> if response and '<STR_LIT>' in response : <EOL> message = response . get ( '<STR_LIT>' , '<STR_LIT>' % <EOL> ( self . resource . get_display_name ( ) . lower ( ) , args . id ) ) <EOL> elif response : <EOL> message = '<STR_LIT>' % ( self . resource . get_display_name ( ) . lower ( ) , <EOL> args . id ) <EOL> else : <EOL> message = '<STR_LIT>' % ( self . resource . get_display_name ( ) . lower ( ) , <EOL> args . id ) <EOL> print ( message ) <EOL> class ActionExecutionReRunCommand ( ActionRunCommandMixin , resource . ResourceCommand ) : <EOL> def __init__ ( self , resource , * args , ** kwargs ) : <EOL> super ( ActionExecutionReRunCommand , self ) . __init__ ( <EOL> resource , kwargs . pop ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> * args , ** kwargs ) <EOL> self . parser . add_argument ( '<STR_LIT:id>' , nargs = '<STR_LIT:?>' , <EOL> metavar = '<STR_LIT:id>' , <EOL> help = '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' , <EOL> help = '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , nargs = '<STR_LIT:*>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> self . _add_common_options ( ) <EOL> @ add_auth_token_to_kwargs_from_cli <EOL> def run ( self , args , ** kwargs ) : <EOL> existing_execution = self . manager . get_by_id ( args . id , ** kwargs ) <EOL> if not existing_execution : <EOL> raise resource . ResourceNotFoundError ( '<STR_LIT>' % <EOL> ( args . id ) ) <EOL> action_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> runner_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> action_exec_mgr = self . app . client . managers [ '<STR_LIT>' ] <EOL> action_ref = existing_execution . action [ '<STR_LIT>' ] <EOL> action = action_mgr . get_by_ref_or_id ( action_ref ) <EOL> runner = runner_mgr . get_by_name ( action . runner_type ) <EOL> action_parameters = self . _get_action_parameters_from_args ( action = action , runner = runner , <EOL> args = args ) <EOL> execution = action_exec_mgr . re_run ( execution_id = args . id , <EOL> parameters = action_parameters , <EOL> tasks = args . tasks , <EOL> no_reset = args . no_reset , <EOL> ** kwargs ) <EOL> execution = self . _get_execution_result ( execution = execution , <EOL> action_exec_mgr = action_exec_mgr , <EOL> args = args , ** kwargs ) <EOL> return execution </s>
