a
    4ñ»je9  ã                   @  sn  U d Z ddlmZ ddlZddlmZ ddlmZ ddlm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 dd
lmZ ddlZddlmZ ddlZddlZzddlmZ W n" eyì   dddœdd„ZY n0 G dd„ dƒZdddœdd„ZG dd„ de	jƒZG dd„ dƒZG dd„ dƒZdddœdd „ZG d!d"„ d"eƒZ d"d#œd$d%„Z!d&d'dd(œd)d*„Z"e#d+k�rje$Z$d,e%d-< e$ &¡ \Z'Z(Z)Z*e*du �ràe
 +¡ Z,ej- .de,¡ e,e
j/ e
j0 1d.d/¡ e
j0d.< ne*e_-e'd0 e
j0d1< e'd2 e
j0d3< e2e'd4 ƒe
j0d5< ee(dƒZ3e"e3e) 1d6¡ƒ e
j- 4e'd7 d ¡e3j5_6e'e3_'i e3_7ee3e$ƒZ8e3j9j:e3d8� dS )9a/  
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.
é    )ÚannotationsN)Ú	Generator)ÚIterable)ÚSequence)ÚAny)ÚLiteral)Ú	TypedDict)ÚUnion)Ú_prepareconfig)ÚsetproctitleÚstrÚNone)ÚtitleÚreturnc                 C  s   d S ©N© ©r   r   r   ú2E:\soccer\ml_env\Lib\site-packages\xdist/remote.pyr   #   s    r   c                   @  sX   e Zd ZdZddœddddœdd	„Zdd
œdd„Zddddœdd„Zd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©Úenabledr   Úboolr   )Únamer   r   c                C  s   || _ || _d S r   )r   r   )Úselfr   r   r   r   r   Ú__init__/   s    zProducer.__init__©r   c                 C  s    t | ƒj› d| j›d| j› d�S )Nú(z
, enabled=ú))ÚtypeÚ__name__r   r   ©r   r   r   r   Ú__repr__3   s    zProducer.__repr__r   )ÚaÚkr   c                 O  s4   | j r0td| j› d�g|¢R i |¤dtji¤Ž d S )Nú[ú]Úfile)r   Úprintr   ÚsysÚstderr)r   r"   r#   r   r   r   Ú__call__6   s    zProducer.__call__)r   r   c                 C  s   t | ƒ|| jd�S )Nr   )r   r   )r   r   r   r   r   Ú__getattr__:   s    zProducer.__getattr__N)r   Ú
__module__Ú__qualname__Ú__doc__r   r!   r*   r+   r   r   r   r   r   '   s
   r   c                 C  s$   zt | ƒ W n ty   Y n0 d S r   )r   Ú	Exceptionr   r   r   r   Úworker_title>   s    r0   c                   @  s   e Zd ZdZdS )ÚMarkerr   N)r   r,   r-   ÚSHUTDOWNr   r   r   r   r1   F   s   r1   c                   @  sr   e Zd ZdZeeeej f Z	ddœdd„Z
ddœdd	„Zdd
dœdd„Zdd
dœdd„Zejddœdd„ƒZdS )Ú	TestQueuezcA simple queue that can be inspected and modified while the lock is held via the ``lock()`` method.zexecnet.gateway_base.ExecModel)Ú	execmodelc                 C  s"   t  ¡ | _| ¡ | _| ¡ | _d S r   )ÚcollectionsÚdequeÚ_itemsÚRLockÚ_lockÚEventÚ_has_items_event)r   r4   r   r   r   r   O   s    

