
    Vhe;                       d Z ddlm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
mZ ddlmZ ddlmZmZmZmZmZ ddlmZmZ dd	lmZmZmZ dd
lmZ ddlmZmZm Z  ddl!m"Z"m#Z# ddl$m%Z% ejL                  jO                  edd      Z(ejL                  jO                  edd      Z) G d de      Z* ejV                  d       G d d             Z, ejV                  d       G d de,             Z- ejV                  d       G d de,             Z. ejV                  d       G d de,             Z/ ejV                  d       G d de,             Z0	 	 	 	 	 d.	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d/dZ1d0d Z2	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d1d!Z3	 	 	 	 	 	 	 	 	 	 d2d"Z4	 	 d3	 	 	 	 	 d4d#Z5d5d6d$Z6d7d%Z7d8d&Z8d9d'Z9	 	 d:	 	 	 	 	 	 	 	 	 d;d(Z:d<d=d)Z;d>d*Z<d?d+Z=d@d,Z>dAd-Z?y)BzPython requirements management    )annotationsN   )to_textto_bytes)read_text_file)ANSIBLE_TEST_DATA_ROOTANSIBLE_TEST_TARGET_ROOTApplicationErrorSubprocessErrordisplay)check_pyyamlcreate_result_directories)EnvironmentConfigIntegrationConfigUnitsConfig)data_context)PosixConfigPythonConfigVirtualPythonConfig)LocalConnection
Connection)get_coverage_versionsetupzquiet_pip.pyzrequirements.pyc                  $     e Zd ZdZd fdZ xZS )PipUnavailableErrorz+Exception raised when pip is not available.c                Z    t         |   d|j                   d|j                   d       y )NzPython z at "z" does not have pip available.)super__init__versionpath)selfpython	__class__s     Z/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/python_requirements.pyr   zPipUnavailableError.__init__B   s)    76>>"2%}Dbcd    )r"   r   returnNone)__name__
__module____qualname____doc__r   __classcell__)r#   s   @r$   r   r   ?   s    5e er%   r   T)frozenc                      e Zd ZdZddZy)
PipCommandzBase class for pip commands.c                j    t        |       j                  dd j                         }|| j                  fS )z3Return a serialized representation of this command.   N)typer(   lower__dict__r!   names     r$   	serializezPipCommand.serializeJ   s0    Dz""12&,,.T]]""r%   N)r&   ztuple[str, dict[str, t.Any]])r(   r)   r*   r+   r7    r%   r$   r/   r/   F   s
    &#r%   r/   c                  8    e Zd ZU dZded<   ded<   ded<   d	dZy)

PipInstallz*Details required to perform a pip install.list[tuple[str, str]]requirementsconstraints	list[str]packagesc                    j                         t        fd| j                  D              xs t        fd| j                  D              S )zHReturn True if the specified package will be installed, otherwise False.c              3  B   K   | ]  }|j                         v   y wNr3   ).0packager6   s     r$   	<genexpr>z)PipInstall.has_package.<locals>.<genexpr>\   s     IDGMMO+Is   c              3  H   K   | ]  \  }}|j                         v   y wrB   rC   )rD   r    contentsr6   s      r$   rF   z)PipInstall.has_package.<locals>.<genexpr>]   s!     UxDHNN,,Us   ")r3   anyr?   r<   r5   s    `r$   has_packagezPipInstall.has_packageX   s@    zz|I4==II VU4CTCTUU	Wr%   N)r6   strr&   bool)r(   r)   r*   r+   __annotations__rJ   r8   r%   r$   r:   r:   P   s    4''&&Wr%   r:   c                  &    e Zd ZU dZded<   ded<   y)PipUninstallz,Details required to perform a pip uninstall.r>   r?   rL   ignore_errorsNr(   r)   r*   r+   rM   r8   r%   r$   rO   rO   `   s    6r%   rO   c                      e Zd ZdZy)
PipVersionz(Details required to get the pip version.N)r(   r)   r*   r+   r8   r%   r$   rS   rS   h   s    2r%   rS   c                  :    e Zd ZU dZded<   ded<   ded<   ded<   y	)
PipBootstrapz"Details required to bootstrap pip.rK   pip_versionr>   r?   rL   
setuptoolswheelNrQ   r8   r%   r$   rU   rU   m   s    ,Kr%   rU   c           	        t        |        t        | |      sy|r$t        | t        t        f      r| j
                  rd}|r@	 t        j                  }|j                  |j                        }|rd}nd||j                  <   t        ||||r| j                  nd|dd      }	|	syt        | ||	|       t        d |	D              rt        |       yy# t        $ r i x}t        _        Y w xY w)zHInstall requirements for the given Python using the specified arguments.NTF)r"   
