B
    `2                 @   sN  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mZ ddlmZmZmZmZ dd	lmZmZ dd
lmZ ddlmZmZ ddlmZ ddl m!Z! ej"rddl#Z$dZ%ej&'edZ(dZ)G dd deZ*dd Z+dd Z,d)ddZ-d*ddZ.d+ddZ/dd Z0dd  Z1d!d" Z2d#d$ Z3d,d%d&Z4G d'd( d(Z5dS )-z)Common logic for the coverage subcommand.    )absolute_importdivisionprint_functionN   )types)to_bytes)open_binary_fileread_json_file)ApplicationErrorcommon_environmentdisplayANSIBLE_TEST_DATA_ROOT)intercept_command
ResultType)EnvironmentConfig)Delegateinstall_command_requirements)walk_module_targets)data_context)commandtargetenvironmentversionZ
coveragerccoveragec                   s    e Zd ZdZ fddZ  ZS )CoverageConfigz'Configuration for the coverage command.c                sx   t t| |d d|kr*|jr*t|jnt | _d|kr@|jnd| _d|krT|jnd| _d|krh|jnd | _d| _	d S )Nr   group_byallFstubexport)
superr   __init__r   	frozensetsetr   r   r   r   )selfargs)	__class__ W/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/coverage/__init__.pyr    :   s     zCoverageConfig.__init__)__name__
__module____qualname____doc__r    __classcell__r&   r&   )r%   r'   r   8   s   r   c             C   s   | j rt | jrt|  yddl}W n tk
r>   d}Y nX |sLtd|j}tdd |	dD }d}d}d	}d
}||k s||krd}|std||f |S )zDelegate execution if requested, install requirements, then import and return the coverage module. Raises an exception if coverage is not available.r   NzBYou must install the "coverage" python module to use this command.c             s   s   | ]}t |V  qd S )N)int).0vr&   r&   r'   	<genexpr>U   s    z&initialize_coverage.<locals>.<genexpr>.)   r   )   r   Tz4.5.4FzZVersion %s of "coverage" is not supported. Version %s is known to work and is recommended.)
Zdelegater   requirementsr   r   ImportErrorr
   __version__tuplesplit)r$   r   Zcoverage_version_stringZcoverage_versionmin_versionZmax_versionZsupported_versionZrecommended_versionr&   r&   r'   initialize_coverageD   s,    
r:   c             C   s@   t  }|t|d ddd|dtg| }t| d||dd d	S )
z5Run the coverage cli tool with the specified options.)ZCOVERAGE_FILEpythonz-mzcoverage.__main__z--rcfiler   T)Ztarget_nameenvcmdZdisable_coverageN)r   updatedictCOVERAGE_CONFIG_PATHr   )r$   Zoutput_filer   r=   r<   r&   r&   r'   run_coverageg   s    rA   c             C   s
   t d| S )z.Return the list of Python coverage file paths.r;   )get_coverage_files)pathr&   r&   r'   get_python_coverage_filesq   s    rD   c             C   s
   t d| S )z2Return the list of PowerShell coverage file paths.Z
powershell)rB   )rC   r&   r&   r'   get_powershell_coverage_filesv   s    rE   c                s*   |p
t jj  fddt D }|S )z>Return the list of coverage file paths for the given language.c                s.   g | ]&}d |krd |krt j |qS )z
=coverage.z=%s)osrC   join)r.   f)coverage_dirlanguager&   r'   
<listcomp>~   s    z&get_coverage_files.<locals>.<listcomp>)r   ZCOVERAGErC   rF   listdir)rJ   rC   Zcoverage_filesr&   )rI   rJ   r'   rB   {   s    rB   c              C   sH   t  jjr8tdt  jjj } tdt  jjj }nd} d}| |fS )zPReturn a pair of regexes used for identifying and manipulating collection paths.z/%s/z^.*?/%s/N)r   contentZ
collectionrecompile	directory)collection_search_recollection_sub_rer&   r&   r'   get_collection_path_regexes   s    
rS   c               C   s   t dd tt D S )z<Return a dictionary of Ansible module names and their paths.c             s   s&   | ]}|j d r|j|j fV  qdS )z.pyN)rC   endswithmodule)r.   r   r&   r&   r'   r0      s    z%get_python_modules.<locals>.<genexpr>)r?   listr   r&   r&   r&   r'   get_python_modules   s    rW   c             c   s  t j| dkr&tjd|  dd dS | }y||  W nn tk
r } zPt| }|	d}W dQ R X |dkrt
dt j|   nt
d	|  dS d}~X Y nX xV| D ]J}	||	}
|
std
|	| f  qt|	|||d}	|	sq|	t|
fV  qW dS )z6Enumerate Python code coverage arcs in the given file.r   zEmpty coverage file: %sr   )	verbosityN   s   SQLitez#File created by "coverage" 5.0+: %sz%sz+No arcs found for "%s" in coverage file: %s)modulesrQ   rR   )rF   rC   getsizer   warningZCoverageData	read_file	Exceptionr   readerrorrelpathZmeasured_filesarcssanitize_filenamer"   )rC   r   rZ   rQ   rR   originalexfileheaderfilenamerb   r&   r&   r'   enumerate_python_arcs   s,    

ri   c          
   c   s   t j| dkr&tjd|  dd dS yt| }W n0 tk
rb } ztd|  dS d}~X Y nX x| D ]~\}}t	|||d}|sqnt
|tr|dstd	d
 | D }||fV  qnt
|ts|g}tdd
 |D }||fV  qnW dS )z;Enumerate PowerShell code coverage lines in the given file.r   zEmpty coverage file: %sr   )rX   Nz%s)rQ   rR   Linec             s   s   | ]\}}t ||fV  qd S )N)r-   )r.   keyvaluer&   r&   r'   r0      s    z-enumerate_powershell_lines.<locals>.<genexpr>c             s   s"   | ]}|r|d  |d fV  qdS )rj   ZHitCountNr&   )r.   hitr&   r&   r'   r0      s    )rF   rC   r[   r   r\   r	   r^   r`   itemsrc   
