
    Vh                        d Z ddlm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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 ddlmZ ddZddZddZ ddZ!ddZ"ddZ#y)zPyPI proxy management.    )annotationsN   )write_text_file)EnvironmentConfig)PosixConfig)ApplicationErrordisplay)ExitHandlerprocess_scoped_temporary_file)docker_available)HostTypeget_container_databaserun_support_containerrun_playbook)HostProfile)create_posix_inventoryc                   | j                   ryt               | j                  D cg c]  }t        |t              s| }}|xr t        fd|D              }| j                  xs |}|syt               s-| j                  rt        d      t        j                  d       yd}d}t        | d|d|g	       yc c}w )
z#Run a PyPI proxy support container.Nc              3  N   K   | ]  }|j                   j                  v   y w)N)pythonversion).0targetversions_needing_proxys     Q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/pypi_proxy.py	<genexpr>z!run_pypi_proxy.<locals>.<genexpr>;   s"     )v^d&--*?*?CY*Y)vs   "%zAUse of the PyPI proxy was requested, but Docker is not available.zlUnable to use the PyPI proxy because Docker is not available. Installation of packages using `pip` may fail.z)quay.io/ansible/pypi-test-container:3.2.0E  __pypi_proxy__zpypi-test-container)argscontextimagenameports)pypi_endpointtupletargets
isinstancer   any
pypi_proxyr   r   r	   warningr   )	r   targets_use_pypir   posix_targets
need_proxy	use_proxyr!   portr   s	           @r   run_pypi_proxyr0   4   s    .3g*.,,Z*V[:YVZMZ!vc)vhu)v&vJ-:I??"#fgg  G  	H7ED "f# [s
   B;B;c                P   | j                   r| j                   }nt        |       }|j                  j                  |j                  rt
        j                  nt
        j                  i       j                  d      }|syt        |j                               d   }|j                  }t        |j                               d   }d| d| d}t        j                  j                  |      d   j!                  d      d   }|j                  rt#        | |||       yt%        | |||       y)	z:Configure the environment to use a PyPI proxy, if present.r   Nr   r   zhttp://:z/root/pypi/+simple/r   )r$   r   dataget
controllerr   controlmanagedlistvalueshost_ipdictport_mapurllibparseurlparsesplitconfigure_controller_pypi_proxyconfigure_target_pypi_proxy)	r   profiler$   
containersr    accesshostr/   pypi_hostnames	            r   configure_pypi_proxyrH   T   s    **+D1
//%%':L:Lh&6&6RZRbRbdfgkkl|}gnn&'*~~FOO%&t,!$q.ABLL))-8;AA#FqIM'g}mT#D'=-P    c                :    t        | |||       t        | ||       y)z9Configure the controller environment to use a PyPI proxy.N)configure_pypi_proxy_pip!configure_pypi_proxy_easy_install)r   rC   r$   rG   s       r   rA   rA   n   s    T7M=I%dG]CrI   c                     t               t         |g       d	 fd}|j                  j                  rdnd}t	         ddt        |||             t        j                  |       y)
z5Configure the target environment to use a PyPI proxy.c                 $    t         dd       y)z.Undo changes made to configure the PyPI proxy.zpypi_proxy_restore.ymlT)captureNr   )r   inventory_paths   r   cleanup_pypi_proxyz7configure_target_pypi_proxy.<locals>.cleanup_pypi_proxyz   s    T>+CTRrI   yesnozpypi_proxy_prepare.ymlT)r$   rG   force)rO   	variablesNreturnNone)r   r   config
is_managedr   r;   r
   register)r   rC   r$   rG   rQ   rT   rP   s   `     @r   rB   rB   t   sj    248N4';S ^^..EDE~'?Y]#=ZO P +,rI   c                   t         j                  j                  d      dj                  ||      j	                         }dfd}t         j                  j                        r$|j                  j                  st        dz        t        j                  dz  d       t        j                  dd	|d
       | j                  s#t        |d       t        j                  |       yy)z0Configure a custom index for pip based installs.z~/.pip/pip.confz-
[global]
index-url = {0}
trusted-host = {1}
c                 d    t        j                  d z  d       t        j                          y)zRemove custom pip PyPI config.Removing custom PyPI config: %sr   	verbosityNr	   infoosremove)pip_conf_paths   r   pip_conf_cleanupz2configure_pypi_proxy_pip.<locals>.pip_conf_cleanup   s$    6FRST
		- rI   'Refusing to overwrite existing file: %s Injecting custom PyPI config: %sr   r_   Config: 
   TNrV   rc   path
expanduserformatstripexistsrY   rZ   r   r	   rb   explainr   r
   r[   )r   rC   r$   rG   pip_confrf   re   s         @r   rK   rK      s    GG&&'89M F=-(	 !
 
ww~~m$W^^-F-FH=XYYLL3mCqQLL]H=K<<x6-. rI   c                   t         j                  j                  d      dj                  |      j	                         }t         j                  j                        r$|j                  j                  st        dz        dfd}t        j                  dz  d       t        j                  dd	|d
       | j                  s#t        |d       t        j                  |       yy)z9Configure a custom index for easy_install based installs.z~/.pydistutils.cfgz 
[easy_install]
index_url = {0}
rg   c                 d    t        j                  d z  d       t        j                          y)zRemove custom PyPI config.r^   r   r_   Nra   )pydistutils_cfg_paths   r   pydistutils_cfg_cleanupzBconfigure_pypi_proxy_easy_install.<locals>.pydistutils_cfg_cleanup   s&    69MMYZ[
		&'rI   rh   r   r_   ri   rj   rk   TNrV   rl   )r   rC   r$   pydistutils_cfgrw   rv   s        @r   rL   rL      s    77--.BC F=%%' 
 
ww~~*+GNN4M4MHK__``(
 LL36JJVWXLL%9?KWXY<<,otD45 rI   )r   r   r+   boolrW   rX   )r   r   rC   r   rW   rX   )
r   r   rC   r   r$   strrG   rz   rW   rX   )r   r   rC   r   r$   rz   rW   rX   )$__doc__
__future__r   rc   urllib.parser=   ior   rY   r   host_configsr   utilr   r	   util_commonr
   r   docker_utilr   rD   r   r   r   ansible_utilr   host_profilesr   	inventoryr   r0   rH   rA   rB   rK   rL    rI   r   <module>r      si     " 	 

 
@Q4D-$/26rI   