controlleransiblecommandcoverageminimizesanityc              3  b   K   | ]'  }t        |t              xr |j                  d        ) yw)pyyamlN)
isinstancer:   rJ   rD   r\   s     r$   rF   z'install_requirements.<locals>.<genexpr>   s+     
eQX:gz*Lw/B/B8/LL
es   -/)r   requirements_allowedrb   r   r   r]   install_requirementsansible_cacheAttributeErrorgetr    collect_requirementsr\   run_piprI   r   )
argsr"   r[   r\   r]   rZ   
connectionrf   ansible_installedcommandss
             r$   re   re   z   s     d#j1:d[2C$DE$--	D0>>M *--fkk:G)-M&++&# 'TH D&(J/ 
e\d
eeV f5  	DACCM0>	Ds   C C*)C*c                    t        |       }|d   }|j                         D cg c]  \  }}| d|  }}}t        ||dd      }|gS c c}}w )zPReturn the details necessary to bootstrap pip into an empty virtual environment.pipz==F)rV   r?   rW   rX   )get_venv_packagesitemsrU   )r"   infrastructure_packagesrV   r6   r   r?   	bootstraps          r$   collect_bootstrapru      sh    /7)%0K:Q:W:W:YZw4&7)$ZHZ	I ; [s   A
c                   g }|r=|j                  t        dt        | j                        j                   gd             |s|r|j                  t        ||             |r|j                  t        |             |dk(  r|j                  t                      |dv r|j                  t        ||             |s|rOt        d |D              r=t        |       |z   }t        t        |             }|j                  t        |             |S )zHCollect requirements for the given Python using the specified arguments.z
coverage==Fr?   r=   units)integrationzwindows-integrationznetwork-integrationc              3  <   K   | ]  }t        |t                y wrB   )rb   r:   rc   s     r$   rF   z'collect_requirements.<locals>.<genexpr>   s     #\Jw
$C#\s   )r?   )extendcollect_package_installr   r   coverage_versioncollect_general_installcollect_sanity_installcollect_units_installcollect_integration_installrI   ru   listrq   collect_uninstall)	r"   rZ   r[   r]   r^   r\   r_   rn   uninstall_packagess	            r$   ri   ri      s     "$H/JG[\b\j\jGkG|G|F}:~9  NS  T  	U'/AB.v67'-/0OO3GZHI(#\S[#\ \ %V,x7 ""3F";<)3EFGOr%   c                P   |xs t        |       }t        |      }t        | t              rt        |t               rdnd}nd}t        |t              r|dz  }t        j                  d|j                   |        | j                  s!	 |j                  |j                  g|d       y
y
# t        $ rs t        t               g      }	 |j                  |j                  g|d        # t        $ r1}d	|j                  |j                  z   v rt        |      d
Y d
}~ d
}~ww xY ww xY w)zWRun the specified pip commands for the given Python, and optionally the specified host.z (controller)z	 (target) z [venv]z#Installing requirements for Python F)datacaptureTzpip is unavailable:N)r   prepare_pip_scriptrb   r   r   r   infor   explainrunr    r   rS   stdoutstderrr   )rk   r"   rn   rl   scriptcontextexs          r$   rj   rj      s     4t4J)F$)* &0
O%L/R]&-.9 LL6v~~6FwiPQ<<	NNFKK=vuNE   		'7F@}64H
 	 # @(BII		,AA-f54? B 	@		s6   B) )D%C'&D%'	D!0'DD%D!!D%c                    g }g }|r<t         j                  j                  t        dd      }|j	                  t        |f       | r?t         j                  j                  t        d|  d      }|j	                  t        |f       t        ||      S )zJReturn details necessary for the specified general-purpose pip install(s).r<   zansible.txt.txt)osr    joinr   appendcollect_install)r\   r[   requirements_pathsconstraints_pathsr    s        r$   r~   r~     s    
 13/1ww||2NMR!!#94"@Aww||2NwitDTU!!#94"@A-/@AAr%   c                     t        g g | |      S )z?Return the details necessary to install the specified packages.r=   )r   rw   s     r$   r|   r|   #  s    2r8EEr%   c                   g }g }t         j                  j                  t        dd|  d      }|j	                  t        |f       t               j                  j                  rot         j                  j                  t               j                  j                  d|  d      }|j	                  t               j                  j                  |f       t        ||d      S )zEReturn the details necessary for the specified sanity pip install(s).r<   zsanity.r   z
