B
    |?XcU(                 @   s   d dl Z d dlZd dlZd dlZd dlZeedo:ejdkZeej	Z
ejdd Zdd Zejdd	 Zejd
d Zejdddd Zejdddd ZedddZedddZeZdd Zejdddd Zejdddd ZdS )     Nversion_tuple)   r   c                s*  t jd}|r8yt|S  tk
r6   td Y nX ydd l}W n tk
rX   Y n*X | j	j
dk}|j|dpx| }|r|S yddl m   fdd}W nZ tk
r   t jd	d
krdS yddl m} W n  tk
r   ddlm} Y nX Y nX y
| }W n tk
r   dS X |r&|S dS )NZPYTEST_XDIST_AUTO_NUM_WORKERSzHPYTEST_XDIST_AUTO_NUM_WORKERS is not a number: {env_var!r}. Ignoring it.r   logical)r   )sched_getaffinityc                  s   t  dS )Nr   )len )r   r   +lib/python3.7/site-packages/xdist/plugin.py	cpu_count%   s    z0pytest_xdist_auto_num_workers.<locals>.cpu_countZTRAVIStrue   )r	      )osenvirongetint
ValueErrorwarningswarnpsutilImportErroroptionnumprocessesr	   r   ZmultiprocessingNotImplementedError)configZenv_varr   Zuse_logicalcountr	   nr   )r   r   pytest_xdist_auto_num_workers   s<    
r   c             C   s    | dkr| S | d k	rt | S d S )N)autor   )r   )sr   r   r   parse_numprocesses7   s    r   c          
   C   s"  |  dd}|jdddddtdd |jd	d
d
dtdd |jddd ddd |jdddddddddgdddd |jdddg ddd  |jd!d"d#d$d%d& |jd'dg d(d)d* |jd+dg d,d-d* |jd.dd/d0 | jd1d2trd3nd4d5 | jd6d7trd3nd4d5 | jd8trd3nd4d9d: d S );Nxdistz"distributed and subprocess testingz-nz--numprocessesr   storezShortcut for '--dist=load --tx=NUM*popen'. With 'auto', attempt to detect physical CPU count. With 'logical', detect logical CPU count. If physical CPU count cannot be found, falls back to logical count. This will be 0 when used with --pdb.)destmetavaractiontypehelpz--maxprocessesmaxprocesseszWlimit the maximum number of workers to process the tests when using --numprocesses=autoz--max-worker-restartZmaxworkerrestartzbmaximum number of workers that can be restarted when crashed (set to zero to disable this feature))r$   defaultr"   r&   z--distZdistmodeZeachloadZ	loadscopeZloadfileZ	loadgroupnodista  set mode for distributing tests to exec environments.

each: send each test to all available environments.

load: load balance by sending any pending test to any available environment.

loadscope: load balance by sending pending groups of tests in the same scope to any available environment.

loadfile: load balance by sending test grouped by file to any available environment.

loadgroup: like load, but sends tests marked with 'xdist_group' to the same worker.

