o
    Óó·ip8  ã                   @   s   d Z ddlZddlmZmZmZmZmZ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 ddlmZmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z)m*Z*m+Z+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8m9Z9 edd�G dd„ de:ƒƒZ;G dd„ deƒZ<G dd„ deƒZ=e
d ƒZ>ee=gee> f Z?G d!d"„ d"e!ƒZ@G d#d$„ d$e!ƒZAeeƒG d%d&„ d&ƒƒZBeeƒG d'd(„ d(ƒƒZCG d)d*„ d*e:ƒZDG d+d,„ d,eƒZEdS )-z:
This module implements the worker classes.

@since: 12.3
é    N)ÚAnyÚ	AwaitableÚCallableÚDictÚListÚOptionalÚTextIOÚTypeVar)ÚTestCase)Úimplementer)Úfrozen)ÚProtocolÚ	TypedDict)ÚDeferredÚDeferredList)ÚProcessDone)ÚIAddressÚ
ITransport)ÚProcessProtocol)ÚLogger)ÚAMP)ÚFailure)ÚFilePath)ÚnamedObject)Ú_WORKER_AMP_STDINÚ_WORKER_AMP_STDOUTÚmanagercommandsÚworkercommands)ÚWorkerReporter)Ú
TestResult)Ú
TestLoaderÚ
TrialSuite)ÚTodoé   )Ú
StreamOpenÚStreamReceiverÚStreamWriteF)Zauto_excc                   @   ó   e Zd ZU dZeed< dS )ÚWorkerExceptionz‰
    An exception was reported by a test running in a worker process.

    @ivar message: An error message describing the exception.
    ÚmessageN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__© r0   r0   ú`C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/trial/_dist/worker.pyr(   +   s   
 r(   c                   @   r'   )Ú	RunResultzB
    Represent the result of a L{workercommands.Run} command.
    ÚsuccessN)r*   r+   r,   r-   Úboolr/   r0   r0   r0   r1   r2   6   s   
 r2   c                   @   s&   e Zd ZdZdededefdd„ZdS )ÚWorkerz)
    An object that can run actions.
    ÚcaseÚresultÚreturnc                 Ã   s   �dS )z"
        Run a test case.
        Nr0   )Úselfr6   r7   r0   r0   r1   ÚrunC   s    z
Worker.runN)r*   r+   r,   r-   r
   r   r2   r:   r0   r0   r0   r1   r5   >   s    r5   Ú_Tc                   @   sJ   e Zd ZdZeƒ Zddd„Zejj	de
defdd„ƒZejj	d	d
„ ƒZdS )ÚWorkerProtocolz5
    The worker-side trial distributed protocol.
    Fc                 C   s   t ƒ | _t| ƒ| _|| _d S ©N)r    Ú_loaderr   Ú_resultÚ_forceGarbageCollection)r9   ZforceGarbageCollectionr0   r0   r1   Ú__init__T   s   

zWorkerProtocol.__init__ÚtestCaser8   c              	   Ã   sÌ   �| j  ¡ �}| j |¡}t|g| jƒ}| | j ¡ W d  ƒ n1 s$w   Y  d}t|dd�I dH D ]-\}}|r;q4d}| jj	d||d� z| j  
||¡I dH  W q4 tya   | j 	d¡ Y q4w d|iS )	z*
        Run a test case by name.
        NT)ZconsumeErrorsFz Result reporting for {id} failed)ÚfailureÚidz5Additionally, reporting the reporting failure failed.r3   )r?   ZgatherReportingResultsr>   Z
loadByNamer!   r@   r:   r   ÚloggerrC   ZaddErrorFallibleÚBaseException)r9   rB   Úresultsr6   ZsuiteZallSucceededr3   r7   r0   r0   r1   r:   Y   s6   €ýüýÿüzWorkerProtocol.runc                 C   s   t  |¡ ddiS )zb
        Set up the worker, moving into given directory for tests to run in
        them.
        r3   TN)ÚosÚchdir)r9   Ú	directoryr0   r0   r1   Ústart†   s   
zWorkerProtocol.startN)F)r*   r+   r,   r-   r   rE   rA   r   ÚRunÚ	responderr.   r2   r:   ÚStartrK   r0   r0   r0   r1   r<   M   s    
,r<   c                       sD  e Zd ZdZd)‡ fdd„	Zejdd„ ƒZejdd„ ƒZ	e
jjd	d
„ ƒZdededee defdd„Ze
jjdededededeeef f
dd„ƒZe
jjdededededeeef f
dd„ƒZe
jjdd„ ƒZe
jjdededee deeef fdd„ƒZe
jjdd „ ƒZe
j jd!d"„ ƒZ!d#e"d$e#de$fd%d&„Z%d'd(„ Z&‡  Z'S )*ÚLocalWorkerAMPz7
    Local implementation of the manager commands.
    Nc                    s   t ƒ  ||¡ tƒ | _d S r=   )ÚsuperrA   r%   Ú_streams)r9   ZboxReceiverÚlocator©Ú	__class__r0   r1   rA   •   s   zLocalWorkerAMP.__init__c                 C   s   d| j  ¡ iS )NÚstreamId)rQ   Úopen©r9   r0   r0   r1   Ú
streamOpen™   s   zLocalWorkerAMP.streamOpenc                 C   s   | j  ||¡ i S r=   )rQ   Úwrite)r9   rU   Údatar0   r0   r1   ÚstreamWrite�   s   zLocalWorkerAMP.streamWritec                 C   s   | j  | j¡ ddiS )z0
        Add a success to the reporter.
        r3   TN)r?   Ú
addSuccessÚ	_testCase)r9   ÚtestNamer0   r0   r1   r\   ¢   s   zLocalWorkerAMP.addSuccessÚerrorÚ
errorClassÚframesr8   c              	   C   sZ   t |ƒ}t||ƒ}tdt|ƒdƒD ]}|j || ||d  t||d  ƒg g f¡ q|S )a   
        Helper to build a C{Failure} with some traceback.

        @param error: An C{Exception} instance.

        @param errorClass: The class name of the C{error} class.

        @param frames: A flat list of strings representing the information need
            to approximatively rebuild C{Failure} frames.

        @return: A L{Failure} instance with enough information about a test
           error.
        r   é   r#   é   N)r   r   ÚrangeÚlenra   ÚappendÚint)r9   r_   r`   ra   Z	errorTyperC   Úir0   r0   r1   Ú_buildFailureª   s   
