
    Vh-                    z   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m	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZ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l'm(Z( ddZ)ddZ*dddZ+ddZ,e(dd       Z-d dZ.e(dd       Z/ G d d      Z0 G d de      Z1d!dZ2	 d"	 	 	 	 	 	 	 	 	 	 	 d#dZ3y)$zJMiscellaneous utility functions and classes specific to ansible cli tools.    )annotationsN   )ANSIBLE_BIN_SYMLINK_MAPSOFT_RLIMIT_NOFILE)common_environmentApplicationErrorANSIBLE_LIB_ROOTANSIBLE_TEST_ROOTANSIBLE_TEST_DATA_ROOTANSIBLE_ROOTANSIBLE_SOURCE_ROOTANSIBLE_TEST_TOOLS_ROOTMODE_FILE_EXECUTEraw_commandverified_chmod)create_temp_dir
ResultTypeintercept_pythonget_injector_path)IntegrationConfigPosixIntegrationConfigEnvironmentConfigCommonConfig)data_context)install_requirements)PythonConfig)mutexc           
         dd|dg}t        |       }t        j                  t        | | j                  ||dd      d         }|S )z3Return a dict parsed from the given inventory file.zansible-inventory-iz--listT)capturealwaysr   )ansible_environmentjsonloadsr   controller_python)argsinventory_pathcmdenv	inventorys        S/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/ansible_util.pyparse_inventoryr,   ;   sL    nh
?C
d
#C

+D$2H2H#s\`imnopqrI    c                    | j                  di       j                  di       | j                  |i       }|j                  dg       }t        fd|D              }|S )zGReturn a dict of hosts from the specified group in the given inventory._metahostvarshostsc              3  F   K   | ]  }|j                  |i       f  y wN)get).0namer0   s     r+   	<genexpr>zget_hosts.<locals>.<genexpr>H   s!     GD$T2./Gs   !)r4   dict)r*   
group_namegroup
host_namesr1   r0   s        @r+   	get_hostsr<   C   sU    }}Wb)--j"=HMM*b)E7B'JGJGGELr-   c                   t               }|d   }t        |       }|j                  |t        j                  j
                  z         s |t        j                  j
                  z   |z   }|s| j                         }| j                  s-t        j                  j                  |      st        d|z        t        d"i dt        t              d| j                  r|rddz  ndddd	d
ddddddddd|dddddt        |       ddd|dddddt        }t!        | t"              rD| j$                  r8|j'                  t        j                  j)                  t+               d             t!        | t,              r|j'                  d       |j'                  |       | j.                  rI|j'                  dt        j                  j)                  t0        j2                  j4                  d              t7               j8                  j:                  r8|j'                  t7               j8                  j:                  j<                  !       t7               j8                  j>                  r|j'                  tA        |              |S )#zRReturn a dictionary of environment variables to use when running Ansible commands.PATHzConfiguration not found: %s#ANSIBLE_PYTHON_MODULE_RLIMIT_NOFILEANSIBLE_FORCE_COLORz%struefalseANSIBLE_FORCE_HANDLERSANSIBLE_HOST_PATTERN_MISMATCHerrorANSIBLE_INVENTORYz	/dev/nullANSIBLE_DEPRECATION_WARNINGSANSIBLE_HOST_KEY_CHECKINGANSIBLE_RETRY_FILES_ENABLEDANSIBLE_CONFIGANSIBLE_LIBRARYANSIBLE_DEVEL_WARNING
PYTHONPATHPAGERz/bin/cat"ANSIBLE_WORKER_SHUTDOWN_POLL_COUNT100"ANSIBLE_WORKER_SHUTDOWN_POLL_DELAYz0.1ANSIBLE_TEST_ANSIBLE_LIB_ROOTzansible_connection_cli_stub.py)_ANSIBLE_CONNECTION_PATHz,/set/ansible_python_interpreter/in/inventory)ANSIBLE_PYTHON_INTERPRETERz	debug.log)ANSIBLE_DEBUGANSIBLE_LOG_PATHANSIBLE_COLLECTIONS_PATH )!r   get_ansible_bin_path
startswithospathpathsepget_ansible_configexplainexistsr   r8   strr   colorget_ansible_python_pathr	   
isinstancer   coverageupdatejoinr   r   debugr   LOGSr6   r   content
collectionroot
is_ansibleconfigure_plugin_paths)r&   rc   ansible_configr)   r]   ansible_bin_pathansibles          r+   r"   r"   L   sY   

