3
ebf&                 @   s   d Z ddlZddlZddlmZ dddddgZi Zd#ZddddddddgZdd Z	d$ddZ
dd Zdd ZG dd dZd%ddZee
fddZdd Zd d! Zed"kre  dS )&zUtilities for comparing files and directories.

Classes:
    dircmp

Functions:
    cmp(f1, f2, shallow=True) -> int
    cmpfiles(a, b, common) -> ([], [], [])
    clear_cache()

    N)filterfalseclear_cachecmpdircmpcmpfilesDEFAULT_IGNORES   i   ZRCSZCVSZtagsz.gitz.hgz.bzrZ_darcs__pycache__c               C   s   t j  dS )zClear the filecmp cache.N)_cacheclear r   r   /usr/lib/python3.6/filecmp.pyr      s    Tc             C   s   t tj| }t tj|}|d tjks8|d tjkr<dS |rL||krLdS |d |d kr`dS tj| |||f}|dkrt| |}ttdkrt  |t| |||f< |S )a  Compare two files.

    Arguments:

    f1 -- First file name

    f2 -- Second file name

    shallow -- Just check stat signature (do not read the files).
               defaults to True.

    Return value:

    True if the files are the same, False otherwise.

    This function uses a cache for past comparisons and the results,
    with cache entries invalidated if their stat information
    changes.  The cache may be cleared by calling clear_cache().

    r   FT   Nd   )	_sigosstatS_IFREGr
   get_do_cmplenr   )f1f2shallows1s2Zoutcomer   r   r   r      s    
c             C   s   t j| j| j| jfS )N)r   S_IFMTst_modest_sizest_mtime)str   r   r   r   D   s    
r   c             C   sb   t }t| dJ}t|d4}x,|j|}|j|}||kr>dS |sdS qW W d Q R X W d Q R X d S )NrbFT)BUFSIZEopenread)r   r   bufsizefp1fp2Zb1Zb2r   r   r   r   I   s    

r   c               @   s   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zee	eeeeeeeeeeedZdd ZdS )r   aM  A class that manages the comparison of 2 directories.

    dircmp(a, b, ignore=None, hide=None)
      A and B are directories.
      IGNORE is a list of names to ignore,
        defaults to DEFAULT_IGNORES.
      HIDE is a list of names to hide,
        defaults to [os.curdir, os.pardir].

    High level usage:
      x = dircmp(dir1, dir2)
      x.report() -> prints a report on the differences between dir1 and dir2
       or
      x.report_partial_closure() -> prints report on differences between dir1
            and dir2, and reports on common immediate subdirectories.
      x.report_full_closure() -> like report_partial_closure,
            but fully recursive.

    Attributes:
     left_list, right_list: The files in dir1 and dir2,
        filtered by hide and ignore.
     common: a list of names in both dir1 and dir2.
     left_only, right_only: names only in dir1, dir2.
     common_dirs: subdirectories in both dir1 and dir2.
     common_files: files in both dir1 and dir2.
     common_funny: names in both dir1 and dir2 where the type differs between
        dir1 and dir2, or the name is not stat-able.
     same_files: list of identical files.
     diff_files: list of filenames which differ.
     funny_files: list of files which could not be compared.
     subdirs: a dictionary of dircmp objects, keyed by names in common_dirs.
     Nc             C   sD   || _ || _|d kr$tjtjg| _n|| _|d kr:t| _n|| _d S )N)leftrightr   curdirpardirhider   ignore)selfabr-   r,   r   r   r   __init__x   s    zdircmp.__init__c             C   sP   t tj| j| j| j | _t tj| j| j| j | _| jj	  | jj	  d S )N)
_filterr   listdirr(   r,   r-   	left_listr)   
right_listsort)r.   r   r   r   phase0   s    
zdircmp.phase0c             C   s   t tttjj| j| j}t tttjj| j| j}tt|j	t
|j|| _tt|j	t|j|| _tt|j	t|j|| _d S )N)dictzipmapr   pathnormcaser4   r5   list__getitem__filter__contains__commonr   	left_only
right_only)r.   r/   r0   r   r   r   phase1   s
    zdircmp.phase1c       
      C   s:  g | _ g | _g | _x | jD ]}tjj| j|}tjj| j|}d}ytj	|}W n& t
k
rz } z
d}W Y d d }~X nX ytj	|}W n& t
k
r } z
d}W Y d d }~X nX |r&t	j|j}t	j|j}	||	kr| jj| n>t	j|r| j j| n&t	j|r| jj| n| jj| q| jj| qW d S )Nr   r   )common_dirscommon_filescommon_funnyrA   r   r;   joinr(   r)   r   OSErrorr   r   appendS_ISDIRS_ISREG)
r.   xZa_pathZb_pathokZa_statZwhyZb_statZa_typeZb_typer   r   r   phase2   s4    
zdircmp.phase2c             C   s&   t | j| j| j}|\| _| _| _d S )N)r   r(   r)   rF   
same_files
diff_filesfunny_files)r.   Zxxr   r   r   phase3   s    zdircmp.phase3c             C   sR   i | _ xF| jD ]<}tjj| j|}tjj| j|}t||| j| j	| j |< qW d S )N)
subdirsrE   r   r;   rH   r(   r)   r   r-   r,   )r.   rM   Za_xZb_xr   r   r   phase4   s
    zdircmp.phase4c             C   s(   | j   x| jj D ]}|j  qW d S )N)rU   rT   valuesphase4_closure)r.   sdr   r   r   rW      s    zdircmp.phase4_closurec             C   s   t d| j| j | jr2| jj  t d| jd| j | jrT| jj  t d| jd| j | jrp| jj  t d| j | jr| jj  t d| j | jr| jj  t d| j | j	r| j	j  t d| j	 | j
r| j
j  t d| j
 d S )	NZdiffzOnly in:zIdentical files :zDiffering files :zTrouble with common files :zCommon subdirectories :zCommon funny cases :)printr(   r)   rB   r6   rC   rP   rQ   rR   rE   rG   )r.   r   r   r   report   s,    






zdircmp.reportc             C   s.   | j   x | jj D ]}t  |j   qW d S )N)r[   rT   rV   rZ   )r.   rX   r   r   r   report_partial_closure   s    zdircmp.report_partial_closurec             C   s.   | j   x | jj D ]}t  |j  qW d S )N)r[   rT   rV   rZ   report_full_closure)r.   rX   r   r   r   r]      s    zdircmp.report_full_closure)rT   rP   rQ   rR   rE   rF   rG   rA   rB   rC   r4   r5   c             C   s*   || j krt|| j | |  t| |S )N)	methodmapAttributeErrorgetattr)r.   attrr   r   r   __getattr__   s    
zdircmp.__getattr__)NN)__name__
__module____qualname____doc__r1   r7   rD   rO   rS   rU   rW   r[   r\   r]   r8   r^   rb   r   r   r   r   r   V   s"    
#

c             C   sN   g g g f}x>|D ]6}t jj| |}t jj||}|t||| j| qW |S )a]  Compare common files in two directories.

    a, b -- directory names
    common -- list of file names found in both directories
    shallow -- if true, do comparison based solely on stat() information

    Returns a tuple of three lists:
      files that compare equal
      files that are different
      filenames that aren't regular files.

    )r   r;   rH   _cmprJ   )r/   r0   rA   r   ZresrM   ZaxZbxr   r   r   r      s    

c             C   s,   y||| || S  t k
r&   dS X d S )N   )rI   )r/   r0   Zshabsr   r   r   r   rg     s    rg   c             C   s   t t|j| S )N)r=   r   r@   )Zflistskipr   r   r   r2     s    r2   c              C   sr   dd l } dd l}|j| jdd  d\}}t|dkrB|jdd t|d |d }d|krf|j  n|j  d S )	Nr   r   rrh   zneed exactly two args-r )rl   rm   )sysgetoptargvr   ZGetoptErrorr   r]   r[   )rn   ro   ZoptionsargsZddr   r   r   demo$  s    
rr   __main__i    )T)T)rf   r   r   	itertoolsr   __all__r
   r"   r   r   r   r   r   r   r   ri   rg   r2   rr   rc   r   r   r   r   <module>   s&   
' %
	