
    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 dd
lmZ ddZddZddZddZy)z%Analyze powershell import statements.    )annotationsN   )read_text_file)display   )resolve_csharp_ps_util)data_context)
TestTargetc                n   t               }i }| D ]%  }t        |j                  |      ||j                  <   ' |D ci c]  }|t                }}|j	                         D ]   \  }}|D ]  }||   j                  |        " t        |      D ]   }||   r	t        j                  d|z         " |S c c}w )zRReturn a dictionary of module_utils names mapped to sets of powershell file paths.z0No imports found which use the "%s" module_util.)	enumerate_module_utils'extract_powershell_module_utils_importspathsetitemsaddsortedr   warning)powershell_targetsmodule_utilsimports_by_target_pathtargetmodule_utilimportstarget_pathmoduless           `/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/classification/powershell.py#get_powershell_module_utils_importsr      s    )+L$ q.UV\VaVaco.pv{{+q KW#W;K$6#WG#W 6 < < > 2W" 	2KK $$[1	22 g ^{#OONQ\\]^ N $Xs   B2c                   t               j                  j                  }t               j                  j                  r/dt               j                  j                  j                  z   dz   }nd}|t
        j                  j                  t
        j                  j                  | |            d   j                  t
        j                  j                  d      z   }|S )z=Return a namespace and name from the given module_utils path.zansible_collections.zplugins.module_utils. r   .)r	   contentmodule_utils_powershell_path
collectionprefixosr   splitextrelpathreplacesep)r   	base_pathr$   names       r    get_powershell_module_utils_namer,   2   s    &&CCI~((',.*@*@*K*K*R*RRUllBGG$$RWW__T9%EFqIQQRTRYRYR]R]_bccDK    c                     t        d t               j                  j                  t               j                  j                        D              S )z/Return a set of available module_utils imports.c              3  |   K   | ]4  }t         j                  j                  |      d    dk(  rt        |       6 yw)r   z.psm1N)r%   r   r&   r,   ).0ps     r   	<genexpr>z)enumerate_module_utils.<locals>.<genexpr>B   s9      5''""1%a(G3 02 5s   :<)r   r	   r!   
walk_filesr"    r-   r   r   r   @   s=     5$..99,.:P:P:m:mn5 5 5r-   c                :   t               }t        |       }t               j                  j                  rd|v r|j                  d       |j                         }d}|D ]  }|dz  }t        j                  d|      }|s!t        |j                  d      |       }||v r|j                  |       Rt               j                  j                  s1|j                  dt               j                  j                  z        st        j                  d| ||fz          |S )zHReturn a set of module_utils imports found in the specified source file.z# POWERSHELL_COMMONzAnsible.ModuleUtils.Legacyr   r   zj(?i)^#\s*(?:requires\s+-modules?|ansiblerequires\s+-powershell)\s*((?:Ansible|ansible_collections|\.)\..+)zansible_collections.%sz%%s:%d Invalid module_utils import: %s)r   r   r	   r!   
is_ansibler   
splitlinesresearchr   group
startswithr$   r   r   )	r   r   r   codelinesline_numberlinematchimport_names	            r   r   r   G   s   eG$D~((-Bd-J01OOEK hq		  H  JN  O,U[[^TB,&KK$^##..&&'?,.BXBXB_B_'_`OOCt[ZeFffgh Nr-   )r   zlist[TestTarget]returnzdict[str, set[str]])r   strrB   rC   )rB   set[str])r   rC   r   rD   rB   rD   )__doc__
__future__r   r%   r8   ior   utilr   commonr   datar	   r   r
   r   r,   r   r   r4   r-   r   <module>rK      s>    + " 	 	
,5r-   