o
    ZhD/                     @   s  d 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Zddlm	Z	m
Z
 ddlmZmZ zddlmZ W n eyA   dd ZY nw 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u re 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 re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 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	   C/var/www/html/lang_env/lib/python3.10/site-packages/xdist/remote.pyr      s   r   c                   @   s^   e Zd ZdZdddedefddZdefd	d
ZdededdfddZ	dedd f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 r   )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__akNc                 O   s8   | j rtd| j dg|R i |dtji d S d S )N[]file)r   printr   sysstderr)r   r   r   r	   r	   r   __call__+   s   .zProducer.__call__c                 C   s   t | || jdS )Nr   )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$   zt |  W d S  ty   Y d S w r   )r   	Exceptionr
   r	   r	   r   worker_title3   s
   r*   c                   @   s   e Zd Ze Ze Zdd Zd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dd Z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   sb   || _ |jdd| _|jd | _td| j |jjd| _|| _	| 
 | _d | _|j|  d S )Nworkerid?
testrunuidzworker-r   )configworkerinputgetr,   r.   r   optiondebuglogchannel_make_queuetorunnextitem_indexZpluginmanagerregister)r   r/   r5   r	   r	   r   r   ?   s   
zWorkerInteractor.__init__c                 C   s   | j jjj S r   )r5   gateway	execmodelqueueQueuer   r	   r	   r   r6   I   s   zWorkerInteractor._make_queuec                 C   s,   | j  }|| ju r| j  }|| ju s
|S )zGets the next item from test queue. Handles the case when the queue
        is replaced concurrently in another thread.
        )r7   r1   QUEUE_REPLACED_MARK)r   resultr	   r	   r   _get_next_item_indexL   s
   



