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 C# import statements.    )absolute_importdivisionprint_functionN   )open_text_file)display)resolve_csharp_ps_util)data_contextc             C   s   t  }i }x | D ]}t|j|d||j< qW x |D ]}t|j|d||j< q2W tdd |D }x,|D ]$}x|| D ]}|| | qtW qfW x&t|D ]}|| std|  qW |S )zReturn a dictionary of module_utils names mapped to sets of powershell file paths.
    :type powershell_targets: list[TestTarget] - C# files
    :type csharp_targets: list[TestTarget] - PS files
    :rtype: dict[str, set[str]]
    FTc             S   s   g | ]}|t  fqS  )set).0module_utilr
   r
   \/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/csharp_import_analysis.py
<listcomp>*   s    z3get_csharp_module_utils_imports.<locals>.<listcomp>z0No imports found which use the "%s" module_util.)enumerate_module_utils#extract_csharp_module_utils_importspathdictaddsortedr   warning)Zpowershell_targetsZcsharp_targetsmodule_utilsZimports_by_target_pathtargetimportstarget_pathr   r
   r
   r   get_csharp_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_csharp_pathZ
collectionprefixosr   splitextrelpathreplacesep)r   	base_pathr    namer
   r
   r   get_csharp_module_utils_name7   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.csN)r!   r   r"   r(   )r   pr
   r
   r   	<genexpr>I   s   z)enumerate_module_utils.<locals>.<genexpr>)r   r	   r   Z
walk_filesr   r
   r
   r
   r   r   E   s    r   c       
   	   C   s   t  }|rtd}n
td}t| }x~t|dD ]p\}}t||}|sPq6t|d| }	|	|krt||	 q6t	 j
js|	dt	 j
j r6td| ||	f  q6W W dQ R X |S )zReturn a list of module_utils imports found in the specified source file.
    :type path: str
    :type module_utils: set[str]
    :type is_pure_csharp: bool
    :rtype: set[str]
    z2(?i)^using\s((?:Ansible|AnsibleCollections)\..+);$zQ(?i)^#\s*ansiblerequires\s+-csharputil\s+((?:Ansible|ansible.collections|\.)\..+)r   zansible_collections.%sz%%s:%d Invalid module_utils import: %sN)r   recompiler   	enumeratesearchr   groupr   r	   r   Z
is_ansible
startswithr    r   r   )
r   r   Zis_pure_csharpr   patternZmodule_fileline_numberlinematchimport_namer
   r
   r   r   N   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   	