o
    .&ßa.V  ã                   @   s(  d dl Z d dlZ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 d dlmZmZmZmZ d dlmZ d dlmZ d d	lmZ d
d„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"dS )é    N)Údatetime)Útzutc)Úrelativedelta)Úparse_timestamp)Ú
is_windowsÚurlparseÚRawConfigParserÚStringIO)Úutils)ÚBasicCommand)Ú	uni_printc                 C   s|   g }d}dD ]0}t | |ƒ}|dkr.|r| d¡ | t|ƒ¡ | |dkr+|d d… n|¡ |r2 n|dk}qd |¡}|S )NF)ÚyearsÚmonthsÚdaysÚhoursÚminutesr   Úandé   éÿÿÿÿú )ÚgetattrÚappendÚstrÚjoin)Ú	remainingÚvaluesÚprev_non_zero_attrÚattrÚvalueÚmessage© r    úJ/usr/lib/python3/dist-packages/awscli/customizations/codeartifact/login.pyÚget_relative_expiration_time   s   



r"   c                   @   sL   e Zd ZdZ	ddd„Zddd„Zdd	„ Zd
d„ Zddd„Ze	dd„ ƒZ
dS )Ú	BaseLoginz-%s was not found. Please verify installation.Nc                 C   s.   || _ || _|| _|| _|| _|| _|| _d S ©N)Ú
auth_tokenÚ
expirationÚrepository_endpointÚdomainÚ
repositoryÚsubprocess_utilsÚ	namespace)Úselfr%   r&   r'   r(   r)   r*   r+   r    r    r!   Ú__init__(   s   
zBaseLogin.__init__Fc                 C   ó   t dƒ‚)Nzlogin()©ÚNotImplementedError)r,   Údry_runr    r    r!   Úlogin2   ó   zBaseLogin.loginc                 C   s<   |D ]}t j d |¡¡ t j tj¡ t j tj¡ qd S )Nr   )ÚsysÚstdoutÚwriter   ÚosÚlinesep)r,   ÚtoolÚcommandsÚcommandr    r    r!   Ú_dry_run_commands5   s
   ýzBaseLogin._dry_run_commandsc                 C   sr   t | jt tƒ ¡ƒt dd� }t|ƒ}tj d 	|| j