code-smellz.requirements.txtFr   )r   r    r   r   r   r   content
is_ansiblesanity_pathrootr   )r_   r   r   r    s       r$   r   r   (  s    02/177<<.'&QU@VWD5t<=~((ww||LN22>>QWPXXiNjk!!<>#9#9#>#>"EF-/@eTTr%   c                    g } g }t         j                  j                  t               j                  j
                  d      }| j                  t               j                  j                  |f       t         j                  j                  t               j                  j
                  d      }|j                  t               j                  j                  |f       t        | |      S )z@Return details necessary for the specified units pip install(s).requirements.txtconstraints.txt)	r   r    r   r   r   	unit_pathr   r   r   )r   r   r    s      r$   r   r   7  s    02/177<<..88:LMD|~55::DAB77<<..88:KLDln44994@A-/@AAr%   c                   g }g }|rdnddf}|D ]  }t         j                  j                  t               j                  j
                  | d      }t         j                  j                  |      sb|j                  t               j                  j                  |f        n |D ]  }t         j                  j                  t               j                  j
                  |  d| d      }t         j                  j                  |      se|j                  t               j                  j                  |f        n |D ]  }t         j                  j                  t               j                  j
                  | d      }t         j                  j                  |      sb|j                  t               j                  j                  |f        n t        ||      S )zFReturn details necessary for the specified integration pip install(s).zcontroller.ztarget.r   r   .r   )
r   r    r   r   r   integration_pathexistsr   r   r   )r\   rZ   r   r   prefixesprefixr    s          r$   r   r   E  s   02/1
 ",B?H ww||LN22CCxO_E`a77>>$%%|~'='='B'BD&IJ  ww||LN22CCyPQRXQYYiEjk77>>$%%|~'='='B'BD&IJ  ww||LN22CCxE_`77>>$$$ln&<&<&A&A4%HI -/@AAr%   c                
   t        |      }|r:|j                  t        t        j                  j                  t        dd      f       | D cg c]<  \  }}t        |      st        j                  j                  ||      t        |      f> }}}|D cg c]<  \  }}t        |      st        j                  j                  ||      t        |      f> }}}|xs g }|s|rt        |||      g}|S g }|S c c}}w c c}}w )zOBuild a pip install list from the given requirements, constraints and packages.r<   r   )r<   r=   r?   )
