
    i'                    >   d Z ddlmZ ddlZddlmc mZ ddl	m
Z
 ddlZddlmZ ddlZddlZddlmZ ddlmZ ddlZddlZddlmZ ej        d%d            Zej        d&d            Zd'dZ G d d          Zd(dZd)d Z G d! d"          Z G d# d$          Z dS )*a&  
Implement -f aka looponfailing for pytest.

NOTE that we try to avoid loading and depending on application modules
within the controlling process (the one that starts repeatedly test
processes) otherwise changes to source code can crash
the controlling process which should best never happen.
    )annotationsN)SequencePath)Any)TerminalWriter)
visit_pathparserpytest.ParserreturnNonec                h    |                      dd          }|                    dddddd	           d S )
Nxdistz"distributed and subprocess testingz-fz--looponfail
store_true
looponfailFzdRun tests in subprocess: wait for files to be modified, then re-run failing test set until all pass.)actiondestdefaulthelp)getgroup
_addoption)r
   groups     W/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/xdist/looponfail.pypytest_addoptionr      sQ    OOG%IJJE	2          configpytest.Config
int | Nonec                    |                      d          r=|                      dd          }|rt          j        d          t          |            dS d S )Nr   usepdbFz(--pdb is incompatible with --looponfail.   )	getoptionpytest
UsageErrorlooponfail_main)r   r    s     r   pytest_cmdline_mainr&   (   s`    %% !!(E22 	P#$NOOOq4r   c                   t          |           }|                     d          }|st          j                    g}d |D             }t	          |          }	 	 |                                 |j        s|j        r$t          |j        |           |	                    d           P# t          $ r t                       Y d S w xY w)Nlooponfailrootsc                ,    g | ]}t          |          S  r   ).0roots     r   
<listcomp>z#looponfail_main.<locals>.<listcomp>8   s    444tT

444r      )failreportsrootdirsg       @)checkinterval)RemoteControlgetinir   cwdStatRecorder	loop_oncefailures
wasfailingrepr_pytest_looponfailinfowaitonchangeKeyboardInterruptprint)r   remotecontrolconfig_rootsr0   statrecorders        r   r%   r%   3   s    !&))M==!233L $

|44|444H))L	9##%%% ) m.F &)2X    %%C%888	9    s   AB( (CCc                  R    e Zd ZU ded<   ddZdd
ZddZddZddZddZ	ddZ
dS )r2   execnet.Gatewaygatewayr   r   r   r   c                "    || _         g | _        d S N)r   r7   )selfr   s     r   __init__zRemoteControl.__init__K   s    #%r   argsobjectc                    | j         j        j        r1d                    d |D                       }t	          d|           d S d S )N c              3  4   K   | ]}t          |          V  d S rD   )str)r+   xs     r   	<genexpr>z&RemoteControl.trace.<locals>.<genexpr>Q   s(      00a3q66000000r   zRemoteControl:)r   optiondebugjoinr<   )rE   rG   msgs      r   tracezRemoteControl.traceO   sS    ;# 	)((00400000C"C(((((	) 	)r   c                *    t          j        d          S )Nz!execmodel=main_thread_only//popen)execnetmakegatewayrE   s    r   initgatewayzRemoteControl.initgatewayT   s    "#FGGGr   c                   t          | d          rt          d| j        z            |                     d           |                                 | _        | j                            t          | j        j        t          | j        j
                            x| _        }|                                }t                      d
fd	}|                    |           d S )NrB   zalready have gateway %rzsetting up worker session)rG   option_dictsrL   r   r   c                n    j                             |            j                                          d S rD   )_filewriteflush)r[   outs    r   r^   z"RemoteControl.setup.<locals>.writee   s/    IOOAIOOr   )r[   rL   r   r   )hasattr
ValueErrorrB   rS   rX   remote_execinit_worker_sessionr   rG   varsrO   channelreceiver   setcallback)rE   rf   remote_outchannelr^   r`   s       @r   setupzRemoteControl.setupW   s    4## 	G6EFFF

.///''))!%!9!9!T[/00 ": "
 "
 	
w
 .5__->->	 	 	 	 	 	 	%%e,,,,,r   c                T   t          | d          rO| j                                        s4|                     d| j                   | j                                         | `t          | d          r8|                     d| j                   | j                                         | `d S d S )Nrf   closingrB   exiting)ra   rf   isclosedrS   closerB   exitrW   s    r   ensure_teardownzRemoteControl.ensure_teardownk   s    4## 	<((** %