Cv;D+D1??+bggoo=>"''//1D8002<<~ ><~MNN ,/0B,C-1ZZED6Mw  & '.	
 & &- #* %, & $ & +40  $ ,1%& ,1'* '7+G0 $)*t}} 	%'WW\\2C2EGg%h 	 	
 $./'U 	 	
 JJwzz

 WW\\*//*>*>L 	 	

 ~((

%1^%;%;%F%F%K%K 	 	
 ~((

)$/0Jr-   c                   t        | t              si S t        j                  j	                  t
        dd| j                        }t        j                  j	                  |d      }t        |      }t        j                  j	                  |d      g d}t        d |D              }|j                  dd	d
       |j                  t        fd|j                         D                     t        d |j                         D              }|S )zTReturn environment variables with paths to plugins relevant for the current command.testsupportcollectionsrW   plugins)actionbecomecachecallbackcliconf
connectionfilterhttpapir*   lookupnetconfstrategyterminalrt   varsc              3  *   K   | ]  }d |z  |f  yw)z
%s_pluginsNrY   )r5   r6   s     r+   r7   z)configure_plugin_paths.<locals>.<genexpr>   s     Jd|d*D1Js   doc_fragmentsmodulesmodule_utils)doc_fragmentlibraryr   c              3     K   | ]:  \  }}d |j                         z  t        j                  j                  |      f < yw)z
ANSIBLE_%sN)upperr\   r]   rh   )r5   keyvalueplugin_roots      r+   r7   z)configure_plugin_paths.<locals>.<genexpr>   s7     xWaWZ\a\CIIK/k51QRxs   A Ac              3  j   K   | ]+  \  }}t         j                  j                  |      s&||f - y wr3   )r\   r]   isdir)r5   r   r   s      r+   r7   z)configure_plugin_paths.<locals>.<genexpr>   s'     R
URWW]]5=QU|Rs   '3	3)
re   r   r\   r]   rh   r   commandr8   rg   items)r&   support_pathcollection_rootr)   plugin_list
plugin_mapr   s         @r+   ro   ro      s    d-.	77<< 3VYUL ggll<?O
!0C
 '',,|Y7KK( JkJJJ $#   JJtxeoeueuewxxy RciikR
RCJr-   c                
   	 t         j                  S # t        $ r Y nw xY wt        r%t        j
                  j                  t        d      }nt        dd      }t        j                         D ci c]/  \  }}t        j
                  j                  ||      t        |      1 nc c}}w }}}| j                  s>|j                         D ]+  \  }}t        j                  ||       t        |t                - |t         _        |S )z
    Return a directory usable for PATH, containing only the ansible entry points.
    If a temporary directory is required, it will be cached for the lifetime of the process and cleaned up at exit.
    binansible-test-z-bin)prefixsuffix)rZ   bin_pathAttributeErrorr   r\   r]   rh   r   r   r   r   get_cli_pathr`   shutilcopyr   r   )r&   r   r6   r]   	bin_linksdstsrcs          r+   rZ   rZ      s    #,,,  77<<e4 #/&IXoXuXuXwx*$PTRWW\\(D1<3EExx	x||%OO- 7SC%s$567 %-!Os    	04B%c                    t         t        d}|j                         D ]D  \  }}| j                  |      st        j
                  j                  || t        |      d       c S  t        |       )zReturn the absolute path to the CLI script from the given path which is relative to the `bin` directory of the original source tree layout.)z../lib/ansible/z../test/lib/ansible_test/N)	r	   r
   r   r[   r\   r]   rh   lenRuntimeError)r]   path_rewriter   destinations       r+   r   r      sk     ,%6L
  ,113 A??6"77<<T#f+,-?@@A t
r-   c                :   ~ 	 t         j                  S # t        $ r Y nw xY wt        r$t        j
                  j                  t              }nDt        d      }t	        j                  t        t        j
                  j                  |d             |t         _        |S )z
    Return a directory usable for PYTHONPATH, containing only the ansible package.
    If a temporary directory is required, it will be cached for the lifetime of the process and cleaned up at exit.
    r   )r   rr   )rd   python_pathr   r   r\   r]   dirnamer	   r   symlinkrh   )r&   r   s     r+   rd   rd      s{     	&222  ggoo&67
 &_=


#RWW\\+y%IJ*5's    	c                      e Zd ZdZddZy)CollectionDetailzCollection detail.c                    d | _         y r3   )version)selfs    r+   __init__zCollectionDetail.__init__  s	    (,r-   N)returnNone)__name__
__module____qualname____doc__r   rY   r-   r+   r   r     s
    -r-   r   c                  $     e Zd ZdZd fdZ xZS )CollectionDetailErrorz/An error occurred retrieving collection detail.c                8    t         |   d|z         || _        y )Nz&Error collecting collection detail: %s)superr   reason)r   r   	__class__s     r+   r   zCollectionDetailError.__init__!  s    AFJKr-   )r   rb   r   r   )r   r   r   r   r   __classcell__)r   s   @r+   r   r     s    9 r-   r   c                   t               j                  j                  }t        j                  j                  |j                  |j                        }t        | j                  t        j                  j                  t        d      |gd      d   }t        j                  |      }|j                  d      }|rt        |      |j                  d      }t               }|t        |      |_        |S d|_        |S )zReturn collection detail.zcollection_detail.pyTr    r   rE   r   N)r   rk   rl   r\   r]   rh   rm   	directoryr   r   r#   r$   r4   r   r   rb   r   )pythonrl   r   stdoutresultrE   r   details           r+   get_collection_detailr   &  s    ''22JZ__j.B.BCI&++rww||4KMc'dfopz~  AB  CFZZFJJwE#E**jj#GF%,%8S\FNM ?CFNMr-   c                ~   t         j                  j                  t        d|      }dd||g}|r&|j	                  dt        j                  |      g       | j                  r!|j                  dd| j                  z  z         t        | | j                  d       t        |       }t        | | j                  |||	       y
)zQRun the specified playbook using the given inventory file and playbook variables.	playbookszansible-playbookr   z-ez-%svT)rr   r   N)r\   r]   rh   r   extendr#   dumps	verbosityappendr   r%   r"   r   )r&   r'   playbookr    	variablesplaybook_pathr(   r)   s           r+   run_playbookr   :  s     GGLL!7hOMt^]
CC

D$**Y/01~~

5C$..012t55tD
d
#CT4113WMr-   )r&   r   r'   rb   r   dict[str, t.Any])r*   r   r9   rb   r   zdict[str, dict[str, t.Any]])TN)r&   r   rc   boolrp   zt.Optional[str]r   dict[str, str])r&   r   r   r   )r&   r   r   rb   )r]   rb   r   rb   )r   r   r   r   r3   )r&   r   r'   rb   r   rb   r    r   r   zt.Optional[dict[str, t.Any]]r   r   )4r   
__future__r   r#   r\   r   typingt	constantsr   r   utilr   r   r	   r
   r   r   r   r   r   r   r   util_commonr   r   r   r   configr   r   r   r   datar   python_requirementsr   host_configsr   threadr   r,   r<   r"   ro   rZ   r   rd   r   r   r   r   rY   r-   r+   <module>r      s   P "  	  
     
FR3l  >  8- -, 2 /3N
NN N 	N
 ,N 
Nr-   