$ÿzLocalWorkerAMP._buildFailurer^   ÚerrorStreamIdÚframesStreamIdc                 C   sX   d  | j |¡¡ d¡}dd„ | j |¡D ƒ}|  t|ƒ||¡}| j | j|¡ ddiS )a¯  
        Add an error to the reporter.

        @param errorStreamId: The identifier of a stream over which the text
            of this error was previously completely sent to the peer.

        @param framesStreamId: The identifier of a stream over which the lines
            of the traceback for this error were previously completely sent to
            the peer.

        @param error: A message describing the error.
        ó    úutf-8c                 S   ó   g | ]}|  d ¡‘qS ©rm   ©Údecode©Ú.0Úframer0   r0   r1   Ú
<listcomp>Ú   ó    
ÿz+LocalWorkerAMP.addError.<locals>.<listcomp>r3   TN)	ÚjoinrQ   Úfinishrq   ri   r(   r?   ÚaddErrorr]   )r9   r^   r`   rj   rk   r_   ra   rC   r0   r0   r1   ry   Å   s   
ÿzLocalWorkerAMP.addErrorÚfailStreamIdÚ	failClassc                 C   sX   d  | j |¡¡ d¡}dd„ | j |¡D ƒ}|  t|ƒ||¡}| j | j|¡ ddiS )az  
        Add a failure to the reporter.

        @param failStreamId: The identifier of a stream over which the text of
            this failure was previously completely sent to the peer.

        @param framesStreamId: The identifier of a stream over which the lines
            of the traceback for this error were previously completely sent to the
            peer.
        rl   rm   c                 S   rn   ro   rp   rr   r0   r0   r1   ru   ø   rv   z-LocalWorkerAMP.addFailure.<locals>.<listcomp>r3   TN)	rw   rQ   rx   rq   ri   r(   r?   Ú
addFailurer]   )r9   r^   rz   r{   rk   Zfailra   rC   r0   r0   r1   r|   å   s   
ÿzLocalWorkerAMP.addFailurec                 C   ó   | j  | j|¡ ddiS )z-
        Add a skip to the reporter.
        r3   TN)r?   ÚaddSkipr]   )r9   r^   Úreasonr0   r0   r1   r~      ó   zLocalWorkerAMP.addSkipÚtodoc                 C   sF   d  | j |¡¡ d¡}t|du rdn|ƒ}| j | j||¡ ddiS )zÎ
        Add an expected failure to the reporter.

        @param errorStreamId: The identifier of a stream over which the text
            of this error was previously completely sent to the peer.
        rl   rm   Nz	<unknown>r3   T)rw   rQ   rx   rq   r"   r?   ÚaddExpectedFailurer]   )r9   r^   rj   r�   r_   Z_todor0   r0   r1   r‚     s   
z!LocalWorkerAMP.addExpectedFailurec                 C   r}   )z<
        Add an unexpected success to the reporter.
        r3   TN)r?   ÚaddUnexpectedSuccessr]   )r9   r^   r�   r0   r0   r1   rƒ     r€   z#LocalWorkerAMP.addUnexpectedSuccessc                 C   s"   | j  |d ¡ | j  ¡  ddiS )z4
        Print test output from the worker.
        Ú
r3   TN)Ú_testStreamrY   Úflush)r9   Úoutr0   r0   r1   Ú	testWrite  s   
zLocalWorkerAMP.testWriterB   r7   c              	   Ã   sV   �|| _ || _| j |¡ | ¡ }z| jtj|d�I dH W | j |¡ S | j |¡ w )z
        Run a test.
        )rB   N)r]   r?   Z	startTestrD   Ú
callRemoter   rL   ZstopTest)r9   rB   r7   Z
testCaseIdr0   r0   r1   r:   (  s   €zLocalWorkerAMP.runc                 C   s
   || _ dS )z?
        Set the stream used to log output from tests.
        N)r…   )r9   Ústreamr0   r0   r1   ÚsetTestStream5  s   
zLocalWorkerAMP.setTestStream)NN)(r*   r+   r,   r-   rA   r$   rM   rX   r&   r[   r   Z
AddSuccessr\   r(   r.   r   r   ri   ZAddErrorrg   r   r4   ry   Z
AddFailurer|   ZAddSkipr~   ZAddExpectedFailurer   r‚   ZAddUnexpectedSuccessrƒ   Z	TestWriterˆ   r
   r   r2   r:   r‹   Ú__classcell__r0   r0   rS   r1   rO   �   sx    


þýü
ûþýüû
úþýüû
ú
ÿÿÿ
þ

rO   c                   @   ó   e Zd ZdZdS )ÚLocalWorkerAddressz
    A L{IAddress} implementation meant to provide stub addresses for
    L{ITransport.getPeer} and L{ITransport.getHost}.
    N©r*   r+   r,   r-   r0   r0   r0   r1   rŽ   <  s    rŽ   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚLocalWorkerTransportzi
    A stub transport implementation used to support L{AMP} over a
    L{ProcessProtocol} transport.
    c                 C   s
   || _ d S r=   )Ú
_transport)r9   Ú	transportr0   r0   r1   rA   K  s   
zLocalWorkerTransport.__init__c                 C   s   | j  t|¡ dS )z,
        Forward data to transport.
        N©r‘   ZwriteToChildr   ©r9   rZ   r0   r0   r1   rY   N  s   zLocalWorkerTransport.writec                 C   s   |D ]	}| j  t|¡ qdS )zP
        Emulate C{writeSequence} by iterating data in the C{sequence}.
        Nr“   )r9   ÚsequencerZ   r0   r0   r1   ÚwriteSequenceT  s   ÿz"LocalWorkerTransport.writeSequencec                 C   s   | j  ¡  dS )z'
        Closes the transport.
        N)r‘   ÚloseConnectionrW   r0   r0   r1   r—   [  s   z#LocalWorkerTransport.loseConnectionc                 C   ó   t ƒ S ©z:
        Return a L{LocalWorkerAddress} instance.
        N©rŽ   rW   r0   r0   r1   ÚgetHosta  ó   zLocalWorkerTransport.getHostc                 C   r˜   r™   rš   rW   r0   r0   r1   ÚgetPeerg  rœ   zLocalWorkerTransport.getPeerN)
r*   r+   r,   r-   rA   rY   r–   r—   r›   r�   r0   r0   r0   r1   r�   D  s    r�   c                   @   r�   )Ú
NotRunningzN
    An operation was attempted on a worker process which is not running.
    Nr�   r0   r0   r0   r1   rž   n  s    rž   c                   @   sn   e Zd ZdZdedee defdd„Zdd	d
„Z	dd„ Z
dd„ Zdeddfdd„Zdd„ Zdd„ Zdd„ ZdS )ÚLocalWorkeraL  
    Local process worker protocol. This worker runs as a local process and
    communicates via stdin/out.

    @ivar _ampProtocol: The L{AMP} protocol instance used to communicate with
        the worker.

    @ivar _logDirectory: The directory where logs will reside.

    @ivar _logFile: The main log file for tests output.
    ÚampProtocolÚlogDirectoryÚlogFilec                 C   s   || _ || _|| _tƒ | _d S r=   )Ú_ampProtocolÚ_logDirectoryÚ_logFiler   ÚendDeferred)r9   r    r¡   r¢   r0   r0   r1   rA   �  s   zLocalWorker.__init__r8   Nc                 Ã   sJ   �| j du r	tƒ ‚| j}| j  t¡ z|I dH  W dS  ty$   Y dS w )z3
        Cause the worker process to exit.
        N)r’   rž   r¦   ZcloseChildFDr   r   )r9   r¦   r0   r0   r1   ÚexitŒ  s   €
ÿzLocalWorker.exitc                 C   s~   | j  t| jƒ¡ | jjdd� | j d¡ d¡| _| j d¡ d¡| _	| j  
| j¡ | j jtj| jjd�}| dd„ ¡ d	S )
zL
        When connection is made, create the AMP protocol instance.
        T)ZignoreExistingDirectoryzout.logÚwzerr.log)rJ   c                 S   s   d S r=   r0   )Úxr0   r0   r1   Ú<lambda>©  s    z,LocalWorker.connectionMade.<locals>.<lambda>N)r£   ZmakeConnectionr�   r’   r¤   ÚmakedirsÚchildrV   Ú_outLogÚ_errLogr‹   r¥   r‰   r   rN   ÚpathZ
addErrback)r9   Údr0   r0   r1   ÚconnectionMadeš  s   þzLocalWorker.connectionMadec                 C   s   | j  ¡  | j ¡  d| _dS )zk
        On connection lost, close the log files that we're managing for stdin
        and stdout.
        N)r­   Úcloser®   r’   ©r9   r   r0   r0   r1   ÚconnectionLost«  s   


zLocalWorker.connectionLostr   c                 C   s&   |   |¡ | j  |¡ | j |¡ dS )z’
        When the process closes, call C{connectionLost} for cleanup purposes
        and forward the information to the C{_ampProtocol}.
        N)r´   r£   r¦   Úcallbackr³   r0   r0   r1   ÚprocessEnded´  s   
zLocalWorker.processEndedc                 C   ó   | j  |¡ dS )z8
        Send data received from stdout to log.
        N)r­   rY   r”   r0   r0   r1   ÚoutReceived½  s   zLocalWorker.outReceivedc                 C   r·   )z*
        Write error data to log.
        N)r®   rY   r”   r0   r0   r1   ÚerrReceivedÄ  s   zLocalWorker.errReceivedc                 C   s*   |t kr| j |¡ dS t | ||¡ dS )zT
        Handle data received on the specific pipe for the C{_ampProtocol}.
        N)r   r£   ZdataReceivedr   ÚchildDataReceived)r9   ZchildFDrZ   r0   r0   r1   rº   Ê  s   zLocalWorker.childDataReceived)r8   N)r*   r+   r,   r-   rO   r   r   r   rA   r§   r±   r´   r   r¶   r¸   r¹   rº   r0   r0   r0   r1   rŸ   t  s     þý
ü
		rŸ   )Fr-   rH   Útypingr   r   r   r   r   r   r   r	   Zunittestr
   Zzope.interfacer   Úattrsr   Ztyping_extensionsr   r   Ztwisted.internet.deferr   r   Ztwisted.internet.errorr   Ztwisted.internet.interfacesr   r   Ztwisted.internet.protocolr   Ztwisted.loggerr   Ztwisted.protocols.ampr   Ztwisted.python.failurer   Ztwisted.python.filepathr   Ztwisted.python.reflectr   Ztwisted.trial._distr   r   r   r   Z"twisted.trial._dist.workerreporterr   Ztwisted.trial.reporterr   Ztwisted.trial.runnerr    r!   Ztwisted.trial.unittestr"   rŠ   r$   r%   r&   Ú	Exceptionr(   r2   r5   r;   ZWorkerActionr<   rO   rŽ   r�   rž   rŸ   r0   r0   r0   r1   Ú<module>   sJ   (
C -)