isinstancer?   getrV   )rC   rQ   rR   Zcoverage_runre   rh   hitsr&   r&   r'   enumerate_powershell_lines   s(    

rr   c       	      C   s>  t jdd }t jjd }t jjt jtj	j
d t jj }|dkrLi }d| krtd|| }tjd| |f dd	 |} n|r|| rt j|d
| }tjd| |f dd	 |} nztd| rtd|| }tjd| |f dd	 |} nBd| krXtdd| }||kr,td|  dS t j|| }tjd| |f dd	 |} ntd| rtdd| d}||krtd|  dS t j|| }tjd| |f dd	 |} nxtd| rtd|| }tjd| |f dd	 |} n@|| kr:tdt| d || }tjd| |f dd	 |} | S )zoConvert the given code coverage path to a local absolute path and return its, or None if the path is not valid.zlib/ansible//ZintegrationNz/ansible_modlib.zip/ansible/z^.*/ansible_modlib.zip/ansible/z%s -> %s   )rX    z$/ansible_[^/]+_payload\.zip/ansible/z'^.*/ansible_[^/]+_payload\.zip/ansible/z/ansible_module_z%^.*/ansible_module_(?P<module>.*).py$z
\g<module>z'Skipping coverage of unknown module: %sz2/ansible_[^/]+_payload(_[^/]+|\.zip)/__main__\.py$zA^.*/ansible_(?P<module>[^/]+)_payload(_[^/]+|\.zip)/__main__\.py$_z^(/.*?)?/root/ansible/z^.*z[^/]+/)rF   rC   abspathr   rM   rootseprG   r   TMPrelative_pathrN   subr   infosearchr\   rstripescape)	rh   rZ   rQ   rR   Zansible_path	root_pathZintegration_temp_pathnew_namemodule_namer&   r&   r'   rc      sV    "



rc   c               @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
PathCheckerzPChecks code coverage paths to verify they are valid and reports on the findings.Nc             C   s   || _ || _g | _d| _d S )Nr   )r$   rQ   invalid_pathsinvalid_path_chars)r#   r$   rQ   r&   r&   r'   r    (  s    zPathChecker.__init__c             C   sv   t jt|rdS | jr:| j|r:t j|dkr:dS | j| |  j	t
|7  _	| jjdkrrtd|  dS )z^Return True if the given coverage path is valid, otherwise display a warning and return False.Tz__init__.pyF   zInvalid coverage path: %s)rF   rC   isfiler   rQ   r~   basenamer   appendr   lenr$   rX   r   r\   )r#   rC   r&   r&   r'   
check_path.  s    "zPathChecker.check_pathc             C   s$   | j r td| jt| j f  dS )z<Display a warning regarding invalid paths if any were found.z7Ignored %d characters from %d invalid coverage path(s).N)r   r   r\   r   r   )r#   r&   r&   r'   report@  s    zPathChecker.report)N)r(   r)   r*   r+   r    r   r   r&   r&   r&   r'   r   &  s   
r   )N)N)N)NNN)6r+   
__future__r   r   r   type__metaclass__rF   rN   ru   r   tencodingr   ior   r	   utilr
   r   r   r   Zutil_commonr   r   configr   executorr   r   r   r   datar   TYPE_CHECKINGr   Zcoverage_moduleZCOVERAGE_GROUPSrC   rG   r@   ZCOVERAGE_OUTPUT_FILE_NAMEr   r:   rA   rD   rE   rB   rS   rW   ri   rr   rc   r   r&   r&   r&   r'   <module>   s@   #



	+)  
;