
    Vh+                    x   d Z ddlmZ ddlmZ ddlZddlZddlZ	ddl
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 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! ddl"m#Z#  ejH                  d       G d d             Z% G d d      Z&edd       Z' ejH                  d       G d d             Z(edd       Z)y)z?Context information for the current invocation of ansible-test.    )annotationsN   )ApplicationErrorimport_plugins	is_subdirANSIBLE_LIB_ROOTANSIBLE_TEST_ROOTANSIBLE_SOURCE_ROOTdisplaycache)find_path_providerget_path_provider_classesProviderNotFoundForPath)SourceProvider)UnversionedSource)InstalledSource)UnsupportedSource)ContentLayoutLayoutProvider)UnsupportedLayoutT)frozenc                  &    e Zd ZU dZded<   ded<   y)PayloadConfigzEConfiguration required to build a source tree payload for delegation.zlist[tuple[str, str]]fileszdict[str, int]permissionsN__name__
__module____qualname____doc____annotations__     K/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/data.pyr   r   4   s    O  r#   r   c                  v    e Zd ZdZddZddZe	 	 	 	 	 	 	 	 	 	 dd       Zd Ze	dd       Z
ddZddZdd	Zy
)DataContextzXData context providing details about the current execution environment for ansible-test.c                   t         j                  j                  d      }t        j                         }t	        t
              }t	        t              }|| _        || _        d | _	        g | _
        |r| j                  |||d      \  }}nIt        r,t        |t              r| j                  ||t        d      \  }}n| j                  |||d      \  }}|| _        || _        y )NANSIBLE_TEST_CONTENT_ROOTFT)osenvirongetgetcwdr   r   r   _DataContext__layout_providers_DataContext__source_providers_DataContext__ansible_sourcepayload_callbacks#_DataContext__create_content_layoutr
   r   contentsource_provider)selfcontent_pathcurrent_pathlayout_providerssource_providersr2   r3   s          r$   __init__zDataContext.__init__?   s    zz~~&AByy{4^D4^D"2"2IMJL'+'C'CDTVfhtv{'|$G_ Y|=P%Q'+'C'CDTVfh{  ~C  (D$G_'+'C'CDTVfhtvz'{$G_&-.r#   c                   | j                   }|j                  }|sg S t        j                  j	                  |j
                  d      t        j                  dz  d       t        fdt        j                        D              }g }|D ]3  }t        j                  j	                  |      t        fdt        j                        D              }|D ]  }t        j                  j	                  |      }|t        j                  j	                  |j
                  |j                        k(  r|}	n+| j                  | j                  | j                  |d      d   }	t        |	j                               }
|
st        j                  d	|	j                  j                   |
fz  d       |j#                  |	        6 |S )
z
        Return a list of collection layouts, one for each collection in the same collection root as the current collection layout.
        An empty list is returned if the current content layout is not a collection layout.
        ansible_collectionszScanning collection root: %sr   )	verbosityc              3     K   | ]D  }t         j                  j                  t         j                  j                  |            sA| F y wNr)   pathisdirjoin).0name	root_paths     r$   	<genexpr>z8DataContext.create_collection_layouts.<locals>.<genexpr>c   s6      x$277==Y[Y`Y`YeYefoquYvKw x   AAAc              3     K   | ]D  }t         j                  j                  t         j                  j                  |            sA| F y wr>   r?   )rC   rD   namespace_paths     r$   rF   z8DataContext.create_collection_layouts.<locals>.<genexpr>h   sM       &HtUWU\U\UbUbcecjcjcocop~  AE  dF  VGd  &HrG   Fr   z#Including collection: %s (%d files))r2   
collectionr)   r@   rB   rootr   infosortedlistdir	directoryr1   r-   r.   len	all_files	full_nameappend)r4   layoutrJ   namespace_namescollectionsnamespace_namecollection_namescollection_namecollection_pathcollection_layout
file_countrI   rE   s              @@r$   create_collection_layoutsz%DataContext.create_collection_layoutsV   s   
 &&
