B
    18\<&                 @   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	ej
dddej
dddej
dddej
dd	ej
dd
gZedZdd ZdFddZdd Zdd Zeddd ddd Zeddd ddGddZdd  Zed!edd"d# Zed$edd%d& Zed'Zed(edd)d* Zed+d,d-d.d/ Zed0d,d-d1d2 Zed3d,d-d4d5 Zed6d,ed7d8d9 Zed:d,ed7d;d< Z d=d> Z!d?d@ Z"e#dAkrddl$Z$e$j%e dBZ&e&j'dCdDdE e&( Z)e)j!re!e)j! ne"  dS )Hz)Check proposed changes for common issues.    NModulesZ_ctypesZ
libffi_osxZlibffi_msvcZ_decimalZlibmpdecZexpatzlibsrcdirc             C   s   d | | dkrdndS )z7Return 'N file(s)' with the proper plurality on 'file'.z	{} file{}   s )format)count r
   X/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Tools/scripts/patchcheck.pyn_files_str   s    r   Fc                s    fdd}|S )z*Decorator to output status info to stdout.c                s    fdd}|S )Nc                 s\   t jd  t j   | |}s6s6td n"rHt| nt|rRdnd |S )Nz ... doneyesZNO)sysstdoutwriteflushprint)argskwargsresult)fxninfomessagemodalr
   r   call_fxn    s    


z/status.<locals>.decorated_fxn.<locals>.call_fxnr
   )r   r   )r   r   r   )r   r   decorated_fxn   s    zstatus.<locals>.decorated_fxnr
   )r   r   r   r   r
   )r   r   r   r   status   s    r   c              C   s6   d  } ytj| tjtdS  tjk
r0   dS X dS )z0Get the symbolic name for the current git branchzgit rev-parse --abbrev-ref HEAD)stderrcwdN)split
subprocesscheck_outputDEVNULLSRCDIRCalledProcessError)cmdr
   r
   r   get_git_branch/   s    r'   c              C   s:   d  } ytj| tjtd W n tjk
r4   dS X dS )zkGet the remote name to use for upstream branches

    Uses "upstream" if it exists, "origin" otherwise
    zgit remote get-url upstream)r   r   originZupstream)r    r!   r"   r#   r$   r%   )r&   r
   r
   r   get_git_upstream_remote:   s    r)   zGetting base branch for PRc             C   s   | d k	r| S dS )Nznot a PR branchr
   )xr
   r
   r   <lambda>J       r+   )r   c              C   sf   t jt jtdsd S tj} | jdkr0d}n
d| }t	 }|d ksP||krTd S t
 }|d | S )Nz.gitalphamasterz{0.major}.{0.minor}/)ospathexistsjoinr$   r   version_inforeleaselevelr   r'   r)   )versionbase_branchZthis_branchZupstream_remoter
   r
   r   get_base_branchI   s    

r8   z6Getting the list of files that have been added/changedc             C   s   t t| S )N)r   len)r*   r
   r
   r   r+   ]   r,   c          	      s  t jt jtdr| r$d|  }nd}g }tj| tjtdl}xd|j	D ]Z}|
  }|jdd\} t|}|dsqLd kr dd	d   |  qLW W d
Q R X n
td g }x:|D ]2 t j  t fddtD rq|  qW |S )z0Get the list of changed or added files from git.z.gitzgit diff --name-status zgit status --porcelain)r   r   r   )maxsplitZMAUz ->    Nz)need a git checkout to get modified filesc             3   s   | ]}  |V  qd S )N)
startswith).0r1   )filenamer
   r   	<genexpr>~   s    z changed_files.<locals>.<genexpr>)r0   r1   r2   r3   r$   r!   Popenr    PIPEr   decoderstripsetintersectionstripappendr   exitnormpathanyEXCLUDE_DIRS)r7   r&   	filenamesstlineZstatus_textr   Z
filenames2r
   )r>   r   changed_files\   s2    





rO   c             C   sT   t | }|dkrt|S dt|g}x| D ]}|d| q.W d|S d S )Nr   z{}:z  {}
)r9   r   r   rG   r3   )
file_pathsr	   linesr1   r
   r
   r   report_modified_files   s    
rS   zFixing Python file whitespacec             C   s   dt _dd | D }|S )zAMake sure that the whitespace for .py files have been normalized.Fc             S   s.   g | ]&}| d rttjt|r|qS )z.py)endswithreindentcheckr0   r1   r3   r$   )r=   r1   r
   r
   r   
<listcomp>   s    z(normalize_whitespace.<locals>.<listcomp>)rU   Z
makebackup)rQ   fixedr
   r
   r   normalize_whitespace   s    rY   zFixing C file whitespacec          
   C   sb   g }xX| D ]P}t jt|}t|d}d| kr6w
