o
    >hp8                     @   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)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__ r1   r1   z/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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.   boolr0   r1   r1   r1   r2   r3   6   s   
 r3   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.
        Nr1   )selfr7   r8   r1   r1   r2   runC   s    z
Worker.runN)r+   r,   r-   r.   r
   r   r3   r;   r1   r1   r1   r2   r6   >   s    r6   _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)r:   forceGarbageCollectionr1   r1   r2   __init__T   s   

zWorkerProtocol.__init__testCaser9   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)consumeErrorsFz Result reporting for {id} failed)failureidz5Additionally, reporting the reporting failure failed.r4   )r@   gatherReportingResultsr?   
loadByNamer!   rA   r;   r   loggerrF   addErrorFallibleBaseException)r:   rD   resultsr7   suiteallSucceededr4   r8   r1   r1   r2   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.
        r4   T)oschdir)r:   	directoryr1   r1   r2   start   s   
zWorkerProtocol.startN)F)r+   r,   r-   r.   r   rJ   rC   r   Run	responderr/   r3   r;   StartrS   r1   r1   r1   r2   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>   )superrC   r%   _streams)r:   boxReceiverlocator	__class__r1   r2   rC      s   zLocalWorkerAMP.__init__c                 C   s   d| j  iS )NstreamId)rY   openr:   r1   r1   r2   
streamOpen   s   zLocalWorkerAMP.streamOpenc                 C   s   | j || i S r>   )rY   write)r:   r^   datar1   r1   r2   streamWrite   s   zLocalWorkerAMP.streamWritec                 C   s   | j | j ddiS )z0
        Add a success to the reporter.
        r4   T)r@   
addSuccess	_testCase)r:   testNamer1   r1   r2   re      s   zLocalWorkerAMP.addSuccesserror
errorClassframesr9   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#      )r   r   rangelenrj   appendint)r:   rh   ri   rj   	errorTyperF   ir1   r1   r2   _buildFailure   s   
$zLocalWorkerAMP._buildFailurerg   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 rw   decode.0framer1   r1   r2   
<listcomp>       
z+LocalWorkerAMP.addError.<locals>.<listcomp>r4   T)	joinrY   finishr{   rs   r)   r@   addErrorrf   )r:   rg   ri   rt   ru   rh   rj   rF   r1   r1   r2   r      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.
        rv   rw   c                 S   rx   ry   rz   r|   r1   r1   r2   r      r   z-LocalWorkerAMP.addFailure.<locals>.<listcomp>r4   T)	r   rY   r   r{   rs   r)   r@   
addFailurerf   )r:   rg   r   r   ru   failrj   rF   r1   r1   r2   r      s   
zLocalWorkerAMP.addFailurec                 C      | j | j| ddiS )z-
        Add a skip to the reporter.
        r4   T)r@   addSkiprf   )r:   rg   reasonr1   r1   r2   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.
        rv   rw   Nz	<unknown>r4   T)r   rY   r   r{   r"   r@   addExpectedFailurerf   )r:   rg   rt   r   rh   _todor1   r1   r2   r     s   
z!LocalWorkerAMP.addExpectedFailurec                 C   r   )z<
        Add an unexpected success to the reporter.
        r4   T)r@   addUnexpectedSuccessrf   )r:   rg   r   r1   r1   r2   r     r   z#LocalWorkerAMP.addUnexpectedSuccessc                 C   s"   | j |d  | j   ddiS )z4
        Print test output from the worker.
        
r4   T)_testStreamrb   flush)r:   outr1   r1   r2   	testWrite  s   
zLocalWorkerAMP.testWriterD   r8   c              	      sV   || _ || _| j| | }z| jtj|dI dH W | j| S | j| w )z
        Run a test.
        )rD   N)rf   r@   	startTestrG   
callRemoter   rT   stopTest)r:   rD   r8   
testCaseIdr1   r1   r2   r;   (  s   zLocalWorkerAMP.runc                 C   s
   || _ dS )z?
        Set the stream used to log output from tests.
        N)r   )r:   streamr1   r1   r2   setTestStream5  s   
zLocalWorkerAMP.setTestStream)NN)(r+   r,   r-   r.   rC   r$   rU   ra   r&   rd   r   
AddSuccessre   r)   r/   r   r   rs   AddErrorrp   r   r5   r   
AddFailurer   AddSkipr   AddExpectedFailurer   r   AddUnexpectedSuccessr   	TestWriter   r
   r   r3   r;   r   __classcell__r1   r1   r\   r2   rW      sx    









rW   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.   r1   r1   r1   r2   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)r:   	transportr1   r1   r2   rC   K  s   
zLocalWorkerTransport.__init__c                 C   s   | j t| dS )z,
        Forward data to transport.
        Nr   writeToChildr   r:   rc   r1   r1   r2   rb   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   )r:   sequencerc   r1   r1   r2   writeSequenceT  s   z"LocalWorkerTransport.writeSequencec                 C   s   | j   dS )z'
        Closes the transport.
        N)r   loseConnectionr`   r1   r1   r2   r   [  s   z#LocalWorkerTransport.loseConnectionc                 C      t  S z:
        Return a L{LocalWorkerAddress} instance.
        r   r`   r1   r1   r2   getHosta     zLocalWorkerTransport.getHostc                 C   r   r   r   r`   r1   r1   r2   getPeerg  r   zLocalWorkerTransport.getPeerN)
r+   r,   r-   r.   rC   rb   r   r   r   r   r1   r1   r1   r2   r   D  s    r   c                   @   r   )
NotRunningzN
    An operation was attempted on a worker process which is not running.
    Nr   r1   r1   r1   r2   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)r:   r   r   r   r1   r1   r2   rC     s   zLocalWorker.__init__r9   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   closeChildFDr   r   )r:   r   r1   r1   r2   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)ignoreExistingDirectoryzout.logwzerr.log)rR   c                 S   s   d S r>   r1   )xr1   r1   r2   <lambda>  s    z,LocalWorker.connectionMade.<locals>.<lambda>N)r   makeConnectionr   r   r   makedirschildr_   _outLog_errLogr   r   r   r   rV   path
addErrback)r:   dr1   r1   r2   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   r:   r   r1   r1   r2   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   r1   r1   r2   processEnded  s   
zLocalWorker.processEndedc                 C      | j | dS )z8
        Send data received from stdout to log.
        N)r   rb   r   r1   r1   r2   outReceived  s   zLocalWorker.outReceivedc                 C   r   )z*
        Write error data to log.
        N)r   rb   r   r1   r1   r2   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   dataReceivedr   childDataReceived)r:   childFDrc   r1   r1   r2   r     s   zLocalWorker.childDataReceived)r9   N)r+   r,   r-   r.   rW   r   r   r   rC   r   r   r   r   r   r   r   r   r1   r1   r1   r2   r   t  s     

		r   )Fr.   rP   typingr   r   r   r   r   r   r   r	   unittestr
   zope.interfacer   attrsr   typing_extensionsr   r   twisted.internet.deferr   r   twisted.internet.errorr   twisted.internet.interfacesr   r   twisted.internet.protocolr   twisted.loggerr   twisted.protocols.ampr   twisted.python.failurer   twisted.python.filepathr   twisted.python.reflectr   twisted.trial._distr   r   r   r   "twisted.trial._dist.workerreporterr   twisted.trial.reporterr   twisted.trial.runnerr    r!   twisted.trial.unittestr"   r   r$   r%   r&   	Exceptionr)   r3   r6   r<   WorkerActionr=   rW   r   r   r   r   r1   r1   r1   r2   <module>   sJ   (
C -)