r   r   r   r   r    r   usable_pip_filerelpathr   r:   )r   r   r?   r=   r   r    r<   installss           r$   r   r   g  s#    ./  "8"'',,G]_m  pA  ;B  "C  	DZl  GJDRVp  AE  qFRWW__T40.2FG  GL  GYj  E:4QUn}  C  oDBGGOOD$/1EF  EK  E~2Hx%#
  O O G Es   C9!-C9C?*-C?c                "    t        | |      }|gS )z=Return the details necessary for the specified pip uninstall.)r?   rP   )rO   )r?   rP   	uninstalls      r$   r   r     s    #I
 ;r%   c                    t        d      }i }|j                  | j                  |      j                         D ci c]  \  }}||xs ||    }}}|S c c}}w )zxReturn a dictionary of Python packages needed for a consistent virtual environment specific to the given Python version.z24.2)rp   )dictrh   r   rr   )r"   default_packagesoverride_packagesr6   r   r?   s         r$   rq   rq     s     4 N_MbMbcicqcq  tD  NE  NK  NK  NM  NMD'g7!1$!77  NH  NO Ns   Ac                   | j                   ry|r8| j                  j                  xs  | j                  j                  j                  S | j	                  t
              d   }|j                  xs |j                  j                  S )a  
    Return True if requirements can be installed, otherwise return False.

    Requirements are only allowed if one of the following conditions is met:

    The user specified --requirements manually.
    The install will occur on the controller and the controller or controller Python is managed by ansible-test.
    The install will occur on the target and the target or target Python is managed by ansible-test.
    Tr   )r<   rZ   
is_managedr"   only_targetsr   )rk   rZ   targets      r$   rd   rd     sh     ))NT__-C-C-N-NN{+A.F8 8 88r%   c           	     
   | D cg c]  }|j                          }}t        j                  dt        j                  |d       d       t        t        t              t        j                  |      }t        t        j                  t        t        j                  |                        }t        }t        |      }|j                  |      }t        j                  d| d	|j                          d       |S c c}w )
z?Generate a Python script to perform the requested pip commands.z>>> Requirements Commands
   )indentr1   )	verbosity)r   r   rn   )payloadz!>>> Python Script from Template (z)
)r7   r   r   jsondumpsr   r   QUIET_PIP_SCRIPT_PATHr   r   base64	b64encoder   REQUIREMENTS_SCRIPT_PATHformatstrip)rn   r\   r   rk   r   r    templater   s           r$   r   r     s    /78GG8D8LL.tzz$q/I.JKWXY34##D f&&x

40@'ABCG#Dd#H__W_-FLL4TF#flln=MNZ[\M# 9s   D c                    t        |       xrI t        j                  j                  |       xr( t        t        j                  j	                  |             S )zAReturn True if the specified pip file is usable, otherwise False.)rL   r   r    r   getsize)r    s    r$   r   r     s5    :N"''...N48M3NNr%   )FFFTN)rk   r   r"   r   r[   rL   r\   rL   r]   rL   rZ   rL   rl   t.Optional[Connection]r&   r'   )r"   r   r&   list[PipCommand])r"   r   rZ   rL   r[   rL   r]   rL   r^   rL   r\   t.Optional[str]r_   r   r&   r   )
rk   r   r"   r   rn   r   rl   r   r&   r'   )NF)r\   r   r[   rL   r&   list[PipInstall])T)r?   r>   r=   rL   r&   r   )r_   rK   r&   r   )r&   r   )r\   rK   rZ   rL   r&   r   )NT)
r   r;   r   r;   r?   zt.Optional[list[str]]r=   rL   r&   r   )F)r?   r>   rP   rL   r&   zlist[PipUninstall])r"   r   r&   zdict[str, str])rk   r   rZ   rL   r&   rL   )rn   r   r&   rK   )r    r   r&   rL   )@r+   
__future__r   r   dataclassesr   r   typingtencodingr   r   ior   utilr   r	   r
   r   r   util_commonr   r   configr   r   r   r   r   host_configsr   r   r   connectionsr   r   coverage_utilr   r    r   r   r   r   	dataclassr/   r:   rO   rS   rU   re   ru   ri   rj   r~   r|   r   r   r   r   r   rq   rd   r   r   r8   r%   r$   <module>r      s   $ "    	 
 
  
 %=wW 77<<(@'K\] 
e* e d## # $# d#W W $W d#:  $ d#3 3 $3 d#:  $ )-0
00 0 	0
 0 0 '0 
0f &&& & 	&
 & & & &R$
$$ $ '	$
 
$V  $BBB B&F
UBBJ '+	-, $ 	
 :$9*,Or%   