o
    ¯bÅ
  ã                   @   s˜   d Z ddlZddlZddlZdd„ Zeejƒ ddlmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ G d	d
„ d
ƒZejfdd„ZedkrJeƒ  dS dS )zj
Implementation of C{AMP} worker commands, and main executable entry point for
the workers.

@since: 12.3
é    Nc                 C   s*   d| v r| d   tj¡tjdd…< dS dS )z¡
    Override C{sys.path} with what the parent passed in B{TRIAL_PYTHONPATH}.

    @see: twisted.trial._dist.disttrial.DistTrialRunner.launchWorkerProcesses
    ÚTRIAL_PYTHONPATHN)ÚsplitÚosÚpathsepÚsysÚpath)Úenviron© r	   úA/usr/lib/python3/dist-packages/twisted/trial/_dist/workertrial.pyÚ
_setupPath   s   ÿr   )ÚFileWrapper)ÚstartLoggingWithObserverÚtextFromEventDict)Ú_WORKER_AMP_STDINÚ_WORKER_AMP_STDOUT)ÚWorkerOptionsc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚWorkerLogObserverzF
    A log observer that forward its output to a C{AMP} protocol.
    c                 C   s
   || _ dS )zg
        @param protocol: a connected C{AMP} protocol instance.
        @type protocol: C{AMP}
        N)Úprotocol)Úselfr   r	   r	   r
   Ú__init__*   s   
zWorkerLogObserver.__init__c                 C   s6   ddl m} t|ƒ}|du rdS | jj|j|d� dS )z'
        Produce a log output.
        r   )ÚmanagercommandsN)Úout)Útwisted.trial._distr   r   r   Ú
callRemoteÚ	TestWrite)r   Ú	eventDictr   Útextr	   r	   r
   Úemit1   s
   zWorkerLogObserver.emitN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r	   r	   r	   r
   r   %   s    r   c           
   
   C   s  t ƒ }| ¡  ddlm} ||d ƒ}| tdƒ}| tdƒ}| t|ƒ¡ t|ƒ}t	|j
dƒ 	 z| d¡}W n tyQ } z|jd tjkrLW Y d	}~q.‚ d	}~ww |d
krWn| |¡ | ¡  tj ¡  tj ¡  q/|jr…t d	¡ |j ¡ }	|	jdd| ¡ jd� d	S d	S )z¬
    Main function to be run if __name__ == "__main__".

    @param _fdopen: If specified, the function to use in place of C{os.fdopen}.
    @type _fdopen: C{callable}
    r   )ÚWorkerProtocolzforce-gcÚrbÚwbFTé   Nó    )Úshow_missingÚsummaryÚcoverdir)r   ÚparseOptionsÚtwisted.trial._dist.workerr"   r   r   ÚmakeConnectionr   r   r   r   ÚreadÚOSErrorÚargsÚerrnoÚEINTRÚdataReceivedÚflushr   ÚstdoutÚstderrÚtracerÚsettraceÚresultsÚwrite_resultsr)   r   )
Ú_fdopenÚconfigr"   ÚworkerProtocolÚ
protocolInÚprotocolOutÚobserverÚrÚer8   r	   r	   r
   Úmain=   s@   

€ü


ò


ÿýrB   Ú__main__)r!   r0   r   r   r   r   Útwisted.internet.protocolr   Útwisted.python.logr   r   r   r   r   Útwisted.trial._dist.optionsr   r   ÚfdopenrB   r   r	   r	   r	   r
   Ú<module>   s   

-
ÿ