B
    `jA              	   @   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	Z	ddl
Z
ddl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mZmZmZmZmZmZ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& i Z'i Z(i Z)G dd dZ*e*+  G dd dZ,G dd dZ-dd Z.dd Z/dd Z0dd Z1dd Z2dd Z3dd Z4dd  Z5ej6d!d" Z7d:d$d%Z8d&d' Z9d(d) Z:d;d*d+Z;d,d- Z<d.d/ Z=d<d0d1Z>d=d3d4Z?d5d6 Z@d>d8d9ZAdS )?z1Common utility code that depends on CommonConfig.    )absolute_importdivisionprint_functionN   )types)to_bytes)common_environmentCOVERAGE_CONFIG_NAMEdisplayfind_pythonremove_treeMODE_DIRECTORYMODE_FILE_EXECUTEPYTHON_PATHSraw_commandread_lines_without_commentsANSIBLE_TEST_DATA_ROOTApplicationError)write_text_filewrite_json_file)data_context)LayoutMessagesc               @   s`   e Zd ZdZdZdZdZdZdZdZ	dZ
edd Zdd Zedd Zed	d
 Zdd ZdS )
ResultTypezTest result type.Nc               C   sJ   t dt _t dt _t dt _t dt _t dt _t dt _t dt _d S )NZbotcoveragedataZjunitZlogsZreportsz.tmp)r   BOTCOVERAGEDATAJUNITLOGSREPORTSTMP r"   r"   Q/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/util_common.py	_populate>   s    





zResultType._populatec             C   s
   || _ d S )N)name)selfr%   r"   r"   r#   __init__H   s    zResultType.__init__c             C   s   t jt jj| jS )z)The content relative path to the results.)ospathjoinr   contentresults_pathr%   )r&   r"   r"   r#   relative_pathK   s    zResultType.relative_pathc             C   s   t jt jj| jS )z!The absolute path to the results.)r(   r)   r*   r   r+   rootr-   )r&   r"   r"   r#   r)   P   s    zResultType.pathc             C   s   | j S )N)r%   )r&   r"   r"   r#   __str__U   s    zResultType.__str__)__name__
__module____qualname____doc__r   r   r   r   r   r    r!   staticmethodr$   r'   propertyr-   r)   r/   r"   r"   r"   r#   r   4   s   
r   c               @   s    e Zd ZdZdd Zdd ZdS )CommonConfigz%Configuration common to all commands.c             C   sF   || _ |j| _|j| _|j| _|j| _|j| _|j| _d| _i | _dS )z<
        :type args: any
        :type command: str
        FN)	commandcolorexplain	verbositydebugtruncateZredactZinfo_stderrcache)r&   argsr7   r"   r"   r#   r'   _   s    zCommonConfig.__init__c             C   s   t jtdS )z;Return the path to the Ansible config for the given config.zansible.cfg)r(   r)   r*   r   )r&   r"   r"   r#   get_ansible_configq   s    zCommonConfig.get_ansible_configN)r0   r1   r2   r3   r'   r?   r"   r"   r"   r#   r6   ]   s   r6   c               @   s   e Zd ZdZdd ZdS )NetworkPlatformSettingsz6Settings required for provisioning a network platform.c             C   s   || _ || _d S )N)
collectioninventory_vars)r&   rA   rB   r"   r"   r#   r'   x   s    z NetworkPlatformSettings.__init__N)r0   r1   r2   r3   r'   r"   r"   r"   r#   r@   v   s   r@   c               C   s
   t tdS )z+
    :rtype: dict[str, dict[str, str]]
    Zdocker)get_parameterized_completionDOCKER_COMPLETIONr"   r"   r"   r#   get_docker_completion}   s    rE   c               C   s
   t tdS )z+
    :rtype: dict[str, dict[str, str]]
    remote)rC   REMOTE_COMPLETIONr"   r"   r"   r#   get_remote_completion   s    rH   c               C   s
   t tdS )z+
    :rtype: dict[str, dict[str, str]]
    network)rC   NETWORK_COMPLETIONr"   r"   r"   r#   get_network_completion   s    rK   c                s^   | sZt  jjrd nd ttjtdd| dd}| t	 fddd	d
 |D D  | S )zj
    :type cache: dict[str, dict[str, str]]
    :type name: str
    :rtype: dict[str, dict[str, str]]
    rA   zansible-base
