o
    .&ßaÃi  ã                   @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dlm!Z! d dlm"Z" d dl#m$Z$ d dl#m%Z% d dl#m&Z& d dl'm(Z( d dl'm)Z) d dl'm*Z* d dl'm+Z+ d dl'm,Z, d dl-m.Z. d dl/m0Z0 d dl/m1Z1 d dl2m3Z3 d d l2m4Z4 e 5d!¡Z6d"Z7eƒ Z8d# 9d$¡ d%d&„ Z:d'd(„ Z;d)d*„ Z<G d+d,„ d,e=ƒZ>G d-d.„ d.eƒZ?G d/d0„ d0e=ƒZ@G d1d2„ d2e=ƒZAdS )3é    N)Ú__version__)ÚHierarchicalEmitter)Ú
xform_name)Úcopy_kwargsÚOrderedDict)ÚNoCredentialsError)ÚNoRegionError)ÚProfileNotFound)Úget_global_history_recorder)ÚEnvironmentVariablesr   )Úget_stderr_text_writer)Úget_formatter)Úload_plugins)Ú
CLICommand)Úsix)ÚMainArgParser)ÚServiceArgParser©ÚArgTableArgParser)ÚUSAGE)ÚProviderHelpCommand)ÚServiceHelpCommand)ÚOperationHelpCommand)ÚCustomArgument)ÚListArgument)ÚBooleanArgument)ÚCLIArgument)ÚUnknownArgumentError)Úunpack_argument)ÚAliasLoader)ÚAliasCommandInjector)Ú emit_top_level_args_parsed_event)Úwrite_exceptionzawscli.clidriverzE%(asctime)s - %(threadName)s - %(name)s - %(levelname)s - %(message)sÚ Úidnac                  C   s    t ƒ } |  ¡ }t d|d¡ |S )NÚCLI_RCÚCLI)Úcreate_clidriverÚmainÚHISTORY_RECORDERÚrecord)ÚdriverÚrc© r-   ú2/usr/lib/python3/dist-packages/awscli/clidriver.pyr(   D   s   r(   c                  C   s>   t j t¡} t| ƒ t| j di ¡|  d¡d� t	| d�}|S )NÚpluginsÚevent_emitter)Úevent_hooks©Úsession)
Úbotocorer3   ÚSessionr   Ú_set_user_agent_for_sessionr   Úfull_configÚgetÚget_componentÚ	CLIDriver)r3   r+   r-   r-   r.   r'   K   s   ÿ
r'   c                 C   s   d| _ t| _dt | _d S )Nzaws-clizbotocore/%s)Úuser_agent_namer   Úuser_agent_versionÚbotocore_versionÚuser_agent_extrar2   r-   r-   r.   r6   T   s   r6   c                   @   sˆ   e Zd Zd dd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd dd„Zdd„ Zdd„ Zdd„ ZdS )!r:   Nc                 C   sF   |d u rt j t¡| _t| jƒ n|| _d | _d | _d | _tƒ | _	d S ©N)
r4   r3   Úget_sessionr   r6   Ú	_cli_dataÚ_command_tableÚ_argument_tabler   Úalias_loader©Úselfr3   r-   r-   r.   Ú__init__\   s   zCLIDriver.__init__c                 C   s   | j d u r| j d¡| _ | j S )NÚcli)rA   r3   Úget_data©rF   r-   r-   r.   Ú_get_cli_datag   s   
zCLIDriver._get_cli_datac                 C   ó   | j d u r
|  ¡ | _ | j S r?   )rB   Ú_build_command_tablerJ   r-   r-   r.   Ú_get_command_tableo   ó   

zCLIDriver._get_command_tablec                 C   rL   r?   )rC   Ú_build_argument_tablerJ   r-   r-   r.   Ú_get_argument_tablet   rO   zCLIDriver._get_argument_tablec                 C   s&   |   | j¡}| jjd|| j| d� |S )zš
        Create the main parser to handle the global arguments.

        :rtype: ``argparser.ArgumentParser``
        :return: The parser object

        zbuilding-command-table.main©Úcommand_tabler3   Úcommand_object)Ú_build_builtin_commandsr3   Úemit©rF   rS   r-   r-   r.   rM   y   s   ýzCLIDriver._build_command_tablec                 C   s0   t ƒ }| ¡ }|D ]}t|| j|d�||< q	|S )N)Úcli_namer3   Úservice_name)r   Úget_available_servicesÚServiceCommandr3   )rF   r3   ÚcommandsÚservicesrY   r-   r-   r.   rU   ˆ   s   þz!CLIDriver._build_builtin_commandsc                 C   s(   |   |¡}t| j| jƒ}| ||¡ d S r?   )Ú_create_parserr    r3   rD   Úinject_aliases)rF   rS   ÚparserÚinjectorr-   r-   r.   Ú_add_aliases‘   s
   
