B
    ó±ô`v  ã               @   s€   d Z ddlmZmZmZ e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
„ Zdd„ Zdd„ Zdd„ ZdS )z%Analyze powershell import statements.é    )Úabsolute_importÚdivisionÚprint_functionNé   )Úread_text_file)Údisplay)Úresolve_csharp_ps_util)Údata_contextc             C   s–   t ƒ }i }x| D ]}t|j|ƒ||j< qW tdd„ |D ƒƒ}x,|D ]$}x|| D ]}||  |¡ qPW qBW x&t|ƒD ]}|| stt d| ¡ qtW |S )z¦Return a dictionary of module_utils names mapped to sets of powershell file paths.
    :type powershell_targets: list[TestTarget]
    :rtype: dict[str, set[str]]
    c             S   s   g | ]}|t ƒ f‘qS © )Úset)Ú.0Úmodule_utilr
   r
   ú`/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/powershell_import_analysis.pyú
<listcomp>&   s    z7get_powershell_module_utils_imports.<locals>.<listcomp>z0No imports found which use the "%s" module_util.)Úenumerate_module_utilsÚ'extract_powershell_module_utils_importsÚpathÚdictÚaddÚsortedr   Úwarning)Zpowershell_targetsÚmodule_utilsZimports_by_target_pathÚtargetÚimportsÚtarget_pathr   r
   r
   r   Ú#get_powershell_module_utils_imports   s    

r   c             C   s\   t ƒ jj}t ƒ jjr*dt ƒ jjj d }nd}|tj tj | |¡¡d  	tjj
d¡ }|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_pathZ
collectionÚprefixÚosr   ÚsplitextÚrelpathÚreplaceÚsep)r   Ú	base_pathr    Únamer
   r
   r   Ú get_powershell_module_utils_name3   s    

*r(   c               C   s"   t dd„ tƒ j tƒ jj¡D ƒƒS )zJReturn a list of available module_utils imports.
    :rtype: set[str]
    c             s   s*   | ]"}t j |¡d  dkrt|ƒV  qdS )r   z.psm1N)r!   r   r"   r(   )r   Úpr
   r
   r   ú	<genexpr>E   s   z)enumerate_module_utils.<locals>.<genexpr>)r   r	   r   Z
walk_filesr   r
   r
   r
   r   r   A   s    r   c       	      C   s¸   t ƒ }t| ƒ}tƒ jjr*d|kr*| d¡ | ¡ }d}x||D ]t}|d7 }t d|¡}|sZq<t	| 
d¡| ƒ}||kr~| |¡ q<tƒ jjsœ| dtƒ jj ¡r<t d| ||f ¡ q<W |S )z˜Return a list of module_utils imports found in the specified source file.
    :type path: str
    :type module_utils: set[str]
    :rtype: set[str]
    z# POWERSHELL_COMMONzAnsible.ModuleUtils.Legacyr   r   zn(?i)^#\s*(?:requires\s+-module(?:s?)|ansiblerequires\s+-powershell)\s*((?:Ansible|ansible_collections|\.)\..+)zansible_collections.%sz%%s:%d Invalid module_utils import: %s)r   r   r	   r   Z
is_ansibler   Ú
splitlinesÚreÚsearchr   ÚgroupÚ
startswithr    r   r   )	r   r   r   ÚcodeÚlinesÚline_numberÚlineÚmatchÚimport_namer
   r
   r   r   J   s$    


r   )Ú__doc__Ú
__future__r   r   r   ÚtypeÚ__metaclass__r!   r,   Úior   Úutilr   Zutil_commonr   Údatar	   r   r(   r   r   r
   r
   r
   r   Ú<module>   s   	