
    +/c?*                        d Z ddlZddlZddlZddlmZmZmZ ddlZddl	Zddl
Z
ddlmZ ddlmZmZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZ ej        d         d
k     rddlmZ nddlmZ  ej        ej                  Z  ej!        dd          Z"dZ#ddgZ$g dZ% e ed                    Z&e&'                    dd          Z( e eej)        d                    '                    dd          Z* G d de+          Z, G d de-          Z.d Z/d Z0d Z1d+dZ2d,dZ3d,d Z4	 	 	 d,d!Z5	 	 	 d,d"Z6	 	 	 d,d#Z7	 	 	 	 d-d%Z8	 	 	 d,d&Z9	 	 	 	 d.d'Z:d( Z;	 	 	 d,d)Z<	 	 	 	 	 	 d/d*Z=dS )0z5
Configure Conda to use Anaconda Commercial Edition.
    N)abspath
expanduserjoin)reset_context)Commandsrun_command)CondaKeyError)read_binstar_tokensremove_binstar_tokenset_binstar_token)Channel)version   )urljoinCONDA_TOKEN_REPO_URLz!https://repo.anaconda.cloud/repo/mainrmsys2)freezmro-archiveproz
~/.condarc%z%%z.condarcc                       e Zd ZdS )CondaTokenErrorN__name__
__module____qualname__     7lib/python3.11/site-packages/conda_token/repo_config.pyr   r   '           Dr   r   c                       e Zd ZdS )CondaVersionWarningNr   r   r   r    r#   r#   +   r!   r   r#   c                  <    t           t          j        d          k    S )Nz4.7.0)CONDA_VERSIONr   parser   r   r    can_restore_free_channelr'   /   s    GM'2222r   c                  ,    t                      } | j        S )N)r   
ssl_verify)contexts    r    get_ssl_verifyr+   3   s    ooGr   c                  :    t          t          j        d           dS )zRuns conda clean -i.

    It is important to remove index cache when
    changing the condarc to ensure that the downloaded
    repodata is correct.
    z-iN)r   r   CLEANr   r   r    clean_indexr.   8   s     %%%%%r   Fc                    t                      }|rd}t          t          t          d                    }| |_        |                    d          }t          j        ||          }|j        dk    rt          d          dS )	z2Checks that token can be used with the repository.Fzmain/noarch/repodata.jsonT)with_credentials)verify   zPThe token could not be validated. Please check that you have typed it correctly.N)
r+   r   r   REPO_URLtokenurlrequestsheadstatus_coder   )r4   no_ssl_verifyr)   channel	token_urlr   s         r    validate_tokenr<   B   s      !!J  
gh(CDDEEGGMT22Ii
333A}pqqq r   c                 "   t           t          j        d          k     rt          j        dt
                     dS g }| r|                    d           nB|r|                    d           n*|r(|                    d                    |                     g d}|                    |           t          t          j        g|R   dd	t                              d
          g}|                    |           t          t          j        g|R   dS )zEnable package signature verification.

    This will set extra_safety_checks: True and
    signing_metadata_url_base in the CondaRC file.
    4.10.1z[You need upgrade to at least Conda version 4.10.1 to enable package signature verification.N--system--env	--file={})--setextra_safety_checkstruerB   signing_metadata_url_base/)r%   r   r&   warningswarnr#   appendformatextendr   r   CONFIGr3   rstripcondarc_systemcondarc_envcondarc_filecondarc_file_argssafety_check_argsmetadata_url_argss         r    enable_extra_safety_checksrU   U   s1    w}X....s*	, 	, 	, C  ,,,,	 C  ))))	 C  !3!3L!A!ABBB@@@.///4"34444 "=xs?S?ST.///4"3444444r   c                    t           t          j        d          k     rdS g }| r|                    d           nB|r|                    d           n*|r(|                    d                    |                     g d}|                    |           	 t          t          j        g|R   n# t          $ r Y nw xY wddg}|                    |           	 t          t          j        g|R   dS # t          $ r Y dS w xY w)	zDisable package signature verification.

    This will set extra_safety_checks: false and remove
    signing_metadata_url_base in the CondaRC file.
    r>   Nr?   r@   rA   )rB   rC   false--remove-keyrE   )