ÿzCLIDriver._add_aliasesc                 C   sZ   t ƒ }|  ¡ }| dd ¡}|D ]}t|| ƒ}|  ||¡}| |¡ q| jjd|d� |S )NÚoptionszbuilding-top-level-params)Úargument_table)r   rK   r8   r   Ú_create_cli_argumentÚadd_to_arg_tabler3   rV   )rF   rd   Úcli_dataÚcli_argumentsÚoptionÚoption_paramsÚcli_argumentr-   r-   r.   rP   —   s   ÿzCLIDriver._build_argument_tablec                 C   sD   t || dd¡| d¡| d¡| d¡| d¡| d¡| d¡d	�S )
NÚhelpr#   ÚdestÚdefaultÚactionÚrequiredÚchoicesÚtype)Ú	help_textrm   rn   ro   rp   rq   Úcli_type_name)r   r8   )rF   Úoption_namerj   r-   r-   r.   re   ¥   s   ùzCLIDriver._create_cli_argumentc              
   C   s<   |   ¡ }t| j|  ¡ |  ¡ | dd ¡| dd ¡| dd ¡ƒS )NÚdescriptionÚsynopsisÚ
help_usage)rK   r   r3   rN   rQ   r8   )rF   rg   r-   r-   r.   Úcreate_help_command¯   s   


üzCLIDriver.create_help_commandc                 C   s<   |   ¡ |d< |  ¡ }t|| j ¡ | dd ¡|  ¡ dd�}|S )Nrl   rv   Úaws)Úprog)ry   rK   r   r3   Ú
user_agentr8   rQ   )rF   rS   rg   r`   r-   r-   r.   r^   ·   s   

üzCLIDriver._create_parserc              
   C   sº  |du rt jdd… }|  ¡ }|  |¡}|  ||¡ | |¡\}}z$|  |¡ |  |¡ t 	d| j
 ¡ d¡ t 	d|d¡ ||j ||ƒW S  tyn } zt j dt ¡ t j t|ƒ¡ t j d¡ W Y d}~dS d}~w ty‰ } zd	| }|  |¡ W Y d}~dS d}~w ty¤ } zd
| }|  |¡ W Y d}~dS d}~w ty·   t j d¡ dtj  Y S  tyÜ } ztjddd� t d¡ t|tƒ d� W Y d}~dS d}~ww )zé

        :param args: List of arguments, with the 'aws' removed.  For example,
            the command "aws s3 list-objects --bucket foo" will have an
            args list of ``['s3', 'list-objects', '--bucket', 'foo']``.

        Né   ÚCLI_VERSIONr&   ÚCLI_ARGUMENTSz
usage: %s
Ú
éÿ   zA%s You can also configure your region by running "aws configure".z=%s. You can configure credentials by running "aws configure".é€   zException caught in main()T©Úexc_infozExiting with rc 255)Úoutfile)ÚsysÚargvrN   r^   rb   Úparse_known_argsÚ_handle_top_level_argsÚ_emit_session_eventr)   r*   r3   r|   Úcommandr   ÚstderrÚwriter   Ústrr   Ú_show_errorr   ÚKeyboardInterruptÚstdoutÚsignalÚSIGINTÚ	ExceptionÚLOGÚdebugr"   r   )rF   ÚargsrS   r`   Úparsed_argsÚ	remainingÚeÚmsgr-   r-   r.   r(   Â   sX   


ÿ€ÿ
€ÿ
€
€üzCLIDriver.mainc                 C   s   | j jd| j |d� d S )Nzsession-initialized)r3   r˜   )r3   rV   )rF   r˜   r-   r-   r.   rŠ   ö   s   
þzCLIDriver._emit_session_eventc                 C   s*   t j|dd� tj |¡ tj d¡ d S )NTrƒ   r€   )r•   r–   r†   rŒ   r�   )rF   r›   r-   r-   r.   r�      s   zCLIDriver._show_errorc                 C   sÐ   t | j|ƒ |jr| j d|j¡ |jr| j d|j¡ |jr]| jjdtjt	d� | jjdtjt	d� | jjdtjt	d� | jjdtjt	d� t
 d| j ¡ ¡ t
 d	tjd