9dl333""$$$4## 	JJy$,///L	 	r   !tuple[list[str], list[str], bool]c                   	 |                      d| j                   | j                            | j                   	 | j                                        |                                  S # | j        j        $ r1 t          j                    d         }|                      d|            w xY w# |                                  w xY w)Nsendingr.   ERROR)	rS   r7   rf   sendrg   rq   RemoteErrorsysexc_info)rE   es     r   
runsessionzRemoteControl.runsessionv   s    
	#JJy$-000Ldm,,,|++--   """" <+   LNN1%

7A&&&
   """"s   :B- A* *A B**B- -Cc                    |                                   | j        ot          | j                  | _        |                                 }|\  }}}|rd S g }|D ]}||vr|                    |           || _        d S rD   )rj   r7   lenr8   r{   append)rE   resultr7   reportscollection_faileduniq_failuresfailures          r   r6   zRemoteControl.loop_once   s    

->C,>,>""/5,', 	*DM# 2 2-//!((111)DMMMr   Nr   r   r   r   rG   rH   r   r   )r   rA   r   r   )r   rr   )__name__
__module____qualname____annotations__rF   rS   rX   rj   rq   r{   r6   r*   r   r   r2   r2   H   s         & & & &) ) ) )
H H H H- - - -(	 	 	 	# # # #* * * * * *r   r2   r/   Sequence[str]r0   Sequence[Path]c                    t                      }| r6|                    ddd           | D ]}|r|                    |d           |                    ddd           |D ]}|                    d| d           d S )N#LOOPONFAILINGT)bold)redzwaiting for changesz### Watching:   )r   sepline)r/   r0   trreportrootdirs        r   r9   r9      s     
		B *
sO$///! 	* 	*F *D)))FF3%DF111 9 9
,7,,488889 9r   rf   'execnet.Channel'rG   	list[str]rZ   dict[str, 'Any']c                L   dd l }dd l}| j                                        }|                    d          x|_        |_        |                     |           g }|j        D ]b}|r^|j        	                    |          s/|
                    d          s|j                            |          }|                    |           c||j        d d <   ddlm} |                    |t!          |                    }	||	_        ddlm}
  |
|	|                                            d S )Nr   wz.__path_hook__)Config)WorkerFailSession)osrx   rB   
newchannelmakefilestdoutstderrrv   pathisabsendswithabspathr~   r#   r   fromdictargslistrG   xdist.looponfailr   main)rf   rG   rZ   r   rx   
outchannelnewpathspr   r   r   s              r   rd   rd      sA   
 IIIJJJ++--J(11#666CJLLHX   	7==## 'AJJ7G,H,H 'GOOA&&OOACHQQQK   d4jj99FFK222222fg&&++-----r   c                      e Zd ZddZdd
Zej        dd            Zej        dd            Zej        dd            Z	ddZ
dS )r   r   r   rf   execnet.Channelr   r   c                    || _         || _        g | _        d| _        |j                            |            d|j        _        d|j        _        d S )NF)	r   rf   recorded_failuresr   pluginmanagerregisterrO   r   r    )rE   r   rf   s      r   rF   zWorkerFailSession.__init__   sQ    QS!&%%d+++#( $r   rG   rH   c                    | j         j        j        r7t          d                    t          t          |                               d S d S )NrJ   )r   rO   rP   r<   rQ   maprL   )rE   rG   s     r   DEBUGzWorkerFailSession.DEBUG   sD    ;# 	,#((3sD>>**+++++	, 	,r   sessionpytest.Sessionboolc                :   || _         | j        | _        | j         j        }	 |                    | j        pd           }n*# t
          j        $ r |                    d           }Y nw xY w|                    ||j        |           |	                    |           dS )N)r   r   items)r   T)
r   current_commandtrailsihookperform_collectr#   r$   pytest_collection_modifyitemsr   pytest_collection_finish)rE   r   hookr   s       r   pytest_collectionz#WorkerFailSession.pytest_collection   s    *|!	2++DK,?4@@EE  	2 	2 	2++D11EEE	2**GN% 	+ 	
 	
 	
 	%%g%666ts   > $A%$A%r   pytest.TestReportc                L    |j         r| j                            |           d S d S rD   )failedr   r~   rE   r   s     r   pytest_runtest_logreportz*WorkerFailSession.pytest_runtest_logreport   s3    = 	2"))&11111	2 	2r   pytest.CollectReportc                Z    |j         r#| j                            |           d| _        d S d S )NT)r   r   r~   r   r   s     r   pytest_collectreportz&WorkerFailSession.pytest_collectreport   s;    = 	*"))&111%)D"""	* 	*r   c                   |                      d           	 | j                                        }n# t          $ r Y d S w xY w|                      d|           || _        | j        j                            | j                   g g }}| j        D ]V}|	                    |j
                   |j        }t          t          |d|                    }|	                    |           W||| j        f}| j                            |           d S )Nz:WORKER: received configuration, waiting for command trailsreceived)r   	reprcrash)r   rf   rg   r;   r   r   r   r&   r   r~   nodeidlongreprrL   getattrr   rv   )rE   commandr   r/   replocr   s          r   r   zWorkerFailSession.main   s   