z%WorkerInteractor._get_next_item_indexc                 K   s"   |  d|| | j||f d S )NZsending)r4   r5   send)r   r   kwargsr	   r	   r   	sendeventU   s   zWorkerInteractor.sendeventc                 C   s6   t |}|dD ]}| d| q	tjd|d d S )N
zIERROR>Zinternal_error)formatted_error)r'   splitr4   
interactorrC   )r   ZexcreprrE   liner	   r	   r   pytest_internalerrorY   s   z%WorkerInteractor.pytest_internalerrorc                 C   s   || _ t }| jd|d d S )NZworkerready)
workerinfo)sessiongetinfodictrC   )r   rK   rJ   r	   r	   r   pytest_sessionstart`   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/   rO   rC   )r   rN   r	   r	   r   pytest_sessionfinishf   s   z%WorkerInteractor.pytest_sessionfinishc                 C   s   |  d d S )NZcollectionstartrC   r   rK   r	   r	   r   pytest_collectionm   s   z"WorkerInteractor.pytest_collectionc                 C   s   || j u r| j| j  d S |\}}| d|| |dkr,|d D ]}| j| q!d S |dkrCtt| jjD ]}| j| q8d S |dkrP| j| j  d S |dkr]| |d  d S d S )Nzreceived commandZruntestsindicesZruntests_allshutdownsteal)	SHUTDOWN_MARKr7   putr4   rangelenrK   itemsrW   )r   commandr   rB   ir	   r	   r   handle_commandq   s$   
zWorkerInteractor.handle_commandc                    sz   t |}g }j  _ fdd}t|d D ]}||v r'|| qj| qjd|d  j d S )Nc                      s@   t jjjjj   W  d    S 1 sw   Y  d S r   )
contextlibsuppressr5   r:   r;   r<   Empty
get_nowaitr	   Z	old_queuer   r	   r   old_queue_get_nowait_noraise   s   $z<WorkerInteractor.steal.<locals>.old_queue_get_nowait_noraiseZunscheduled)rU   )setr7   r6   iterappendrY   rC   r>   )r   rU   Zstolenre   r^   r	   rd   r   rW      s   zWorkerInteractor.stealc                 C   sL   |  d | jj| j| jd |  | _| j| jur$|   | j| jusdS )Nzentering main loop)Z	endmarkerT)r4   r5   Zsetcallbackr_   rX   r@   r8   run_one_testrS   r	   r	   r   pytest_runtestloop   s   

z#WorkerInteractor.pytest_runtestloopc                 C   s   | j | _|  | _ | jj}|| j }| j | ju rd }n|| j  }td|j  t }| j	j
j||d t | }td | jd| j|d d S )Nz[pytest-xdist running] %s)itemnextitemz[pytest-xdist idle]Zruntest_protocol_complete)
item_indexduration)r8   rm   r@   rK   r\   rX   r*   nodeidtimer/   hookZpytest_runtest_protocolrC   )r   r\   rk   rl   startrn   r	   r	   r   ri      s   



zWorkerInteractor.run_one_testc                 C   sd   | dr.|D ](}|d}|sqt|jdkr|jd n|jdd}|j d| |_qd S d S )N	loadgroupZxdist_groupr   r   default@)getvalueZget_closest_markerr[   argsrB   r1   ro   Z_nodeid)r   rK   r/   r\   rk   markgnamer	   r	   r   pytest_collection_modifyitems   s   


z.WorkerInteractor.pytest_collection_modifyitemsc                 C   sP   zt | jj}W n ty   t | jj}Y nw | jd|dd |jD d d S )NZcollectionfinishc                 S   s   g | ]}|j qS r	   )ro   ).0rk   r	   r	   r   
<listcomp>   s    z=WorkerInteractor.pytest_collection_finish.<locals>.<listcomp>)topdirZids)r'   r/   ZrootpathAttributeErrorrootdirrC   r\   )r   rK   r}   r	   r	   r   pytest_collection_finish   s   
z)WorkerInteractor.pytest_collection_finishc                 C      | j d||d d S )NZlogstartro   locationrR   r   ro   r   r	   r	   r   pytest_runtest_logstart      z(WorkerInteractor.pytest_runtest_logstartc                 C   r   )NZ	logfinishr   rR   r   r	   r	   r   pytest_runtest_logfinish   r   z)WorkerInteractor.pytest_runtest_logfinishc                 C   s^   | j jj| j |d}| j|d< | j|d< | j|d< | jj| j j|jks&J | j	d|d d S )Nr/   reportrm   Z	worker_idZtestrun_uidZ
testreportdata)
r/   rq   pytest_report_to_serializablerm   r,   r.   rK   r\   ro   rC   r   r   r   r	   r	   r   pytest_runtest_logreport   s   


z)WorkerInteractor.pytest_runtest_logreportc                 C   s0   |j s| jjj| j|d}| jd|d d S d S )Nr   Zcollectreportr   )Zpassedr/   rq   r   rC   r   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whenro   r   )rC   serialize_warning_message)r   warning_messager   ro   r   r	   r	   r   pytest_warning_recorded   s   
z(WorkerInteractor.pytest_warning_recordedN)r   r$   r%   objectrX   r>   r   r6   r@   rC   pytestZhookimplrI   rM   rQ   rT   r_   rW   rj   ri   rz   r   r   r   r   r   r   r	   r	   r	   r   r+   ;   sB    
	











r+   c           
   	   C   s   t | jtr0t| jj}t| jj}t| j}zt| jj W n t	y*   d }Y nw | jj}n	| j}d }d }d }| j
rE| j
j}| j
j}nd }d }||||||d}| jD ]&}|dv r\qUt| |}	zt|	 W n t	yv   t|	||< Y qUw |	||< qU|S )N)message_strmessage_modulemessage_class_namemessage_argscategory_modulecategory_class_name)messagecategory)
isinstancer   Warningr   r$   r   r'   r   rw   r   r   _WARNING_DETAILSgetattrrepr)
r   r   r   r   r   r   r   r?   	attr_nameattrr	   r	   r   r      sJ   



	

r   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   rL   *  s   rL   c                 C   s   | d  d t| |S )Npluginszno:terminal)rh   r   Zfromdictargs)option_dictrw   r	   r	   r   remote_initconfig7  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distrs   Fno)
rv   r2   rs   Z
looponfailZusepdbr   ZdistloadZnumprocessesZmaxprocessesbasetemp)r/   r   r	   r	   r   setup_config<  s   r   Z__channelexec__
PYTHONPATH r.   ZPYTEST_XDIST_TESTRUNUIDr,   ZPYTEST_XDIST_WORKERZworkercountZPYTEST_XDIST_WORKER_COUNTZInvocationParamsr   Zmainargv)r/   )/r&   r`   r    r   rp   typingr   r   Zexecnet.gateway_baser   r   Z_pytest.configr   r   r   ImportErrorr   r*   r+   r   rL   r   r   r   r5   Zreceiver0   rw   r   Zchange_sys_pathr   Z
importpathpathinsertpathsepenvironr1   r'   hasattrr/   basename_parserprogrO   rG   rq   Zpytest_cmdline_mainr	   r	   r	   r   <module>   sZ     ?1