¡¡ tj tj¡ tj d 	|| j¡¡ tj tj¡ d S )Né   )ÚsecondszASuccessfully configured {} to use AWS CodeArtifact repository {} zLogin expires in {} at {})r   r&   r   Únowr   r"   r4   r5   r6   Úformatr'   r7   r8   )r,   r9   r   Úexpiration_messager    r    r!   Ú_write_success_message;   s   ÿÿ
þ
ÿz BaseLogin._write_success_messagec                 C   s€   |r
|   ||¡ d S |D ],}z| jj|| jj| jjd� W q ty8 } z|jtjkr2t| j| ƒ‚|‚d }~ww |  	|¡ d S )N)r5   Ústderr)
r<   r*   Ú
check_callÚPIPEÚOSErrorÚerrnoÚENOENTÚ
ValueErrorÚ_TOOL_NOT_FOUND_MESSAGErB   )r,   r9   r:   r1   r;   Úexr    r    r!   Ú_run_commandsK   s&   
ýÿ€ûzBaseLogin._run_commandsc                 K   r.   )Nzget_commands()r/   )ÚclsÚendpointr%   Úkwargsr    r    r!   Úget_commands`   s   zBaseLogin.get_commandsr$   ©F)Ú__name__Ú
__module__Ú__qualname__rJ   r-   r2   r<   rB   rL   ÚclassmethodrP   r    r    r    r!   r#   %   s    
ÿ


r#   c                   @   sZ   e Zd ZdZd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S )ÚNuGetBaseLoginz{endpoint}v3/index.jsonz/Added source %s to the user level NuGet.Config
z&Updated source %s in the NuGet.Config
Fc              
   C   s  z|   ¡ }W n ty# } z|jtjkrt| j|  ¡  ƒ‚|‚d }~ww | jj| j	d�}|  
||¡\}}|rA|  d||¡}| j}n
|  d||¡}| j}|rad dd„ |D ƒ¡}	t|	ƒ tdƒ d S z| jj|| jjd� W n tjy€ }
 ztd	ƒ |
‚d }
~
ww t|| ƒ |  d
¡ d S )N)rN   ÚupdateÚaddr   c                 S   s   g | ]}t |ƒ‘qS r    )r   )Ú.0Úcdr    r    r!   Ú
<listcomp>�   s    z(NuGetBaseLogin.login.<locals>.<listcomp>Ú
©rC   z"Failed to update the NuGet.Config
Únuget)Ú_get_source_to_url_dictrF   rG   rH   rI   rJ   Ú_get_tool_nameÚ_NUGET_INDEX_URL_FMTr@   r'   Ú_get_source_nameÚ_get_configure_commandÚ_SOURCE_UPDATED_MESSAGEÚ_SOURCE_ADDED_MESSAGEr   r   r*   Úcheck_outputrE   Ú
subprocessÚCalledProcessErrorrB   )r,   r1   Úsource_to_url_dictrK   Únuget_index_urlÚsource_nameÚalready_existsr;   Úsource_configured_messageÚdry_run_commandÚer    r    r!   r2   r   sP   ÿ€ûÿÿÿ
þ€þzNuGetBaseLogin.loginc                 C   sr   | j j|  ¡ | j jd�}| d¡ ¡ }dd„ |D ƒ}i }tdt|ƒdƒD ]}|  ||d  ¡||  	|| ¡< q$|S )Nr]   zutf-8c                 S   s   g | ]
}|  ¡ d kr|‘qS ©Ú ©Ústrip)rY   Úliner    r    r!   r[   ¼   s    z:NuGetBaseLogin._get_source_to_url_dict.<locals>.<listcomp>r   é   )
r*   rf   Ú_get_list_commandrE   ÚdecodeÚ
splitlinesÚrangeÚlenÚ_parse_source_urlÚ_parse_source_name)r,   ÚresponseÚlinesri   Úir    r    r!   r_   ž   s   þÿz&NuGetBaseLogin._get_source_to_url_dictc                 C   s$   |  ¡  dd¡d }| d d¡d S )Nz [r   r   )rs   ÚrsplitÚsplit)r,   rt   Úline_without_tagr    r    r!   r|   Å   s   z!NuGetBaseLogin._parse_source_namec                 C   s   |  ¡ S r$   rr   )r,   rt   r    r    r!   r{   Ï   s   z NuGetBaseLogin._parse_source_urlc                 C   s`   d  | j| j¡}| ¡ D ]\}}||kr|df  S q| ¡ D ]}||kr+|df  S q|dfS )Nz{}/{}TF)r@   r(   r)   ÚitemsÚkeys)r,   Úcodeartifact_urlÚsource_dictÚdefault_namerk   Ú
source_urlr    r    r!   rb   Ô   s   ÿÿzNuGetBaseLogin._get_source_namec                 C   r.   )Nz_get_tool_name()r/   ©r,   r    r    r!   r`   é   r3   zNuGetBaseLogin._get_tool_namec                 C   r.   )Nz_get_list_command()r/   r‰   r    r    r!   rv   ì   r3   z NuGetBaseLogin._get_list_commandc                 C   r.   )Nz_get_configure_command()r/   ©r,   Ú	operationrj   rk   r    r    r!   rc   ï   r3   z%NuGetBaseLogin._get_configure_commandNrQ   )rR   rS   rT   ra   re   rd   r2   r_   r|   r{   rb   r`   rv   rc   r    r    r    r!   rV   e   s    	
,'
rV   c                   @   ó$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú
NuGetLoginc                 C   ó   dS )Nr^   r    r‰   r    r    r!   r`   õ   ó   zNuGetLogin._get_tool_namec                 C   ó   g d¢S )N)r^   ÚsourcesÚlistz-formatÚdetailedr    r‰   r    r    r!   rv   ø   r3   zNuGetLogin._get_list_commandc                 C   s   dd|d|d|ddd| j gS )Nr^   r‘   z-namez-sourcez	-usernameÚawsz	-password)r%   rŠ   r    r    r!   rc   û   s   ûz!NuGetLogin._get_configure_commandN©rR   rS   rT   r`   rv   rc   r    r    r    r!   r�   ó   ó    r�   c                   @   rŒ   )ÚDotNetLoginc                 C   rŽ   )NÚdotnetr    r‰   r    r    r!   r`     r�   zDotNetLogin._get_tool_namec                 C   r�   )N)r˜   r^   r’   Úsourcez--formatr“   r    r‰   r    r    r!   rv   
  r3   zDotNetLogin._get_list_commandc                 C   sf   dd|dg}|dkr|  |¡ |d|g7 }n|  |¡ |d|g7 }|ddd	| jg7 }ts1|  d
¡ |S )Nr˜   r^   r™   rX   z--namez--sourcez
--usernamer”   z
--passwordz--store-password-in-clear-text)r   r%   r   )r,   r‹   rj   rk   r;   r    r    r!   rc     s   

þ
z"DotNetLogin._get_configure_commandNr•   r    r    r    r!   r—     r–   r—   c                   @   sF   e Zd Ze ¡  ¡ dkrdndZddd„Zedd„ ƒZ	ed	d
„ ƒZ
dS )ÚNpmLoginÚwindowsznpm.cmdÚnpmFc                 C   s2   |   | j¡}| j| j| j|d�}|  d||¡ d S )N)Úscoperœ   )Ú	get_scoper+   rP   r'   r%   rL   )r,   r1   r�   r:   r    r    r!   r2   )  s   ÿ
ÿzNpmLogin.loginc                 C   sF   t  d¡}|d u r|S | d¡r|}nd |¡}| |¡s!tdƒ‚|S )Nz^(@[a-z0-9-~][a-z0-9-._~]*)ú@z@{}zZInvalid scope name, scope must contain URL-safe characters, no leading dots or underscores)ÚreÚcompileÚ
startswithr@   ÚmatchrI   )rM   r+   Úvalid_scope_namer�   r    r    r!   rž   2  s   



ÿzNpmLogin.get_scopec           
      K   s�   g }|  d¡}|rd |¡nd}| | jdd||g¡ t |¡}d |j|j¡}| | jdd|dg¡ d |j|j¡}	| | jdd|	|g¡ |S )	Nr�   z{}:registryÚregistryÚconfigÚsetz//{}{}:always-authÚtruez//{}{}:_authToken)Úgetr@   r   ÚNPM_CMDr   ÚurlsplitÚnetlocÚpath)
rM   rN   r%   rO   r:   r�   r¥   Úrepo_uriÚalways_auth_configÚauth_token_configr    r    r!   rP   H  s(   
ÿ
ÿÿÿÿzNpmLogin.get_commandsNrQ   )rR   rS   rT   ÚplatformÚsystemÚlowerrª   r2   rU   rž   rP   r    r    r    r!   rš   #  s    
	
rš   c                   @   s&   e Zd ZdZddd„Zedd„ ƒZdS )	ÚPipLoginz1{scheme}://aws:{auth_token}@{netloc}{path}simple/Fc                 C   s"   |   | j| j¡}|  d||¡ d S )NÚpip)rP   r'   r%   rL   )r,   r1   r:   r    r    r!   r2   n  s   ÿzPipLogin.loginc                 K   s4   t  |¡}| jj|j||j|jd�}dddd|ggS )N)Úschemer%   r¬   r­   rµ   r¦   r§   zglobal.index-url)r   r«   ÚPIP_INDEX_URL_FMTr@   r¶   r¬   r­   )rM   rN   r%   rO   r®   Úpip_index_urlr    r    r!   rP   t  s   
üzPipLogin.get_commandsNrQ   )rR   rS   rT   r·   r2   rU   rP   r    r    r    r!   r´   j  s
    
r´   c                       sF   e Zd ZdZ	d‡ fdd„	Zedd„ ƒZddd	„Zed
d„ ƒZ‡  Z	S )Ú
TwineLoginz‹[distutils]
index-servers=
    pypi
    codeartifact

[codeartifact]
repository: {repository_endpoint}
username: aws
password: {auth_token}Nc                    s4   |d u r|   ¡ }|| _tt| ƒ ||||||¡ d S r$   )Úget_pypi_rc_pathÚpypi_rc_pathÚsuperr¹   r-   )r,   r%   r&   r'   r(   r)   r*   r»   ©Ú	__class__r    r!   r-   Ž  s   

þzTwineLogin.__init__c              
   K   s@  d|v sJ dƒ‚|d }| j j||d�}tƒ }tj |¡r‡zL| |¡ | dd¡}dd„ | d¡D ƒ}d	|vrH| 	d	¡ | 
dddd |¡ ¡ d	| ¡ vrS| d	¡ | 
d	d
|¡ | 
d	dd¡ | 
d	d|¡ W n$ ty† }	 ztj d| ¡ tj tj¡ |	‚d }	~	ww | t|ƒ¡ tƒ }
| |
¡ |
 ¡ }|
 ¡  |S )Nr»   zpypi_rc_path must be provided.)r'   r%   Ú	distutilszindex-serversc                 S   s    g | ]}|  ¡ d kr|  ¡ ‘qS rp   rr   )rY   Úserverr    r    r!   r[   ·  s
    þz+TwineLogin.get_commands.<locals>.<listcomp>r\   Úcodeartifactr)   Úusernamer”   Úpasswordz%s is in an invalid state.)ÚDEFAULT_PYPI_RC_FMTr@   r   r7   r­   ÚexistsÚreadr©   r�   r   r§   r   ÚsectionsÚadd_sectionÚ	Exceptionr4   r5   r6   r8   Úreadfpr	   ÚgetvalueÚclose)rM   rN   r%   rO   r»   Údefault_pypi_rcÚpypi_rcÚindex_serversÚserversro   Úpypi_rc_streamÚpypi_rc_strr    r    r!   rP   Ÿ  sH   þ
þ
ÿ
€ý
zTwineLogin.get_commandsFc                 C   sÄ   | j | j| j| jd�}|r@tj d¡ tj tj¡ tj d| j ¡ tj tj¡ tj tj¡ tj |¡ tj tj¡ d S t	| jdƒ�}| |¡ W d   ƒ n1 sVw   Y  |  
d¡ d S )N)r»   z.Dryrun mode is enabled, not writing to pypirc.z(%s would have been set to the following:zw+Útwine)rP   r'   r%   r»   r4   r5   r6   r7   r8   ÚopenrB   )r,   r1   rÒ   Úfpr    r    r!   r2   ×  s&   ýÿÿzTwineLogin.loginc                 C   s   t j t j d¡d¡S )Nú~z.pypirc)r7   r­   r   Ú
expanduser)rM   r    r    r!   rº   ð  s   zTwineLogin.get_pypi_rc_pathr$   rQ   )
rR   rS   rT   rÄ   r-   rU   rP   r2   rº   Ú__classcell__r    r    r½   r!   r¹   �  s    ø

7r¹   c                   @   sÊ   e Zd ZdZdeddœdeddœdeddœdeddœdeddœdœZ	d	Z
d
Zddee	 ¡ ƒddœddddœddddœddddœdddddœddddœddddddœgZdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'S )(ÚCodeArtifactLoginz>Log in to the idiomatic tool for the requested package format.r^   F)Úpackage_formatÚ	login_clsÚnamespace_supportrœ   TÚpypi)r^   r˜   rœ   rµ   rÓ   r2   z¬Sets up the idiomatic tool for your package format to use your CodeArtifact repository. Your login information is valid for up to 12 hours after which you must login again.r9   z1The tool you want to connect with your repository)ÚnameÚ	help_textÚchoicesÚrequiredr(   zYour CodeArtifact domain name)rÞ   rß   rá   zdomain-ownerz5The AWS account ID that owns your CodeArtifact domainr+   z0Associates a namespace with your repository toolzduration-secondsÚintegerz9The time, in seconds, that the login information is valid)rÞ   Úcli_type_namerß   rá   r)   z!Your CodeArtifact repository namezdry-runÚ
store_truez‰Only print the commands that would be executed to connect your tool with your repository without making any changes to your configuration)rÞ   Úactionrß   rá   Údefaultc                 C   s,   | j | d }|s|jrtd |¡ƒ‚|jS )NrÜ   z,Argument --namespace is not supported for {})ÚTOOL_MAPr+   rI   r@   )r,   r9   Úparsed_argsÚnamespace_compatibler    r    r!   Ú_get_namespaceJ  s   
ÿz CodeArtifactLogin._get_namespacec                 C   s8   |j |j|dœ}|jr|j|d< |jdi |¤Ž}|d S )N)r(   r)   r@   ÚdomainOwnerÚrepositoryEndpointr    )r(   r)   Údomain_ownerÚget_repository_endpoint)r,   Úcodeartifact_clientrè   rÚ   rO   Ú get_repository_endpoint_responser    r    r!   Ú_get_repository_endpointT  s   ý
ÿz*CodeArtifactLogin._get_repository_endpointc                 C   s>   d|j i}|jr|j|d< |jr|j|d< |jdi |¤Ž}|S )Nr(   rë   ÚdurationSecondsr    )r(   rí   Úduration_secondsÚget_authorization_token)r,   rï   rè   rO   Ú get_authorization_token_responser    r    r!   Ú_get_authorization_tokend  s   ÿ

ÿz*CodeArtifactLogin._get_authorization_tokenc                 C   sœ   |j  ¡ }| j| d }t | jd|¡}|  ||¡}|  |||¡}|j}|j	}	|  
||¡}
|d }t|d ƒ}| j| d |||||	t|
ƒ}| |j¡ dS )NrÚ   rÁ   ÚauthorizationTokenr&   rÛ   r   )r9   r³   rç   Ú	cli_utilsÚ!create_client_from_parsed_globalsÚ_sessionrö   rñ   r(   r)   rê   r   rg   r2   r1   )r,   rè   Úparsed_globalsr9   rÚ   rï   Úauth_token_resr'   r(   r)   r+   r%   r&   r2   r    r    r!   Ú	_run_mains  s,   
ÿÿÿþzCodeArtifactLogin._run_mainN)rR   rS   rT   Ú__doc__r�   r—   rš   r´   r¹   rç   ÚNAMEÚDESCRIPTIONr’   r„   Ú	ARG_TABLErê   rñ   rö   rý   r    r    r    r!   rÙ   õ  s~    ýýýýýëÿ
üýüýûýùÝ.
rÙ   )#rG   r7   r±   r4   rg   r    r   Údateutil.tzr   Údateutil.relativedeltar   Úbotocore.utilsr   Úawscli.compatr   r   r   r	   Úawscli.customizationsr
   rø   Úawscli.customizations.commandsr   Úawscli.customizations.utilsr   r"   Úobjectr#   rV   r�   r—   rš   r´   r¹   rÙ   r    r    r    r!   Ú<module>   s0    @ Gt