(default) no: run tests inprocess, don't distribute.)r#   r$   choicesr"   r(   r&   z--txtxappendZxspeczadd a test execution environment. some examples: --tx popen//python=python2.5 --tx socket=192.168.1.102:8888 --tx ssh=user@codespeak.net//chdir=testcache)r"   r$   r(   r#   r&   z-d
store_truedistloadFz/load-balance tests.  shortcut for '--dist=load')r$   r"   r(   r&   z
--rsyncdirZDIRz.add directory for rsyncing to remote tx nodes.)r$   r(   r#   r&   z--rsyncignoreZGLOBz<add expression for ignores when rsyncing to remote tx nodes.z--testrunuidzprovide an identifier shared amongst all workers as the value of the 'testrun_uid' fixture,

,if not provided, 'testrun_uid' is filled with a new unique string on every test run.)r$   r&   	rsyncdirszFlist of (relative) paths to be rsynced for remote distributed testing.pathsZpathlist)r%   Zrsyncignorez?list of (relative) glob-style paths to be ignored for rsyncing.looponfailrootsz=directories to check for changes. Default: current directory.)r%   r&   )ZgetgroupZ
_addoptionr   Z	addoptionr   ZaddiniPYTEST_GTE_7)parsergroupr   r   r   pytest_addoption>   s    r7   c             C   s   ddl m} | | d S )Nr   )newhooks)r    r8   Zadd_hookspecs)pluginmanagerr8   r   r   r   pytest_addhooks   s    r:   T)Ztrylastc             C   s   |  ddkrP| dsPddlm} || }| j|d | jd}|rPd|_|  d	d sf| d
rzt	d}| 
|d |  dd s| drt	d}| 
|d d}| d| d S )Nr+   r*   collectonlyr   )DSessionZdsessionZterminalreporterFZ
looponfailr3   zThe --looponfail command line argument and looponfailroots config variable are deprecated.
The loop-on-fail feature will be removed in pytest-xdist 4.0.r   Zrsyncdirr1   zThe --rsyncdir command line argument and rsyncdirs config variable are deprecated.
The rsync feature will be removed in pytest-xdist 4.0.zuxdist_group: specify group for tests should run in same session.in relation to one another. Provided by pytest-xdist.Zmarkers)	getoptiongetvalueZxdist.dsessionr<   r9   registerZ	getpluginZ
showfspathZgetiniDeprecationWarningZissue_config_time_warningZaddinivalue_line)r   r<   sessiontrZwarningZconfig_liner   r   r   pytest_configure   s"    rC   )Ztryfirstc             C   s   |  dd}| jjdkrD|r.d| j_d| j_n| jj| d}|| j_| jjr| jjdkr`d| j_| jj}| jjr~t|| jj}dg| | j_| jj	rd| j_| j
}|d	s|d
dkr|rtdd S )NusepdbF)r   r   r   r*   )r   r)   popenr;   r+   zG--pdb is incompatible with distributing tests; try using -n0 or -nauto.)r=   r   r   r+   hookr   r'   minr-   r0   r>   pytestZ
UsageError)r   rD   Zauto_num_cpusr   valr   r   r   pytest_cmdline_main   s(    
rJ   )returnc             C   s   t | jdS )zReturn `True` if this is an xdist worker, `False` otherwise

    :param request_or_session: the `pytest` `request` or `session` object
    workerinput)hasattrr   )request_or_sessionr   r   r   is_xdist_worker   s    rO   c             C   s   t |  o| jjjdkS )zReturn `True` if this is the xdist controller, `False` otherwise

    Note: this method also returns `False` when distribution has not been
    activated at all.

    :param request_or_session: the `pytest` `request` or `session` object
    r*   )rO   r   r   r+   )rN   r   r   r   is_xdist_controller  s    	
rP   c             C   s    t | jdr| jjd S dS dS )a*  Return the id of the current worker ('gw0', 'gw1', etc) or 'master'
    if running on the controller node.

    If not distributing tests (for example passing `-n0` or not passing `-n` at all)
    also return 'master'.

    :param request_or_session: the `pytest` `request` or `session` object
    rL   ZworkeridZmasterN)rM   r   rL   )rN   r   r   r   get_xdist_worker_id  s    	rQ   rA   )Zscopec             C   s   t | S )zkReturn the id of the current worker ('gw0', 'gw1', etc) or 'master'
    if running on the master node.
    )rQ   )requestr   r   r   	worker_id)  s    rS   c             C   s&   t | jdr| jjd S t jS dS )z)Return the unique id of the current test.rL   Z
testrunuidN)rM   r   rL   uuidZuuid4hex)rR   r   r   r   testrun_uid2  s    rV   )r   rT   sysr   rH   rM   r   r4   listpathZ	_sys_pathZhookimplr   r   r7   r:   rC   rJ   boolrO   rP   Zis_xdist_masterrQ   ZfixturerS   rV   r   r   r   r   <module>   s$   
)u  	