B
    ó±ô`  ã               @   s¾   d Z ddlmZmZmZ eZddlZddlm	Z
 ddlmZmZmZmZmZmZmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ G dd„ dƒZ dd„ Z!dd„ Z"dS )z?Context information for the current invocation of ansible-test.é    )Úabsolute_importÚdivisionÚprint_functionNé   )Útypes)ÚApplicationErrorÚimport_pluginsÚ	is_subdirÚANSIBLE_LIB_ROOTÚANSIBLE_TEST_ROOTÚANSIBLE_SOURCE_ROOTÚdisplay)Úfind_path_providerÚget_path_provider_classesÚProviderNotFoundForPath)ÚSourceProvider)ÚUnversionedSource)ÚInstalledSource)ÚContentLayoutÚLayoutProviderc               @   sH   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Ze	d
d„ ƒZ
dd„ ZdS )ÚDataContextzXData context providing details about the current execution environment for ansible-test.c             C   sŒ   t j d¡}t  ¡ }ttƒ}ttƒ}|| _|| _d | _	g | _
|rR|  |||d¡}n0trrt|tƒrr|  ||td¡}n|  |||d¡}|| _d S )NZANSIBLE_TEST_CONTENT_ROOTFT)ÚosÚenvironÚgetÚgetcwdr   r   r   Ú_DataContext__layout_providersÚ_DataContext__source_providersÚ_DataContext__ansible_sourceÚpayload_callbacksÚ#_DataContext__create_content_layoutr   r	   Úcontent)ÚselfZcontent_pathÚcurrent_pathÚlayout_providersÚsource_providersr    © r%   úJ/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/data.pyÚ__init__-   s    zDataContext.__init__c                s  | j }|j}|sg S tj |jd¡‰tjdˆ dd t‡fdd„t 	ˆ¡D ƒƒ}g }x¸|D ]°}tj ˆ|¡‰ t‡ fdd„t 	ˆ ¡D ƒƒ}x€|D ]x}tj ˆ |¡}|tj |j|j
¡kr¾|}	n|  | j| j|d¡}	t|	 ¡ ƒ}
|
säqtjd	|	jj|
f dd | |	¡ qW q\W |S )
zè
        Return a list of collection layouts, one for each collection in the same collection root as the current collection layout.
        An empty list is returned if the current content layout is not a collection layout.
        Zansible_collectionszScanning collection root: %sr   )Ú	verbosityc             3   s(   | ] }t j t j ˆ |¡¡r|V  qd S )N)r   ÚpathÚisdirÚjoin)Ú.0Úname)Ú	root_pathr%   r&   ú	<genexpr>P   s    z8DataContext.create_collection_layouts.<locals>.<genexpr>c             3   s(   | ] }t j t j ˆ |¡¡r|V  qd S )N)r   r)   r*   r+   )r,   r-   )Únamespace_pathr%   r&   r/   U   s    Fz#Including collection: %s (%d files))r    Ú
collectionr   r)   r+   Úrootr   ÚinfoÚsortedÚlistdirÚ	directoryr   r   r   ÚlenÚ	all_filesZ	full_nameÚappend)r!   Úlayoutr1   Znamespace_namesÚcollectionsZnamespace_nameZcollection_namesZcollection_nameZcollection_pathZcollection_layoutZ
file_countr%   )r0   r.   r&   Úcreate_collection_layoutsC   s,    

z%DataContext.create_collection_layoutsc             C   s\   t t| ||ƒ}yt t||j|ƒ}W n tk
r@   t|jƒ}Y nX | |j| |j¡¡}|S )z@Create a content layout using the given providers and root path.)r   r   r   r2   r   r   ÚcreateÚ	get_paths)r#   r$   r2   ÚwalkZlayout_providerÚsource_providerr:   r%   r%   r&   Z__create_content_layouti   s    z#DataContext.__create_content_layoutc                sÔ   t s`g }ttƒ‰| ‡fdd„ˆ ˆj¡D ƒ¡ ttƒ‰| ‡fdd„ˆ ˆj¡D ƒ¡ t|ƒS ˆ jj	r„t‡ fdd„ˆ j 
¡ D ƒƒS yttˆ jt dƒ‰W n tk
r´   tt ƒ‰Y nX t‡fdd„ˆ ˆj¡D ƒƒS )zMReturn a tuple of Ansible source files with both absolute and relative paths.c             3   s.   | ]&}t j ˆ j|¡t j d d|¡fV  qdS )ÚlibZansibleN)r   r)   r+   r2   )r,   r)   )r@   r%   r&   r/   …   s   z6DataContext.__create_ansible_source.<locals>.<genexpr>c             3   s0   | ](}t j ˆ j|¡t j d dd|¡fV  qdS )ÚtestrA   Zansible_testN)r   r)   r+   r2   )r,   r)   )r@   r%   r&   r/   ‰   s   c             3   s$   | ]}t j ˆ jj|¡|fV  qd S )N)r   r)   r+   r    r2   )r,   r)   )r!   r%   r&   r/      s    Fc             3   s"   | ]}t j ˆ j|¡|fV  qd S )N)r   r)   r+   r2   )r,   r)   )r@   r%   r&   r/   –   s    )r   r   r
   Úextendr>   r2   r   Útupler    Z
is_ansibler8   r   r   r   r   r   )r!   Úsourcesr%   )r!   r@   r&   Z__create_ansible_source   s     z#DataContext.__create_ansible_sourcec             C   s   | j s|  ¡ | _ | j S )zMReturn a tuple of Ansible source files with both absolute and relative paths.)r   Ú#_DataContext__create_ansible_source)r!   r%   r%   r&   Úansible_source˜   s    
zDataContext.ansible_sourcec             C   s   | j  |¡ dS )z$Register the given payload callback.N)r   r9   )r!   Úcallbackr%   r%   r&   Úregister_payload_callback    s    z%DataContext.register_payload_callbackN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r<   Ústaticmethodr   rF   ÚpropertyrG   rI   r%   r%   r%   r&   r   +   s   &r   c              C   sv   d} x| D ]}t d| ƒ q
W y
tƒ }W nH tk
rp   dg}trR| ddt ¡ tdd |¡t ¡ f ƒ‚Y nX |S )zInitialize provider plugins.)r:   Úsourcezprovider/%szM - an Ansible collection: {...}/ansible_collections/{namespace}/{collection}/r   z - the Ansible source: %s/zUThe current working directory must be at or below:

%s

Current working directory: %sÚ
)	r   r   r   r   Úinsertr   r+   r   r   )Zprovider_typesZprovider_typeÚcontextÚoptionsr%   r%   r&   Ú	data_init¥   s    

rU   c               C   s*   yt jS  tk
r$   tƒ t _t jS X dS )z Return the current data context.N)Údata_contextÚinstanceÚAttributeErrorrU   r%   r%   r%   r&   rV   Â   s
    rV   )#rM   Ú
__future__r   r   r   ÚtypeÚ__metaclass__r   Ú r   ÚtÚutilr   r   r	   r
   r   r   r   Zproviderr   r   r   Zprovider.sourcer   Zprovider.source.unversionedr   Zprovider.source.installedr   Zprovider.layoutr   r   r   rU   rV   r%   r%   r%   r&   Ú<module>   s   $
z