r%   r   r&   rI   rJ   rK   r   r   rL   r	   rN   s         r    disable_extra_safety_checksrY   q   sZ    w}X.... C  ,,,,	 C  ))))	 C  !3!3L!A!ABBBAAA.///HO8&788888    ()DE.///HO8&7888888   s$   B. .
B;:B;C1 1
C?>C?c                     g d}| r|                     d           nB|r|                     d           n*|r(|                     d                    |                     t          t          j        g|R   dS )zRun conda config --set add_anaconda_token true.

    Setting this parameter to true ensures that the token
    is used when making requests to the repository.
    )rB   add_anaconda_tokenrD   r?   r@   rA   NrI   rJ   r   r   rL   rO   rP   rQ   config_argss       r    _set_add_anaconda_tokenr_      s     :99K =:&&&&	 =7####	 =;--l;;<<<.+......r   c                     g d}| r|                     d           nB|r|                     d           n*|r(|                     d                    |                     t          t          j        g|R   dS )zRun conda config --set ssl_verify false.

    Setting this parameter to false disables all
    SSL verification for conda activities
    )rB   r)   rW   r?   r@   rA   Nr\   r]   s       r    _set_ssl_verify_falsera      s     322K =:&&&&	 =7####	 =;--l;;<<<.+......r   c                     g d}| r|                     d           nB|r|                     d           n*|r(|                     d                    |                     t          t          j        g|R ddi dS )zRuns conda config --set restore_free_channel false.

    The free channel is provided by Commercial Edition as
    and should be added directly.)rB   restore_free_channelrW   r?   r@   rA   use_exception_handlerTNr\   r]   s       r    _unset_restore_free_channelre      s     =<<K =:&&&&	 =7####	 =;--l;;<<<J+JJJTJJJJJr   Tc                 $   t          t          |           }|rdndd|g}|r|                    d           nB|r|                    d           n*|r(|                    d                    |                     t	          t
          j        g|R   dS )z<Adds a named Commercial Edition channel to default_channels.z	--prependz--appenddefault_channelsr?   r@   rA   N)r   r3   rI   rJ   r   r   rL   )r:   prependrO   rP   rQ   channel_urlr^   s          r    _set_channelrj      s    
 (G,,K .JK  =:&&&&	 =7####	 =;--l;;<<<.+......r   c                    ddg}| r|                     d           nB|r|                     d           n*|r(|                     d                    |                     	 t          t          j        g|R   dS # t
          $ r Y dS w xY w)zRuns conda config --remove-key default_channels

    It is best to remove the default_channels in case they
    are not set at the default values before configuring
    Commercial Edition.
    rX   rg   r?   r@   rA   N)rI   rJ   r   r   rL   r	   r]   s       r    _remove_default_channelsrl      s     "#56K =:&&&&	 =7####	 =;--l;;<<<HO2k222222   s    A9 9
BBc           	      z   t          | ||           t                      rt          | ||           t          t          d| ||           t
          D ]}t          |d| ||           |dS |D ]L}|t          v rt          |d| ||            t          d|dd                    t                              dS )a  Configure the default_channels to utilize only Commercial Edition.


    This function performs the following actions
    1. unset default_channels if it exists in the condarc
    2. unset restore_free_channel if it exists in the condarc
    3. Add the main, r, and msys2 channels to default_channels
    4. Optionally add any of the archive channels:
       free, pro, mro, mro-archive
    T)rh   rO   rP   rQ   FNzThe archive channel z is not one of z, )	rl   r'   re   rj   MAIN_CHANNELACTIVE_CHANNELSARCHIVE_CHANNELS
ValueErrorr   )rO   rP   rQ   include_archive_channelscs        r    configure_default_channelsrt      s,    ^[,GGG!! O#NKNNNt .KVbd d d d  h hQ$2Zf	h 	h 	h 	h 	h  '% k k   E(6K^jl l l l l *!!!TYYWgMhMhMhijjjk kr   c                      t                      S )zReturn a dictionary of tokens for all configured repository urls.

    Note that this function will return tokens configured for non-Commercial Edition
    urls.)r
   r   r   r    
token_listrv   !  s    
    r   c                     t          t                     t          | ||           t          | ||           t	                       dS )a  Completely remove the Commercial Edition token and default_channels.

    This function performs three actions.
    1. Remove the token
    2. Remove the custom default_channels in the condarc
    3. Disable package signature verification
    4. Run conda clean -i
    N)r   r3   rl   rY   r.   )systemenvfiles      r    token_remover{   )  sC     """VS$///T222MMMMMr   c                    t          t                     t          t          |            t          |||           |rt	          |||           |rt          |||           t          ||||           t                       dS )aJ  Set the Commercial Edition token and configure default_channels.


    This function performs 4 actions.
    1. Remove previous Commercial Edition token if present.
    2. Add token.
    3. Configure default_channels in the condarc file.
    4. Optionally enable Conda package signature verification
    5. Run conda clean -i
    N)r   r3   r   r_   ra   rU   rt   r.   )r4   rx   ry   rz   rr   r9   enable_signature_verifications          r    	token_setr~   :  s    " """h&&&FC... 1fc4000$ 6"63555vsD2JKKKMMMMMr   )F)FFN)TFFN)FFNN)FFNNFF)>__doc__ossysrG   os.pathr   r   r   condaconda.gateways.loggingr6   conda.base.contextr   conda.cli.python_apir   r   conda.exceptionsr	   conda.gateways.anaconda_clientr
   r   r   conda.models.channelr   	packagingr   version_infourlparser   urllib.parser&   __version__r%   getenvr3   rn   ro   rp   user_rc_pathreplaceescaped_user_rc_pathprefixescaped_sys_rc_pathRuntimeErrorr   UserWarningr#   r'   r+   r.   r<   rU   rY   r_   ra   re   rj   rl   rt   rv   r{   r~   r   r   r    <module>r      s    
			 



  - - - - - - - - - -       , , , , , , 6 6 6 6 6 6 6 6 * * * * * *? ? ? ? ? ? ? ? ? ? ) ( ( ( ( (      A       $$$$$$e/0029+-PQQ.111 wzz,//00#++C66 gdd3:z::;;CCCNN 	 	 	 	 	l 	 	 		 	 	 	 	+ 	 	 	3 3 3  
& & &r r r r&5 5 5 58   B ,1(-)-/ / / /( */&+'+/ / / /( 05,1-1K K K K& #' %""/ / / /. -2).*.   0 /4+0,08<"k "k "k "kJ! ! !    $ '+!,1     r   