B
    |?Xc)                 @   s  d Z ddlZddlZddlZddlmZ ddlZddlmZm	Z	 ddl
mZmZ yddlmZ W n ek
r|   dd ZY nX G d	d
 d
Zdd ZG dd dZdd Zdd Zdd Zdd ZedkreZe \ZZZZedkr e Zej de eej! ej"#dd ej"d< nee_ed ej"d< ed ej"d< e$ed ej"d< e%ed rleedZ&neeeZ&ee&_ee&e#d! ej'ed" d e&j(_)ee&_i e&_*ee&eZ+e&j,j-e&d# dS )$aC  
    This module is executed in remote subprocesses and helps to
    control a remote testing session and relay back information.
    It assumes that 'py' is importable and does not have dependencies
    on the rest of the xdist code.  This means that the xdist-plugin
    needs not to be installed in remote environments.
    N)Any)dumps	DumpError)_prepareconfigConfig)setproctitlec             C   s   d S )N )titler   r   +lib/python3.7/site-packages/xdist/remote.pyr      s    r   c               @   sV   e Zd ZdZddeedddZeddd	Zeed
dddZ	ed dddZ
d
S )Producera  
    Simplified implementation of the same interface as py.log, for backward compatibility
    since we dropped the dependency on pylib.
    Note: this is defined here because this module can't depend on xdist, so we need
    to have the other way around.
    T)enabled)namer   c            C   s   || _ || _d S )N)r   r   )selfr   r   r   r   r
   __init__#   s    zProducer.__init__)returnc             C   s    t | j d| jd| j dS )N(z
, enabled=))type__name__r   r   )r   r   r   r
   __repr__'   s    zProducer.__repr__N)akr   c             O   s.   | j r*td| j df||dtji d S )N[]file)r   printr   sysstderr)r   r   r   r   r   r
   __call__*   s    zProducer.__call__)r   r   c             C   s   t | || jdS )N)r   )r   r   )r   r   r   r   r
   __getattr__.   s    zProducer.__getattr__)r   
__module____qualname____doc__strboolr   r   r   r   r   r   r   r   r
   r      s
   r   c             C   s&   yt |  W n tk
r    Y nX d S )N)r   	Exception)r	   r   r   r
   worker_title2   s    r&   c               @   s   e Zd Zdd Zdd Zejdd Zejdd Zejd	d
dd Z	ejdd Z
ejdd Zdd Zdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd  Zd!S )"WorkerInteractorc             C   sR   || _ |jdd| _|jd | _td| j |jjd| _|| _	|j
|  d S )Nworkerid?
testrunuidzworker-)r   )configworkerinputgetr(   r*   r   optiondebuglogchannelZpluginmanagerregister)r   r+   r1   r   r   r
   r   ;   s    zWorkerInteractor.__init__c             K   s"   |  d|| | j||f d S )NZsending)r0   r1   send)r   r   kwargsr   r   r
   	sendeventC   s    zWorkerInteractor.sendeventc             C   s:   t |}x|dD ]}| d| qW tjd|d d S )N
zIERROR>Zinternal_error)formatted_error)r#   splitr0   
interactorr5   )r   Zexcreprr7   liner   r   r
   pytest_internalerrorG   s    z%WorkerInteractor.pytest_internalerrorc             C   s   || _ t }| jd|d d S )NZworkerready)
workerinfo)sessiongetinfodictr5   )r   r=   r<   r   r   r
   pytest_sessionstartN   s    z$WorkerInteractor.pytest_sessionstartT)Zhookwrapperc             c   s,   t || jjd< d V  | jd| jjd d S )N
exitstatusZworkerfinished)workeroutput)intr+   rA   r5   )r   r@   r   r   r
   pytest_sessionfinishT   s    z%WorkerInteractor.pytest_sessionfinishc             C   s   |  d d S )NZcollectionstart)r5   )r   r=   r   r   r
   pytest_collection[   s    z"WorkerInteractor.pytest_collectionc             C   s   |  d g }xy| j \}}W n tk
r6   dS X |  d|| |dkr^||d  n|dkrz|tt|j |  d| xt|dkr| | qW |d	kr|r| | P qW dS )
Nzentering main loopTzreceived commandZruntestsindicesZruntests_allzitems to run:   Zshutdown)	r0   r1   receiveEOFErrorextendrangelenitemsrun_one_test)r   r=   torunr   r4   r   r   r
   pytest_runtestloop_   s(    

z#WorkerInteractor.pytest_runtestloopc             C   s   | j j}|d| _|| j }|r0||d  }nd }td|j  t }| jjj	||d t | }td | j
d| j|d d S )Nr   z[pytest-xdist running] %s)itemnextitemz[pytest-xdist idle]Zruntest_protocol_complete)
item_indexduration)r=   rL   poprR   r&   nodeidtimer+   hookZpytest_runtest_protocolr5   )r   rN   rL   rP   rQ   startrS   r   r   r
   rM   w   s    