d … ¡ d S | jjdtjd� d S )NÚprofileÚregionr4   )Úformat_stringÚawscliÚ
s3transferÚurllib3zCLI version: %szArguments entered to CLI: %sr}   )Úlogger_nameÚ	log_level)r!   r3   rœ   Úset_config_variabler�   r–   Úset_stream_loggerÚloggingÚDEBUGÚ
LOG_FORMATr•   r|   r†   r‡   ÚERROR)rF   r—   r-   r-   r.   r‰     s.   ÿÿÿÿ
ÿz CLIDriver._handle_top_level_argsr?   )Ú__name__Ú
__module__Ú__qualname__rG   rK   rN   rQ   rM   rU   rb   rP   re   ry   r^   r(   rŠ   r�   r‰   r-   r-   r-   r.   r:   Z   s     
	

4
r:   c                   @   s’   e Zd ZdZddd„Zedd„ ƒZejdd„ ƒZedd	„ ƒZed
d„ ƒZ	e	jdd„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )r[   z�A service command for the CLI.

    For example, ``aws ec2 ...`` we'd create a ServiceCommand
    object that represents the ec2 service.

    Nc                 C   s:   || _ || _d | _|d u r|| _n|| _| g| _d | _d S r?   )Ú_namer3   rB   Ú_service_nameÚ_lineageÚ_service_model)rF   rX   r3   rY   r-   r-   r.   rG   )  s   
zServiceCommand.__init__c                 C   ó   | j S r?   ©r­   rJ   r-   r-   r.   Úname?  ó   zServiceCommand.namec                 C   ó
   || _ d S r?   r²   ©rF   Úvaluer-   r-   r.   r³   C  ó   
c                 C   s   |   ¡ S r?   )Ú_get_service_modelrJ   r-   r-   r.   Úservice_modelG  s   zServiceCommand.service_modelc                 C   r±   r?   ©r¯   rJ   r-   r-   r.   ÚlineageK  r´   zServiceCommand.lineagec                 C   rµ   r?   r»   r¶   r-   r-   r.   r¼   O  r¸   c                 C   rL   r?   )rB   Ú_create_command_tablerJ   r-   r-   r.   rN   S  rO   z!ServiceCommand._get_command_tablec                 C   sV   | j d u r(z| j d¡ | jd ¡}W n ty   d }Y nw | jj| j|d�| _ | j S )NÚapi_versions)Úapi_version)r°   r3   Úget_config_variabler8   r®   r	   Úget_service_model)rF   r¿   r-   r-   r.   r¹   X  s   
ÿ
ÿÿÿz!ServiceCommand._get_service_modelc                 C   s.   |   ¡ }| |¡\}}|  ¡ }||j ||ƒS r?   )r^   rˆ   rN   Ú	operation)rF   r—   Úparsed_globalsÚservice_parserr˜   r™   rS   r-   r-   r.   Ú__call__c  s   zServiceCommand.__call__c                 C   sx   t ƒ }|  ¡ }|jD ]}t|dƒ}| |¡}t|| j| j|t| jƒd�||< q
| jj	d| j || j| d� |  
|¡ |S )Nú-)r³   Úparent_namer3   Úoperation_modelÚoperation_callerzbuilding-command-table.%srR   )r   r¹   Úoperation_namesr   rÈ   ÚServiceOperationr­   r3   ÚCLIOperationCallerrV   Ú_add_lineage)rF   rS   rº   Úoperation_namerX   rÈ   r-   r-   r.   r½   l  s&   


ûý
z$ServiceCommand._create_command_tablec                 C   s$   |D ]}|| }| j |g |_ qd S r?   ©r¼   )rF   rS   r‹   Úcommand_objr-   r-   r.   rÍ   €  s   þzServiceCommand._add_lineagec                 C   s,   |   ¡ }t| j|  ¡ |d d | j¡| jd�S )NÚ.)r3   ÚobjrS   Ú	arg_tableÚevent_classr³   )rN   r   r3   r¹   ÚjoinÚlineage_namesr­   rW   r-   r-   r.   ry   …  s   
ûz"ServiceCommand.create_help_commandc                 C   s"   |   ¡ }|  ¡ |d< t|| jd�S )Nrl   )Úoperations_tablerY   )rN   ry   r   r­   rW   r-   r-   r.   r^   Ž  s
   ÿzServiceCommand._create_parserr?   )rª   r«   r¬   Ú__doc__rG   Úpropertyr³   Úsetterrº   r¼   rN   r¹   rÅ   r½   rÍ   ry   r^   r-   r-   r-   r.   r[      s(    





		r[   c                   @   sº   e Zd ZdZeedœZeZdd„ Z	e
dd„ ƒZejdd„ ƒZe
dd	„ ƒZejd
d	„ ƒZe
dd„ ƒZe
dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!S )"rË   zŽA single operation of a service.

    This class represents a single operation for a service, for
    example ``ec2.DescribeInstances``.

    )ÚlistÚbooleanc                 C   s@   d| _ || _|| _|| _| g| _|| _|| _|jrd| _dS dS )aT  

        :type name: str
        :param name: The name of the operation/subcommand.

        :type parent_name: str
        :param parent_name: The name of the parent command.

        :type operation_model: ``botocore.model.OperationModel``
        :param operation_object: The operation model
            associated with this subcommand.

        :type operation_caller: ``CLIOperationCaller``
        :param operation_caller: An object that can properly call the
            operation.

        :type session: ``botocore.session.Session``
        :param session: The session object.

        NT)	Ú