completionz%s.txtT)Zremove_blank_linesc             3   s*   | ]"}|r|d   d  kr|V  qdS )r   contextN)get).0kvp)rM   r"   r#   	<genexpr>   s    z/get_parameterized_completion.<locals>.<genexpr>c             S   s   g | ]}t |qS r"   )parse_parameterized_completion)rO   ir"   r"   r#   
<listcomp>   s    z0get_parameterized_completion.<locals>.<listcomp>)
r   r+   rA   r   r(   r)   r*   r   updatedict)r=   r%   Zimagesr"   )rM   r#   rC      s    
&rC   c             C   sD   |   }|sdS |d }tdd dd |dd D D }||fS )zbParse the given completion entry, returning the entry name and a dictionary of key/value settings.Nr   c             s   s.   | ]&}|d  t |dkr |d ndfV  qdS )r   r    N)len)rO   rP   r"   r"   r#   rQ      s    z1parse_parameterized_completion.<locals>.<genexpr>c             S   s   g | ]}| d dqS )=r   )split)rO   itemr"   r"   r#   rT      s    z2parse_parameterized_completion.<locals>.<listcomp>r   )rZ   rV   )valuevaluesr%   r   r"   r"   r#   rR      s    $rR   c             C   s   t  | i }|d| S )z)
    :type name: str
    :rtype: str
    r%   )rE   rN   )r%   configr"   r"   r#   docker_qualify_image   s    r_   c          	   C   sd   d||f }t  |i }| j||d}t|t| j||d|rVd||f n|d}|S )z<Returns settings for the given network platform and version.z%s/%srA   
connectionz%s.%s)Zansible_connectionZansible_network_os)rK   rN   Zplatform_collectionr@   rV   Zplatform_connection)r>   platformversionplatform_versionrL   rA   settingsr"   r"   r#   get_network_settings   s    re   c             C   sZ   | sdS x| j D ]}tj |dd qW x| jD ]}t| q.W | jrVtd| jdS )z"Display the given layout messages.Nr   )r:   
)infor
   warningerrorr   r*   )messagesmessager"   r"   r#   handle_layout_messages   s    rl   c          	   c   s^   | j r tj|d||f V  n:tj|||d$}|t| |  |j	V  W dQ R X dS )z
    :param args: CommonConfig
    :param prefix: str
    :param suffix: str
    :param directory: str
    :param content: str | bytes | unicode
    :rtype: str
    z%stemp%s)prefixsuffixdirN)
r9   r(   r)   r*   tempfileNamedTemporaryFilewriter   flushr%   )r>   rm   rn   	directoryr+   Ztempfile_fdr"   r"   r#   named_temporary_file   s    
ru   Tc             C   s&   t j| j|}t||d||d dS )z`Write the given json content to the specified test results path, creating directories as needed.T)create_directories	formattedencoderN)r(   r)   r*   r   )categoryr%   r+   rw   rx   r)   r"   r"   r#   write_json_test_results   s    rz   c             C   s"   t j| j|}t||dd dS )z`Write the given text content to the specified test results path, creating directories as needed.T)rv   N)r(   r)   r*   r   )ry   r%   r+   r)   r"   r"   r#   write_text_test_results   s    r{   c             C   s   t |}|r|S d}d}d}| jr>tj|d|d|fS tj|||d}tj|d}tjtj	|tj|k}|rt
jd||f d	d
 t|| n t
jd||f d	d
 t|| t|t t stt |t |< |S )zK
    :type args: TestConfig
    :type interpreter: str
    :rtype: str
    zpython-z-ansiblez/tmprW   temp)rm   rn   ro   pythonz4Injecting "%s" as a symlink to the "%s" interpreter.r   )r:   z;Injecting "%s" as a execv wrapper for the "%s" interpreter.)r   rN   r9   r(   r)   r*   rp   mkdtempdirnamerealpathr
   rg   symlinkcreate_interpreter_wrapperchmodr   atexitregistercleanup_python_paths)r>   interpreterpython_pathrm   rn   Zroot_temp_dirinjected_interpreterZuse_symlinkr"   r"   r#   get_python_path  s*    
 

r   c             C   s(   t j| p
d|pd|d}tt| |S )zKCreate a temporary directory that persists until the current process exits.tmprW   )rm   rn   ro   )rp   r~   r   r   r   )rm   rn   base_dir	temp_pathr"   r"   r#   create_temp_dir.  s    r   c             C   s6   t j}td|| f  }t|| t|t dS )zHCreate a wrapper for the given Python interpreter at the specified path.z
    #!%s

    from __future__ import absolute_import

    from os import execv
    from sys import argv

    python = '%s'

    execv(python, [python] + argv[1:])
    N)	sys
executabletextwrapdedentlstripr   r(   r   r   )r   r   Zshebang_interpretercoder"   r"   r#   r   5  s
    