IGGLL2GH	3i?1M  x"**Y2G xx- 	6NWW\\)^DN%  &Hrzz.7Q  &H   H#3 6"$'',,~"O"bggll:??JDXDX&YY(.%(,(D(DTE\E\^b^u^u  xG  IN  )O  PQ  )R% !2!<!<!>?
!BFWFbFbFlFlnxEyy  FG  H""#456		6( r#   c                   	 t        t        | ||      }	 t	        |t              rt        |j                        }nt        t        ||j                  |      }|j                  |j                  |j                  |j                              }||fS # t        $ r t        |      }Y w xY w# t        $ r t        |j                        }Y sw xY w)z@Create a content layout using the given providers and root path.)r   r   r   r   
isinstancer   rK   r   r   create	get_paths)r7   r8   rK   walklayout_providerr3   rT   s          r$   __create_content_layoutz#DataContext.__create_content_layout|   s    	60AQSWY]^O
	F
 /+<=2CODXDX2Y"4^EUWfWkWkmq"r !''(<(<o>W>WXgXlXl>mn&&# ' 	6/5O	6 ' 	F/0D0DEO	Fs#   B AB* B'&B'*C
Cc                    t         sg }t        t              |j                  fdj	                  j
                        D               t        t              |j                  fdj	                  j
                        D               t        |      S  j                  j                  r,t         fd j                  j                         D              S 	 t        t         j                  t         d      t        fdj	                  j
                        D              S # t        $ r t        t               Y Gw xY w)MReturn a tuple of Ansible source files with both absolute and relative paths.c              3     K   | ]O  }t         j                  j                  j                  |      t         j                  j                  d d|      f Q yw)libansibleNr)   r@   rB   rK   rC   r@   r3   s     r$   rF   z6DataContext.__create_ansible_source.<locals>.<genexpr>   sI      X# GGLL)=)=tDbggllSXZceiFjk Xs   AAc              3     K   | ]P  }t         j                  j                  j                  |      t         j                  j                  d dd|      f R yw)testrh   ansible_testNrj   rk   s     r$   rF   z6DataContext.__create_ansible_source.<locals>.<genexpr>   sL      X# GGLL)=)=tDbggllSY[`bprvFwx Xs   AAc              3     K   | ]:  }t         j                  j                  j                  j                  |      |f < y wr>   )r)   r@   rB   r2   rK   )rC   r@   r4   s     r$   rF   z6DataContext.__create_ansible_source.<locals>.<genexpr>   s/     l4"'',,t||'8'8$?Fls   A AFc              3  v   K   | ]0  }t         j                  j                  j                  |      |f 2 y wr>   rj   rk   s     r$   rF   z6DataContext.__create_ansible_source.<locals>.<genexpr>   s1       C$bggll?#7#7>E  Cs   69)r
   r   r   extendra   rK   r	   tupler2   
is_ansiblerQ   r   r   r.   r   r   )r4   sourcesr3   s   ` @r$   __create_ansible_sourcez#DataContext.__create_ansible_source   s1   "G-.>?ONN X'6'@'@AUAU'VX X ..?@ONN X'6'@'@AUAU'VX X >!<<""lSWS_S_SiSiSklll	E0AXAXZmotuO   CRaRkRkl{  mA  mA  SB  C  C  	C ' 	E/0CDO	Es    D* *EEc                \    | j                   s| j                         | _         | j                   S )rf   )r/   #_DataContext__create_ansible_sourcer4   s    r$   ansible_sourcezDataContext.ansible_source   s*     $$$($@$@$BD!$$$r#   c                :    | j                   j                  |       y)z$Register the given payload callback.N)r0   rS   )r4   callbacks     r$   register_payload_callbackz%DataContext.register_payload_callback   s    %%h/r#   c                b    | j                   j                  rt        | j                               y)z-Report an error if the layout is unsupported.N)r2   unsupportedr   explain_working_directoryrx   s    r$   check_layoutzDataContext.check_layout   s(    <<##"4#A#A#CDD $r#   c                   ddg}t         r*|j                  dt          d       |j                  d       t        j                         }|j                  d       |j                  d       |j                  d       |j                  d       |j                  d| d       t        j                  j                  t        j                  j                  |            d	k(  r3|j                  d
t        j                  j                  |       d       ntt        j                  j                  |      d	k(  r|j                  d
| d       n<d	|j                  t        j                  j                        vr|j                  d       t        | j                  j                  t              r%|j                  | j                  j                         dj                  |      }|S )z?Return a message explaining the working directory requirements.zJThe current working directory must be within the source tree being tested. zTesting Ansible: /zRTesting an Ansible collection: {...}/ansible_collections/{namespace}/{collection}/zNExample #1: community.general -> ~/code/ansible_collections/community/general/zTExample #2: ansible.util -> ~/.ansible/collections/ansible_collections/ansible/util/zCurrent working directory: r;   zExpected parent directory: z/{namespace}/{collection}/z4No "ansible_collections" parent directory was found.
)r
   rS   r)   r,   r@   basenamedirnamesplitsepr_   r2   r~   listrq   rB   )r4   blockscwdmessages       r$   r   z%DataContext.explain_working_directory   sk    Y

 MM-.A-B!DEMM"iikjkfglmb3C5:;77BGGOOC015JJMM78L7MMklmWWc"&;;MM7u<Z[\"#))BGGKK*@@MMPQdll..5MM$,,223))F#r#   N)returnNone)r   zlist[ContentLayout])
r7   zlist[t.Type[LayoutProvider]]r8   zlist[t.Type[SourceProvider]]rK   strrb   boolr   z&t.Tuple[ContentLayout, SourceProvider])r   ztuple[tuple[str, str], ...])r{   z!c.Callable[[PayloadConfig], None]r   r   )r   r   )r   r   r   r    r9   r]   staticmethodr1   rw   propertyry   r|   r   r   r"   r#   r$   r&   r&   <   s|    b/.$L '6'6' ' 	'
 
0' '8C2 % %0E
r#   r&   c                 H    d} | D ]  }t        d|z          t               }|S )zInitialize provider plugins.)rT   sourcezprovider/%s)r   r&   )provider_typesprovider_typecontexts      r$   data_contextr      s5    N
 ( 6}}456 mGNr#   c                  0    e Zd ZU dZded<   ded<   ded<   y)
PluginInfoz$Information about an Ansible plugin.r   plugin_typerD   z	list[str]pathsNr   r"   r#   r$   r   r      s    .
Ir#   r   c                    i } t               j                  j                  j                         D ]  \  }}t	        t               j                  j                  |            }t        |j                  t        j                  j                              }i }|D ]  }t        j                  j                  |      }|j                  t        j                  j                        |d }|dk(  r|dk7  rhYt        j                  j                  |      d   }	t               j                  j                  r|dk(  r|	j                  d      }	|j                  |	       dj!                  |      }	|j#                  |	g       j                  |        |j                         D 	
ci c]  \  }	}
|	t%        ||	|
       c}
}	| |<    | S c c}
}	w )	z
    Analyze content.
    The primary purpose of this analysis is to facilitate mapping of integration tests to the plugin(s) they are intended to test.
    z__init__.pymodule_utilsr   modules_.)r   rD   r   )r   r2   plugin_pathsitemsrM   
walk_filesrP   r   r)   r@   r   r   splitextrs   lstriprS   rB   
setdefaultr   )pluginsr   plugin_directoryr   plugin_directory_offsetplugin_filesplugin_pathplugin_filenameplugin_partsplugin_namer   s              r$   content_pluginsr      s    13G)5)?)?)L)L)R)R)T :%%ln44??@PQR"%&6&<&<RWW[[&I"J-/' 	MK gg..{;O&,,RWW[[9:QRTUL-/.0 gg..?B>))44	9Q"-"4"4S"9K##K0((<0K##K4;;OL#	M. %1$6$6$8	 : !k5	 !,Z#.
 !
  :3:> N :s   G)r   r&   )r   z dict[str, dict[str, PluginInfo]])*r    
__future__r   collections.abcabccdataclassesr)   typingtutilr   r   r   r   r	   r
   r   r   providerr   r   r   provider.sourcer   provider.source.unversionedr   provider.source.installedr   provider.source.unsupportedr   provider.layoutr   r   provider.layout.unsupportedr   	dataclassr   r&   r   r   r   r"   r#   r$   <module>r      s    E "   	 	 	 	 

 d#    $ f fR   d#  $ & &r#   