OPPP	l**,,GG  	 	 	FF	

:w'''&,,DK,@@@ ") 	$ 	$CMM#*%%%,Cgc;4455Cs####+t'=>&!!!!!s   1 
??N)r   r   rf   r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   r   )r   r   r   rF   r   r#   hookimplr   r   r   r   r*   r   r   r   r      s        % % % %, , , , _   _ _2 2 2 _2 _* * * _*
" " " " " "r   r   c                  :    e Zd ZddZdd	Zdd
ZdddZdddZdS )r5   rootdirlistr   r   r   c                J    || _         i | _        |                                  d S rD   )r   	statcachecheck)rE   r   s     r   rF   zStatRecorder.__init__   s"    &57

r   r   r   r   c                v    |                                 o%|j                            d           o
|j        dk    S )N..pyc)is_filename
startswithsuffixrE   r   s     r   filzStatRecorder.fil   s4    yy{{P16#4#4S#9#99Pah&>PPr   c                `    |j                             d           o|                                S )Nr   )r   r   existsr   s     r   reczStatRecorder.rec  s(    6$$S)))8ahhjj8r         ?r1   floatc                ^    	 |                                  }|rd S t          j        |           -rD   )r   timesleep)rE   r1   changeds      r   r:   zStatRecorder.waitonchange  s6    	&jjllG J}%%%		&r   Tremovepycfilesc                   d}i }| j         D ]}t          || j        | j                  D ]}| j                            |d           }	 |                                }|||<   |}|j        |j        k    s|j        |j        k    r\d}t          d|           |rH|j
        dk    r=|                    d          }|                                rt          j        |           d}# t          $ r |rd}Y w xY w| j        rd}|| _        |S )NF)filterrecurseTz
# MODIFIEDz.pyr   )r   r	   r   r   r   popstatst_mtimest_sizer<   r   with_suffixr   r   unlinkOSError)	rE   r   r   newstatr   r   oldstatcurstatpycfiles	            r   r   zStatRecorder.check  sP   .0' 	' 	'G"748TXNNN ' '.,,T488'"iikkG
 %,GDM*#,0@@@&'/AA&*G!,555- 7$+2F2F*.*:*:6*B*B#*??#4#4 !7$&Ig$6$6$6 #'%  ' ' ' '"&'	'. > 	G s   C##C43C4N)r   r   r   r   )r   r   r   r   )r   )r1   r   r   r   )T)r   r   r   r   )r   r   r   rF   r   r   r:   r   r*   r   r   r5   r5      s           
Q Q Q Q9 9 9 9& & & & &      r   r5   )r
   r   r   r   )r   r   r   r   r   )r/   r   r0   r   r   r   )rf   r   rG   r   rZ   r   r   r   )!__doc__
__future__r   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arcollections.abcr   r   pathlibr   rx   r   typingr   _pytest._ior   rU   r#   xdist._pathr	   r   r   r&   r%   r2   r9   rd   r   r5   r*   r   r   <module>r	     s    # " " " " "               $ $ $ $ $ $ 				       



        & & & & & &   " " " " " " 
 
 
 
       *G* G* G* G* G* G* G* G*T9 9 9 9. . . .>8" 8" 8" 8" 8" 8" 8" 8"v1 1 1 1 1 1 1 1 1 1r   