r   c              C   s6   x0t t D ] } tjd|  dd t|  qW dS )z*Clean up all temporary python directories.z*Cleaning up temporary python directory: %s   )r:   N)sortedr   r]   r
   rg   shutilrmtree)r)   r"   r"   r#   r   M  s    r   c          
   C   s  |r|}|}n0| j r6| j }tjt jjt jj}ntdtj|t	}tj|t
jjd| j|| jpnd| d| f }	| jstj|std| | jrd}	t||	d}
|r
|
t||	d |r
tj|d	| j|| jpd
f |
d< tjt jjd|
d< |
S )z
    :type args: TestConfig
    :type target_name: str
    :type version: str
    :type temp_path: str
    :type module_coverage: bool
    :type remote_temp_path: str | None
    :rtype: dict[str, str]
    zXNo temp path and no coverage config base path. Check for missing coverage_context usage.z%s=%s=%s=%s=coveragezlocal-%sz	python-%sz Missing coverage config file: %srW   )ZCOVERAGE_CONFZCOVERAGE_FILE)Z_ANSIBLE_COVERAGE_CONFIGZ_ANSIBLE_COVERAGE_OUTPUTz%s=%s=%srF   Z_ANSIBLE_COVERAGE_REMOTE_OUTPUT*Z"_ANSIBLE_COVERAGE_REMOTE_WHITELIST)coverage_config_base_pathr(   r)   r*   r   r+   r.   r,   	Exceptionr	   r   r   r%   r7   Zcoverage_labelr9   existsZcoverage_checkrV   rU   )r>   target_namerb   r   module_coverageremote_temp_pathr   Zcoverage_output_base_pathconfig_fileZcoverage_fileenvr"   r"   r#   get_coverage_environmentT  s4    
 

r   Fc          
   C   s   |st  }n| }t|}|p$| j}|
p0t|}tjtd}|
s^t	| |}|tjj
 | }|tjj
 |d  |d< ||d< ||d< | jr|s|t| ||||	|d t| |||||dS )a  
    :type args: TestConfig
    :type cmd: collections.Iterable[str]
    :type target_name: str
    :type env: dict[str, str]
    :type capture: bool
    :type data: str | None
    :type cwd: str | None
    :type python_version: str | None
    :type temp_path: str | None
    :type module_coverage: bool
    :type virtualenv: str | None
    :type disable_coverage: bool
    :type remote_temp_path: str | None
    :rtype: str | None, str | None
    ZinjectorPATHZANSIBLE_TEST_PYTHON_VERSIONZANSIBLE_TEST_PYTHON_INTERPRETER)r   )capturer   r   cwd)r   copylistpython_versionr   r(   r)   r*   r   r   pathsepr   rU   r   run_command)r>   cmdr   r   r   r   r   r   r   r   
virtualenvZdisable_coverager   rb   r   Zinject_pathr   r"   r"   r#   intercept_command  s"    



r   c             C   st   t  jjs| ds| S | d}|tjj}x|D ]}|rB|rDP |d= q6W dt  jjd	|dd |D  f S )z4
    :type import_name: str
    :type path: str
    .zansible_collections.%s%sc             S   s   g | ]}|r|qS r"   r"   )rO   pr"   r"   r#   rT     s    z*resolve_csharp_ps_util.<locals>.<listcomp>)
r   r+   Z
is_ansible
startswithrZ   r(   r)   seprm   r*   )import_namer)   packagesZmodule_packagespackager"   r"   r#   resolve_csharp_ps_util  s    



r   strictc             C   s(   | j o
| }t|||||||||	|
d
S )ai  
    :type args: CommonConfig
    :type cmd: collections.Iterable[str]
    :type capture: bool
    :type env: dict[str, str] | None
    :type data: str | None
    :type cwd: str | None
    :type always: bool
    :type stdin: file | None
    :type stdout: file | None
    :type cmd_verbosity: int
    :type str_errors: str
    :rtype: str | None, str | None
    )	r   r   r   r   r9   stdinstdoutcmd_verbosity
str_errors)r9   r   )r>   r   r   r   r   r   alwaysr   r   r   r   r9   r"   r"   r#   r     s    r   )TN)NNN)N)	FNNNNTNFN)	FNNNFNNr   r   )Br3   
__future__r   r   r   type__metaclass__r   
contextlibr(   r   r   rp   r   rW   r   tencodingr   utilr   r	   r
   r   r   r   r   r   r   r   r   r   ior   r   r   r   Zprovider.layoutr   rD   rG   rJ   r   r$   r6   r@   rE   rH   rK   rC   rR   r_   re   rl   contextmanagerru   rz   r{   r   r   r   r   r   r   r   r   r"   r"   r"   r#   <module>   sX   8&
 
-

> 
- 