_arg_tabler­   Ú_parent_nameÚ_operation_callerr¯   Ú_operation_modelÚ_sessionÚ
deprecatedÚ_UNDOCUMENTED)rF   r³   rÇ   rÉ   rÈ   r3   r-   r-   r.   rG   ¥  s   
ÿzServiceOperation.__init__c                 C   r±   r?   r²   rJ   r-   r-   r.   r³   Ç  r´   zServiceOperation.namec                 C   rµ   r?   r²   r¶   r-   r-   r.   r³   Ë  r¸   c                 C   r±   r?   r»   rJ   r-   r-   r.   r¼   Ï  r´   zServiceOperation.lineagec                 C   rµ   r?   r»   r¶   r-   r-   r.   r¼   Ó  r¸   c                 C   s   dd„ | j D ƒS )Nc                 S   s   g | ]}|j ‘qS r-   )r³   )Ú.0Úcmdr-   r-   r.   Ú
<listcomp>Ú  s    z2ServiceOperation.lineage_names.<locals>.<listcomp>rÏ   rJ   r-   r-   r.   rÖ   ×  s   zServiceOperation.lineage_namesc                 C   rL   r?   )rÝ   Ú_create_argument_tablerJ   r-   r-   r.   rÓ   Ü  s   

zServiceOperation.arg_tablec           
      C   s  d| j | jf }| j|| j|| jd� |  | j¡}|  |¡ | |¡\}}|jdkr3|  	¡ }|||ƒS |jr<| 
|j¡ |rGtdd |¡ ƒ‚d| j | jf }| j|||d� |  || j¡}d| j | jf }| j||||d	�}	|	d ur|t|	tƒrz|	‚|	S | j | jjj| jj||¡S )
Nz+before-building-argument-table-parser.%s.%s)rd   r—   r3   rl   zUnknown options: %sz, zoperation-args-parsed.%s.%s)r˜   rÃ   zcalling-command.%s.%s)Úcall_parametersr˜   rÃ   )rÞ   r­   Ú_emitrÓ   rá   Ú_create_operation_parserÚ	_add_helprˆ   rl   ry   Úappendr   rÕ   Ú_build_call_parametersÚ_emit_first_non_none_responseÚ
isinstancer”   rß   Úinvokerà   rº   rY   r³   )
rF   r—   rÃ   ÚeventÚoperation_parserr˜   r™   Úop_helprè   Úoverrider-   r-   r.   rÅ   â  sZ   
ÿÿ


ÿÿÿÿÿü

ýzServiceOperation.__call__c                 C   s"   t | j| j| j| jd | j¡d�S )NrÑ   )rÈ   rÓ   r³   rÔ   )r   rá   rà   rÓ   r­   rÕ   rÖ   rJ   r-   r-   r.   ry     s   üz$ServiceOperation.create_help_commandc                 C   s   |j ddd� d S )Nrl   ú?)Únargs)Úadd_argument)rF   r`   r-   r-   r.   rë     s   zServiceOperation._add_helpc                 C   sL   i }t |ƒ}| ¡ D ]}|j}||v r#|| }|  ||¡}| ||¡ q
|S r?   )ÚvarsÚvaluesÚpy_nameÚ_unpack_argÚadd_to_params)rF   r—   rÓ   Úservice_paramsr˜   Ú
arg_objectrú   r·   r-   r-   r.   rí   %  s   €z'ServiceOperation._build_call_parametersc                 C   s,   | j }| jjj}t| jdƒ}t|||||ƒS )NrÆ   )rá   rà   rº   Úendpoint_prefixr   r­   r   )rF   rk   r·   r3   rY   rÎ   r-   r-   r.   rû   4  s   
ÿzServiceOperation._unpack_argc              	   C   sÒ   t ƒ }| jj}g }i }|d ur|j}|j}| ¡ D ]6\}}t|dƒ}| j |j	| j
¡}|j dd¡}	||v o8|	 }
| j d¡}||||
| j||d�}| |¡ qt |¡ | jd| j| jf | j| j| |d� |S )NrÆ   ÚidempotencyTokenFr0   )r³   Úargument_modelÚis_requiredrÈ   Úserialized_namer0   zbuilding-argument-table.%s.%s)rÈ   r3   r‹   rd   )r   rà   Úinput_shapeÚrequired_membersÚmembersÚitemsr   Ú	ARG_TYPESr8   Ú	type_nameÚDEFAULT_ARG_CLASSÚmetadatará   r9   rf   r•   r–   ré   rÞ   r­   )rF   rd   r  Úrequired_argumentsÚarg_dictÚarg_nameÚ	arg_shapeÚcli_arg_nameÚ	arg_classÚis_tokenr  r0   rþ   r-   r-   r.   rç   >  sD   