W dQ R X tj|ddd || q
W |S )zReport if any C files r	N   F)verbose)	r0   r1   r3   r$   openreaduntabifyprocessrG   )rQ   rX   r1   abspathfr
   r
   r   normalize_c_whitespace   s    
rd   s   \s+(\r?\n)$zFixing docs whitespacec             C   s   g }x| D ]}t jt|}yrt|d}| }W d Q R X dd |D }||krt||d  t|d}|| W d Q R X |	| W q
 t
k
r } ztd||f  W d d }~X Y q
X q
W |S )Nrbc             S   s   g | ]}t d |qS )s   \1)ws_resub)r=   rN   r
   r
   r   rW      s    z-normalize_docs_whitespace.<locals>.<listcomp>z.bakwbzCannot fix %s: %s)r0   r1   r3   r$   r^   	readlinesshutilcopyfile
writelinesrG   	Exceptionr   )rQ   rX   r1   rb   rc   rR   Z	new_lineserrr
   r
   r   normalize_docs_whitespace   s    
&ro   zDocs modifiedT)r   c             C   s   t | S )z9Report if any file in the Doc directory has been changed.)bool)rQ   r
   r
   r   docs_modified   s    rq   zMisc/ACKS updatedc             C   s   t jdd| kS )z$Check if Misc/ACKS has been changed.MiscZACKS)r0   r1   r3   )rQ   r
   r
   r   credit_given   s    rs   z Misc/NEWS.d updated with `blurb`c             C   s   t dd | D S )z&Check if Misc/NEWS.d has been changed.c             s   s$   | ]}| tjd ddV  qdS )rr   zNEWS.dnextN)r<   r0   r1   r3   )r=   pr
   r
   r   r?      s   z reported_news.<locals>.<genexpr>)rJ   )rQ   r
   r
   r   reported_news   s    rv   zconfigure regenerated)r   r   c             C   s    d| krd| krdS dS dS dS )z(Check if configure has been regenerated.zconfigure.acZ	configurer   noz
not neededNr
   )rQ   r
   r
   r   regenerated_configure   s    rx   zpyconfig.h.in regeneratedc             C   s    d| krd| krdS dS dS dS )z,Check if pyconfig.h.in has been regenerated.zconfigure.aczpyconfig.h.inr   rw   z
not neededNr
   )rQ   r
   r
   r   regenerated_pyconfig_h_in   s    ry   c             C   s   | dkrt d d S t }t|}dd |D }dd |D }dd |D }g }|t| |t| |t| |st d n&t dt| d	 t d
 t	d d S )NfalsezNot a pull request; skippingc             S   s   g | ]}| d r|qS )z.py)rT   )r=   fnr
   r
   r   rW      s    ztravis.<locals>.<listcomp>c             S   s   g | ]}| d r|qS ))z.cz.h)rT   )r=   r{   r
   r
   r   rW      s    c             S   s$   g | ]}| d r|dr|qS )Doc)z.rstz.inc)r<   rT   )r=   r{   r
   r
   r   rW      s    zNo whitespace issues foundzPlease fix the z file(s) with whitespace issuesz9(on UNIX you can run `make patchcheck` to make the fixes)r   )
r   r8   rO   extendrY   rd   ro   r9   r   rH   )Zpull_requestr7   rQ   python_filesc_files	doc_filesrX   r
   r
   r   travis   s"    
r   c              C   s   t  } t| }dd |D }dd |D }dd |D }dd |D }t| t| t| t| t| t| t| t	| |s|r|rdnd}t
  t
d	|  d S )
Nc             S   s   g | ]}| d r|qS )z.py)rT   )r=   r{   r
   r
   r   rW      s    zmain.<locals>.<listcomp>c             S   s   g | ]}| d r|qS ))z.cz.h)rT   )r=   r{   r
   r
   r   rW      s    c             S   s$   g | ]}| d r|dr|qS )r|   )z.rstz.inc)r<   rT   )r=   r{   r
   r
   r   rW      s    c             S   s   h | ]}| d r|qS )rr   )r<   )r=   ru   r
   r
   r   	<setcomp>   s    zmain.<locals>.<setcomp>z and check for refleaks??zDid you run the test suite)r8   rO   rY   rd   ro   rq   rs   rv   rx   ry   r   )r7   rQ   r~   r   r   Z
misc_filesendr
   r
   r   main   s$    r   __main__)descriptionz--traviszPerform pass/fail checks)help)FN)N)*__doc__rer   rj   os.pathr0   r!   	sysconfigrU   r`   r1   r3   rK   get_config_varr$   r   r   r'   r)   r8   rO   rS   rY   rd   compilerf   ro   rq   rs   rv   strrx   ry   r   r   __name__argparseArgumentParserparseradd_argument
parse_argsr   r
   r
   r
   r   <module>   sX   


(	
 
