a
    ze                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
dd Zdd Zdd Zd	d
 Zdd ZdddZdd Zdd Zedkre  dS )a  
Command line tool to bisect failing CPython tests.

Find the test_os test method which alters the environment:

    ./python -m test.bisect_cmd --fail-env-changed test_os

Find a reference leak in "test_os", write the list of failing tests into the
"bisect" file:

    ./python -m test.bisect_cmd -o bisect -R 3:3 test_os

Load an existing list of tests from a file using -i option:

    ./python -m test --list-cases -m FileTests test_os > tests
    ./python -m test.bisect_cmd -i tests test_os
    Nc                 C   sL   t | d.}|D ]}t||d q|  W d    n1 s>0    Y  d S )Nw)file)openprintflush)filenametestsfpname r   %/usr/lib/python3.9/test/bisect_cmd.pywrite_tests   s    r   c                 C   s*   | sd S t dt|| f  t| | | S )NzWriting %s tests into %s)r   lenr   )r   r   r   r   r   write_output&   s
    
r   c                 C   s
   d | S )N )join)argsr   r   r   format_shell_args.   s    r   c                  C   s(   t jg} | t  | t  | S )N)sys
executableextend
subprocess_args_from_interpreter_flags"_optim_args_from_interpreter_flags)cmdr   r   r   
python_cmd2   s    r   c                 C   sl   t  }|g d || j tj|tjdd}|j}|r^t|}td||f  t	
| |j }|S )N)-mtestz--list-casesT)stdoutuniversal_newlinesz1Failed to list tests: %s failed with exit code %s)r   r   	test_argsr   runPIPE
returncoder   r   r   exitr   
splitlines)r   r   procexitcoder   r   r   r   
list_cases9   s     

r(   c              	   C   s   t  }zht|| t }|ddd|g || j tdt|  t	|}|j
W tj|rnt| S ntj|rt| 0 d S )Nr   r   z--matchfilez+ %s)tempfilemktempr   r   r   r    r   r   r   r!   r#   ospathexistsunlink)r   r   Z
huntrleakstmpr   r&   r   r   r   	run_testsJ   s     


 r0   c                  C   sf   t  } | jdddd | jdddd | jdd	td
dd | jddtddd |  \}}||_|S )Nz-iz--inputzUTest names produced by --list-tests written into a file. If not set, run --list-tests)helpz-oz--outputzResult of the bisectionz-nz--max-tests   z:Maximum number of tests to stop the bisection (default: 1))typedefaultr1   z-Nz
--max-iterd   z5Maximum number of bisection iterations (default: 100))argparseArgumentParseradd_argumentintparse_known_argsr    )parserr   r    r   r   r   
parse_argsZ   s     r<   c                  C   sR  t  } d| jv sd| jv r(td t  | jrht| j}dd |D }W d    qp1 s\0    Y  nt| }tdt|  tdt| j  td| j| j	f  t
| j|}t  t }d	}zt|| jkr|| j	krt|}t|d
 d	}t||}td|t|t|f  t  t| |}td|t|f  td| |rltd |}t
| j|}ntd t  |d	7 }qW n( ty   t  td t  Y n0 tdt|  |D ]}	td|	  qt  |rtd|  tt | }
t|| jkr6td|tj|
df  td	 ntd|tj|
df  d S )Nz-wz
--verbose2z;WARNING: -w/--verbose2 option should not be used to bisect!c                 S   s   g | ]}|  qS r   )strip).0liner   r   r   
<listcomp>v       zmain.<locals>.<listcomp>zStart bisection with %s testszTest arguments: %szxBisection will stop when getting %s or less tests (-n/--max-tests option), or after %s iterations (-N/--max-iter option)r2      z![+] Iteration %s: run %s tests/%szran %s tests/%sr$   z*Tests failed: continuing with this subtestz;Tests succeeded: skipping this subtest, trying a new subsetzBisection interrupted!zTests (%s):z* %szOutput written into %sz+Bisection completed in %s iterations and %s)Zsecondsz+Bisection failed after %s iterations and %s)r<   r    r   inputr   r(   r   r   Z	max_testsZmax_iterr   outputtime	monotonicmaxrandomsampler0   KeyboardInterruptmathceildatetimeZ	timedeltar   r$   )r   r	   r   rD   Z
start_timeZ	iterationZntestZsubtestsr'   r   Zdtr   r   r   mainn   sn    .


rN   __main__)N)__doc__r6   rM   Zos.pathr+   rK   rH   r   r   r)   rE   r   r   r   r   r(   r0   r<   rN   __name__r   r   r   r   <module>   s&   
D