B
    u9ag                 @   s   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
dZdd Zdd Zedkre  dS )    Nc          	   C   s<   t | d(}x|D ]}t||d qW |  W d Q R X d S )Nw)file)openprintflush)filenametestsfpname r    /usr/lib/python3.7/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   sf   t jdddg}|| j tj|tjdd}|j}|rXt|}t	d||f  t 
| |j }|S )Nz-mtestz--list-casesT)stdoutZuniversal_newlinesz1Failed to list tests: %s failed with exit code %s)sys
executableextend	test_args
subprocessrunPIPE
returncoder   r   exitr   
splitlines)r   cmdprocexitcoder   r   r   r   
list_cases2   s    

r#   c          	   C   sl   t  }zFt|| tjddd|g}|| j tdt|  t	
|}|jS tj|rft| X d S )Nz-mr   z--matchfilez+ %s)tempfileZmktempr   r   r   r   r   r   r   r   r   r   ospathexistsunlink)r   r   Z
huntrleaksZtmpr    r!   r   r   r   	run_testsB   s    

r)   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defaultr*   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_argsQ   s    r5   c           	   C   s&  t  } | jr2t| j}dd |D }W d Q R X nt| }tdt|  tdt| j  td| j| j	f  t
| j|}t  t }d}yxt|| jkrP|| j	krPt|}t|d d}t||}td|t|t|f  t  t| |}td	|t|f  td
| |r8td |}t
| j|}ntd t  |d7 }qW W n* tk
r~   t  td t  Y nX tdt|  x|D ]}	td|	  qW t  |rtd|  tt | }
t|| jkr
td|tj|
df  td ntd|tj|
df  d S )Nc             S   s   g | ]}|  qS r   )strip).0liner   r   r   
<listcomp>j   s    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)r+      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)r5   inputr   r#   r   r   r   r   Z	max_testsZmax_iterr   outputtimeZ	monotonicmaxrandomZsampler)   KeyboardInterruptmathZceildatetimeZ	timedeltar   r   )r   r	   r   r<   Z
start_timeZ	iterationZntestZsubtestsr"   r   Zdtr   r   r   maine   s`    


rC   __main__)N)r/   rB   Zos.pathr%   rA   r?   r   r   r$   r=   r   r   r   r#   r)   r5   rC   __name__r   r   r   r   <module>   s"   
A