zWorkerInteractor.run_one_testc             C   sb   | dr^xR|D ]J}|d}|s$qt|jdkr<|jd n|jdd}d|j||_qW d S )N	loadgroupZxdist_groupr   r   defaultz{}@{})	getvalueZget_closest_markerrK   argsr4   r-   formatrU   Z_nodeid)r   r=   r+   rL   rP   ZmarkZgnamer   r   r
   pytest_collection_modifyitems   s    


z.WorkerInteractor.pytest_collection_modifyitemsc             C   sR   yt | jj}W n  tk
r0   t | jj}Y nX | jd|dd |jD d d S )NZcollectionfinishc             S   s   g | ]
}|j qS r   )rU   ).0rP   r   r   r
   
<listcomp>   s    z=WorkerInteractor.pytest_collection_finish.<locals>.<listcomp>)topdirZids)r#   r+   ZrootpathAttributeErrorZrootdirr5   rL   )r   r=   ra   r   r   r
   pytest_collection_finish   s    z)WorkerInteractor.pytest_collection_finishc             C   s   | j d||d d S )NZlogstart)rU   location)r5   )r   rU   rd   r   r   r
   pytest_runtest_logstart   s    z(WorkerInteractor.pytest_runtest_logstartc             C   s   | j d||d d S )NZ	logfinish)rU   rd   )r5   )r   rU   rd   r   r   r
   pytest_runtest_logfinish   s    z)WorkerInteractor.pytest_runtest_logfinishc             C   s^   | j jj| j |d}| j|d< | j|d< | j|d< | jj| j j|jksLt	| j
d|d d S )N)r+   reportrR   Z	worker_idZtestrun_uidZ
testreport)data)r+   rW   pytest_report_to_serializablerR   r(   r*   r=   rL   rU   AssertionErrorr5   )r   rg   rh   r   r   r
   pytest_runtest_logreport   s    


z)WorkerInteractor.pytest_runtest_logreportc             C   s,   |j s(| jjj| j|d}| jd|d d S )N)r+   rg   Zcollectreport)rh   )Zpassedr+   rW   ri   r5   )r   rg   rh   r   r   r
   pytest_collectreport   s    z%WorkerInteractor.pytest_collectreportc             C   s   | j dt||||d d S )NZwarning_recorded)Zwarning_message_datawhenrU   rd   )r5   serialize_warning_message)r   warning_messagerm   rU   rd   r   r   r
   pytest_warning_recorded   s    z(WorkerInteractor.pytest_warning_recordedN)r   r    r!   r   r5   pytestZhookimplr;   r?   rC   rD   rO   rM   r^   rc   re   rf   rk   rl   rp   r   r   r   r
   r'   :   s   	r'   c       
   	   C   s  t | jtrbt| jj}t| jj}t| j}yt| jj W n t	k
rV   d }Y qtX | jj}n| j}d }d }d }| j
r| j
j}| j
j}nd }d }||||||d}xX| jD ]N}|dkrqt| |}	yt|	 W n  t	k
r   t|	||< Y qX |	||< qW |S )N)message_strmessage_modulemessage_class_namemessage_argscategory_modulecategory_class_name)messagecategory)
isinstancerx   Warningr   r    r   r#   r   r\   r   ry   _WARNING_DETAILSgetattrrepr)
ro   rs   rt   rr   ru   rv   rw   resultZ	attr_nameattrr   r   r
   rn      sD    




rn   c              C   s0   dd l } ttjttjtj |   tjt dS )Nr   )versionversion_infoZsysplatformplatform
executablecwd)	r   dictr   r   tupler   r   osgetcwd)r   r   r   r
   r>      s    r>   c             C   s   | d  d t| |S )NZpluginszno:terminal)appendr   Zfromdictargs)option_dictr\   r   r   r
   remote_initconfig  s    r   c             C   sN   |  ddk| j_d| j_d| j_d| j_d| j_d | j_d | j_|| j_	d S )NdistrY   Fno)
r[   r.   rY   Z
looponfailZusepdbr   ZdistloadZnumprocessesZmaxprocessesbasetemp)r+   r   r   r   r
   setup_config  s    r   Z__channelexec__Z
PYTHONPATH r*   ZPYTEST_XDIST_TESTRUNUIDr(   ZPYTEST_XDIST_WORKERZworkercountZPYTEST_XDIST_WORKER_COUNTZInvocationParamsr   Zmainargv)r+   ).r"   r   r   rV   typingr   rq   Zexecnet.gateway_baser   r   Z_pytest.configr   r   r   ImportErrorr   r&   r'   rn   r>   r   r   r   r1   rG   r,   r\   r   Zchange_sys_pathr   Z
importpathpathinsertpathsepenvironr-   r#   hasattrr+   basenameZ_parserprogrA   r9   rW   Zpytest_cmdline_mainr   r   r   r
   <module>   sP    1

 