ÿú

ÿûz'ServiceOperation._create_argument_tablec                 K   ó   | j j|fi |¤ŽS r?   )rá   rV   ©rF   r³   Úkwargsr-   r-   r.   ré   ^  s   zServiceOperation._emitc                 K   r  r?   )rá   Úemit_first_non_none_responser  r-   r-   r.   rî   a  s
   ÿÿz.ServiceOperation._emit_first_non_none_responsec                 C   s   t |ƒ}|S r?   r   )rF   rÓ   r`   r-   r-   r.   rê   e  s   z)ServiceOperation._create_operation_parserN)rª   r«   r¬   rØ   r   r   r  r   r
  rG   rÙ   r³   rÚ   r¼   rÖ   rÓ   rÅ   ry   rë   rí   rû   rç   ré   rî   rê   r-   r-   r-   r.   rË   –  s8    þ"





6
 rË   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )rÌ   z.Call an AWS operation and format the response.c                 C   rµ   r?   )rá   rE   r-   r-   r.   rG   n  s   
zCLIOperationCaller.__init__c                 C   s<   | j j||j|j|jd�}|  ||||¡}|  |||¡ dS )a©  Invoke an operation and format the response.

        :type service_name: str
        :param service_name: The name of the service.  Note this is the service name,
            not the endpoint prefix (e.g. ``ses`` not ``email``).

        :type operation_name: str
        :param operation_name: The operation name of the service.  The casing
            of the operation name should match the exact casing used by the service,
            e.g. ``DescribeInstances``, not ``describe-instances`` or
            ``describe_instances``.

        :type parameters: dict
        :param parameters: The parameters for the operation call.  Again, these values
            have the same casing used by the service.

        :type parsed_globals: Namespace
        :param parsed_globals: The parsed globals from the command line.

        :return: None, the result is displayed through a formatter, but no
            value is returned.

        )Úregion_nameÚendpoint_urlÚverifyr   )rá   Úcreate_clientr�   r  Ú
verify_sslÚ_make_client_callÚ_display_response)rF   rY   rÎ   Ú
parametersrÃ   ÚclientÚresponser-   r-   r.   rð   q  s   ýÿzCLIOperationCaller.invokec                 C   sR   t |ƒ}| |¡r|jr| |¡}|jdi |¤Ž}|S t|t |ƒƒdi |¤Ž}|S )Nr-   )r   Úcan_paginateÚpaginateÚget_paginatorÚgetattr)rF   r  rÎ   r  rÃ   Úpy_operation_nameÚ	paginatorr   r-   r-   r.   r  ’  s   
þÿz$CLIOperationCaller._make_client_callc                 C   s2   |j }|d u r| j d¡}t||ƒ}|||ƒ d S )NÚoutput)r'  rá   rÀ   r   )rF   Úcommand_namer   rÃ   r'  Ú	formatterr-   r-   r.   r  �  s
   
z$CLIOperationCaller._display_responseN)rª   r«   r¬   rØ   rG   rð   r  r  r-   r-   r-   r.   rÌ   j  s    !rÌ   )Br†   r’   r¦   Úbotocore.sessionr4   r   r=   Úbotocore.hooksr   r   Úbotocore.compatr   r   Úbotocore.exceptionsr   r   r	   Úbotocore.historyr
   rŸ   r   Úawscli.compatr   Úawscli.formatterr   Úawscli.pluginr   Úawscli.commandsr   r   Úawscli.argparserr   r   r   r   Úawscli.helpr   r   r   Úawscli.argumentsr   r   r   r   r   Úawscli.argprocessr   Úawscli.aliasr   r    Úawscli.utilsr!   r"   Ú	getLoggerr•   r¨   r)   Úencoder(   r'   r6   Úobjectr:   r[   rË   rÌ   r-   r-   r-   r.   Ú<module>   sb   
ÿ

	 Gv U