zTestQueue.__init__ÚItemr   c                 C  sR   |   ¡ �*}|r$| ¡ W  d   ƒ S W d   ƒ n1 s80    Y  | j ¡  q d S r   )ÚlockÚpopleftr;   Úwait)r   Úlocked_itemsr   r   r   ÚgetT   s    
4zTestQueue.getr   )Úitemr   c                 C  s6   |   ¡ �}| |¡ W d   ƒ n1 s(0    Y  d S r   )r=   Úappend)r   rB   r@   r   r   r   Úput\   s    
zTestQueue.putzIterable[Item])Úiterabler   c                 C  s8   |   ¡ � t |¡| _W d   ƒ n1 s*0    Y  d S r   )r=   r5   r6   r7   )r   rE   r   r   r   Úreplace`   s    
zTestQueue.replacez"Generator[collections.deque[Item]]c              	   c  sr   | j �X z(| jV  W | jr&| j ¡  qP| j ¡  n| jrD| j ¡  n
| j ¡  0 W d   ƒ n1 sd0    Y  d S r   )r9   r7   r;   ÚsetÚclearr    r   r   r   r=   d   s    
ýzTestQueue.lockN)r   r,   r-   r.   r	   Úintr   r1   r2   r<   r   rA   rD   rF   Ú
contextlibÚcontextmanagerr=   r   r   r   r   r3   J   s   r3   c                   @  sp  e Zd Zddddœdd„Zdddd	œd
d„Zejdddœdd„ƒZejdddœdd„ƒZejdd�dddœdd„ƒZ	ejddœdd„ƒZ
dddœdd „Zd!dd"œd#d$„Zejdd%dœd&d'„ƒZddœd(d)„Zdd*dd+œd,d-„Zejdddœd.d/„ƒZejdd0dd1œd2d3„ƒZejdd0dd1œd4d5„ƒZejd6dd7œd8d9„ƒZejd:dd7œd;d<„ƒZejd=ddd>dd?œd@dA„ƒZdBS )CÚWorkerInteractorúpytest.Configúexecnet.Channelr   )ÚconfigÚchannelr   c                 C  sj   || _ |j}| dd¡| _|d | _td| j› �|jjd�| _|| _	t
| j	jjƒ| _d | _|j | ¡ d S )NÚworkeridú?Ú
testrunuidzworker-r   )rO   ÚworkerinputrA   rQ   rS   r   ÚoptionÚdebugÚlogrP   r3   Zgatewayr4   ÚtorunÚnextitem_indexZpluginmanagerÚregister)r   rO   rP   rT   r   r   r   r   q   s    
zWorkerInteractor.__init__r   Úobject)r   Úkwargsr   c                 K  s"   |   d||¡ | j ||f¡ d S )NZsending)rW   rP   Úsend)r   r   r\   r   r   r   Ú	sendevent|   s    zWorkerInteractor.sendevent)Úexcreprr   c                 C  s6   t |ƒ}| d¡D ]}|  d|¡ qtjd|d� d S )NÚ
zIERROR>Zinternal_error)Úformatted_error)r   ÚsplitrW   Ú
interactorr^   )r   r_   ra   Úliner   r   r   Úpytest_internalerror€   s    z%WorkerInteractor.pytest_internalerrorzpytest.Session)Úsessionr   c                 C  s   || _ tƒ }| jd|d� d S )NZworkerready)Ú
workerinfo)rf   Úgetinfodictr^   )r   rf   rg   r   r   r   Úpytest_sessionstart‡   s    z$WorkerInteractor.pytest_sessionstartT)ZhookwrapperrI   zGenerator[None, object, None])Ú
exitstatusr   c                 c  sD   | j j}t|ƒ|d< | jj|d< | jj|d< d V  | jd|d� d S )Nrj   Ú
shouldfailÚ
shouldstopZworkerfinished)Úworkeroutput)rO   rm   rI   rf   rk   rl   r^   )r   rj   rm   r   r   r   Úpytest_sessionfinish�   s    z%WorkerInteractor.pytest_sessionfinishr   c                 C  s   |   d¡ d S )NZcollectionstart©r^   r    r   r   r   Úpytest_collection—   s    z"WorkerInteractor.pytest_collectionz5tuple[str, dict[str, Any]] | Literal[Marker.SHUTDOWN])Úcommandr   c                 C  s´   |t ju r| j t j¡ d S |\}}|  d||¡ |dkrV|d D ]}| j |¡ qBnZ|dkr‚tt| jjƒƒD ]}| j |¡ qnn.|dkrš| j t j¡ n|dkr°|  	|d ¡ d S )Nzreceived commandZruntestsÚindicesZruntests_allÚshutdownÚsteal)
r1   r2   rX   rD   rW   ÚrangeÚlenrf   Úitemsrt   )r   rq   r   r\   Úir   r   r   Úhandle_command›   s    
zWorkerInteractor.handle_commandzSequence[int])rr   r   c                   sŠ   t |ƒ‰ | j ¡ �V}t‡ fdd„|D ƒƒ}t|ƒtˆ ƒkrV| j ‡ fdd„|D ƒ¡ ng }W d  ƒ n1 sn0    Y  | jd|d� dS )a   
        Remove tests from the queue.

        Removes either all requested tests, or none, if some of these tests
        are not in the queue (for example, if they were processed already).

        :param indices: indices of the tests to remove.
        c                 3  s   | ]}|ˆ v r|V  qd S r   r   ©Ú.0rB   ©Zrequested_setr   r   Ú	<genexpr>¼   ó    z)WorkerInteractor.steal.<locals>.<genexpr>c                 3  s   | ]}|ˆ vr|V  qd S r   r   rz   r|   r   r   r}   À   s   NZunscheduled)rr   )rG   rX   r=   Úlistrv   rF   r^   )r   rr   Zlocked_queueZstolenr   r|   r   rt   °   s    	ÿ
"zWorkerInteractor.stealr   c                 C  sR   |   d¡ | jj| jtjd� | j ¡ | _| jtjurN|  	¡  |j
sN|jr*qNq*dS )Nzentering main loop)Z	endmarkerT)rW   rP   Zsetcallbackry   r1   r2   rX   rA   rY   Úrun_one_testrk   rl   ©r   rf   r   r   r   Úpytest_runtestloopÈ   s    
z#WorkerInteractor.pytest_runtestloopc                 C  s²   t | jtƒsJ ‚| j| _| j ¡ | _| jj}|| j }| jtj	u rHd }n| jd usVJ ‚|| j }t
d|j ƒ t ¡ }| jjj||d� t ¡ | }t
dƒ | jd| j|d� d S )Nz[pytest-xdist running] %s)rB   Únextitemz[pytest-xdist idle]Zruntest_protocol_complete)Ú
item_indexÚduration)Ú
isinstancerY   rI   r„   rX   rA   rf   rw   r1   r2   r0   ÚnodeidÚtimeÚperf_counterrO   ÚhookZpytest_runtest_protocolr^   )r   rw   rB   rƒ   Ústartr…   r   r   r   r€   Ó   s"    

ÿzWorkerInteractor.run_one_testzlist[pytest.Item])rO   rw   r   c                 C  s„   |  d¡r€|D ]p}tƒ }| d¡D ]8}t|jƒdkr>|jd n|j dd¡}| t|ƒ¡ q"|sbq|j	› dd 
t|ƒ¡› �|_qd S )NÚ	loadgroupZxdist_groupr   r   Údefaultú@Ú_)ÚgetvaluerG   Ziter_markersrv   Úargsr\   rA   Úaddr   r‡   ÚjoinÚsortedZ_nodeid)r   rO   rw   rB   ZgnamesÚmarkr   r   r   r   Úpytest_collection_modifyitemsì   s    
ÿýz.WorkerInteractor.pytest_collection_modifyitemsc                 C  s(   | j dt| jjƒdd„ |jD ƒd� d S )NZcollectionfinishc                 S  s   g | ]
}|j ‘qS r   )r‡   rz   r   r   r   Ú
<listcomp>  r~   z=WorkerInteractor.pytest_collection_finish.<locals>.<listcomp>)ZtopdirZids)r^   r   rO   Zrootpathrw   r�   r   r   r   Úpytest_collection_finish   s
    
ýz)WorkerInteractor.pytest_collection_finishztuple[str, int | None, str])r‡   Úlocationr   c                 C  s   | j d||d� d S )NZlogstart©r‡   r™   ro   ©r   r‡   r™   r   r   r   Úpytest_runtest_logstart  s    z(WorkerInteractor.pytest_runtest_logstartc                 C  s   | j d||d� d S )NZ	logfinishrš   ro   r›   r   r   r   Úpytest_runtest_logfinish  s    z)WorkerInteractor.pytest_runtest_logfinishzpytest.TestReport)Úreportr   c                 C  s^   | j jj| j |d�}| j|d< | j|d< | j|d< | jj| j j|jksLJ ‚| j	d|d� d S )N©rO   rž   r„   Z	worker_idZtestrun_uidZ
testreport©Údata)
rO   rŠ   Úpytest_report_to_serializabler„   rQ   rS   rf   rw   r‡   r^   ©r   rž   r¡   r   r   r   Úpytest_runtest_logreport  s    ÿ


z)WorkerInteractor.pytest_runtest_logreportzpytest.CollectReportc                 C  s,   |j s(| jjj| j|d�}| jd|d� d S )NrŸ   Zcollectreportr    )ZpassedrO   rŠ   r¢   r^   r£   r   r   r   Úpytest_collectreport#  s
    ÿz%WorkerInteractor.pytest_collectreportúwarnings.WarningMessageztuple[str, int, str] | None)Úwarning_messageÚwhenr‡   r™   r   c                 C  s   | j dt|ƒ|||d� d S )NZwarning_recorded)Zwarning_message_datar¨   r‡   r™   )r^   Úserialize_warning_message)r   r§   r¨   r‡   r™   r   r   r   Úpytest_warning_recorded,  s    ûz(WorkerInteractor.pytest_warning_recordedN)r   r,   r-   r   r^   ÚpytestZhookimplre   ri   rn   rp   ry   rt   r‚   r€   r–   r˜   rœ   r�   r¤   r¥   rª   r   r   r   r   rL   p   s8   
	

rL   r¦   zdict[str, Any])r§   r   c           
   	   C  s  t | jtƒrdt| jƒj}t| jƒj}t| jƒ}zt | jj	¡ W n tj
yX   d }Y qv0 | jj	}n| j}d }d }d }| jrŽ| jj}| jj}nd }d }||||||dœ}| jD ]P}|dv r¼q®t| |ƒ}	zt |	¡ W n  tj
yô   t|	ƒ||< Y q®0 |	||< q®|S )N)Úmessage_strÚmessage_moduleÚmessage_class_nameÚmessage_argsÚcategory_moduleÚcategory_class_name)ÚmessageÚcategory)r†   r²   ÚWarningr   r,   r   r   ÚexecnetÚdumpsr‘   Z	DumpErrorr³   Ú_WARNING_DETAILSÚgetattrÚrepr)
r§   r­   r®   r¬   r¯   r°   r±   ÚresultÚ	attr_nameÚattrr   r   r   r©   =  sF    



ú	


r©   c                   @  sN   e Zd ZU ded< ded< ded< ded< ded< ded< ded	< d
ed< dS )Ú
WorkerInfor   Úversionztuple[int, int, int, str, int]Úversion_infoÚsysplatformÚplatformÚ
executableÚcwdÚidzexecnet.XSpecÚspecN)r   r,   r-   Ú__annotations__r   r   r   r   r½   p  s   
r½   r   c                  C  s0   dd l } ttjttjƒtj |   ¡ tjt ¡ d�S )Nr   )r¾   r¿   rÀ   rÁ   rÂ   rÃ   )	rÁ   Údictr(   r¾   Útupler¿   rÂ   ÚosÚgetcwd)rÁ   r   r   r   rh   {  s    úrh   rM   z
str | None)rO   Úbasetempr   c                 C  sN   |   d¡dk| j_d| j_d| j_d| j_d| j_d | j_d | j_|| j_	d S )NÚdistrŒ   FÚno)
r�   rU   rŒ   Z
looponfailZusepdbrÌ   ZdistloadZnumprocessesZmaxprocessesrË   )rO   rË   r   r   r   Úsetup_configˆ  s    rÎ   Z__channelexec__rN   rP   Ú
PYTHONPATHÚ rS   ZPYTEST_XDIST_TESTRUNUIDrQ   ZPYTEST_XDIST_WORKERZworkercountZPYTEST_XDIST_WORKER_COUNTrË   Zmainargv)rO   );r.   Ú
__future__r   r5   Úcollections.abcr   r   r   rJ   ÚenumrÉ   r(   rˆ   Útypingr   r   r   r	   ÚwarningsZ_pytest.configr
   rµ   r«   r   ÚImportErrorr   r0   ÚEnumr1   r3   rL   r©   r½   rh   rÎ   r   rP   rÆ   ZreceiverT   r‘   Úoption_dictZchange_sys_pathrÊ   Z
importpathÚpathÚinsertÚpathsepÚenvironrA   r   rO   ÚbasenameÚ_parserÚprogrm   rc   rŠ   Zpytest_cmdline_mainr   r   r   r   Ú<module>   sf   & N3

ÿ


