
    Rh'                         d Z ddlZddlZddlmZ ddlmZ g dZi ZdZ	g dZ
d Zdd	Zd
 Zd Z G d d      ZddZeefdZd Zd Zedk(  r e        yy)zUtilities for comparing files and directories.

Classes:
    dircmp

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

    N)filterfalse)GenericAlias)clear_cachecmpdircmpcmpfilesDEFAULT_IGNORESi    )RCSCVStagsz.gitz.hgz.bzr_darcs__pycache__c                  ,    t         j                          y)zClear the filecmp cache.N)_cacheclear     /usr/lib/python3.12/filecmp.pyr   r      s    
LLNr   c                    t        t        j                  |             }t        t        j                  |            }|d   t        j                  k7  s|d   t        j                  k7  ry|r||k(  ry|d   |d   k7  ryt        j                  | |||f      }|5t        | |      }t        t              dkD  r
t                |t        | |||f<   |S )aM  Compare two files.

    Arguments:

    f1 -- First file name

    f2 -- Second file name

    shallow -- treat files as identical if their stat signatures (type, size,
               mtime) are identical. Otherwise, files are considered different
               if their sizes or contents differ.  [default: 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   d   )	_sigosstatS_IFREGr   get_do_cmplenr   )f1f2shallows1s2outcomes         r   r   r      s    . 
bggbk	B	bggbk	B	!u1 528	!u1~jj"b"b)*G"b/v;M!(r2r2~Nr   c                 n    t        j                  | j                        | j                  | j                  fS N)r   S_IFMTst_modest_sizest_mtime)sts    r   r   r   F   s*    KK

#JJKK r   c                 "   t         }t        | d      5 }t        |d      5 }	 |j                  |      }|j                  |      }||k7  r	 d d d        d d d        y|s	 d d d        d d d        yN# 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)NrbTF)BUFSIZEopenread)r   r    bufsizefp1fp2b1b2s          r   r   r   K   s    G	b$ 3R #'"B'"BRx         s4   B*A0
BA0B/A00A9	5BBc                       e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Z ee	eeeeeeeeeee      Zd Z ee      Zy)r   a  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 instances (or MyDirCmp instances if this
       object is of type MyDirCmp, a subclass of dircmp), keyed by names
       in common_dirs.
     Nc                     || _         || _        |&t        j                  t        j                  g| _        n|| _        |t        | _        y || _        y r&   )leftrightr   curdirpardirhider	   ignore)selfabr=   r<   s        r   __init__zdircmp.__init__|   sC    	
<BII.DIDI>)DK DKr   c                    t        t        j                  | j                        | j                  | j
                  z         | _        t        t        j                  | j                        | j                  | j
                  z         | _        | j                  j                          | j                  j                          y r&   )
_filterr   listdirr8   r<   r=   	left_listr9   
right_listsortr>   s    r   phase0zdircmp.phase0   sx     DII!6!%4;;!68!"**TZZ"8"&))DKK"79r   c           	         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                  |                  | _        y r&   )dictzipmapr   pathnormcaserE   rF   list__getitem__filter__contains__commonr   	left_only
right_only)r>   r?   r@   s      r   phase1zdircmp.phase1   s    S))4>>:DNNKLS))4??;T__MN3q}}fQ^^Q.GHIc!--Q^^Q1OPQs1==+anna2PQRr   c                    g | _         g | _        g | _        | j                  D ]  }t        j
                  j                  | j                  |      }t        j
                  j                  | j                  |      }d}	 t	        j                  |      }	 t	        j                  |      }|rt        j                  j                        }t        j                  j                        }||k7  r| j                  j                  |       t        j                  |      r| j                   j                  |       t        j                  |      r| j                  j                  |       K| j                  j                  |       h| j                  j                  |        y # t        $ r d}Y %w xY w# t        $ r d}Y !w xY w)NTF)common_dirscommon_filescommon_funnyrT   r   rN   joinr8   r9   r   OSErrorr'   r(   appendS_ISDIRS_ISREG)	r>   xa_pathb_pathoka_statb_stata_typeb_types	            r   phase2zdircmp.phase2   s^    	,AWW\\$))Q/FWW\\$**a0FB
 V^^4V^^4V#%%,,Q/\\&)$$++A.\\&)%%,,Q/%%,,Q/!!((+9	,  
  s$   =F)F;)F87F8;G
	G
c                     t        | j                  | j                  | j                        }|\  | _        | _        | _        y r&   )r   r8   r9   rZ   
same_files
diff_filesfunny_files)r>   xxs     r   phase3zdircmp.phase3   s1    diiT->->?=?:$*:r   c                 F   i | _         | j                  D ]  }t        j                  j	                  | j
                  |      }t        j                  j	                  | j                  |      }| j                  ||| j                  | j                        | j                   |<    y r&   )
subdirsrY   r   rN   r\   r8   r9   	__class__r=   r<   )r>   ra   a_xb_xs       r   phase4zdircmp.phase4   sv    
 !! 	PA'',,tyy!,C'',,tzz1-C#~~c3TYYODLLO	Pr   c                     | j                          | j                  j                         D ]  }|j                           y r&   )ru   rq   valuesphase4_closurer>   sds     r   rx   zdircmp.phase4_closure   s3    ,,%%' 	 B	 r   c                    t        d| j                  | j                         | j                  r<| j                  j	                          t        d| j                  d| j                         | j
                  r<| j
                  j	                          t        d| j                  d| j
                         | j                  r0| j                  j	                          t        d| j                         | j                  r0| j                  j	                          t        d| j                         | j                  r0| j                  j	                          t        d| j                         | j                  r0| j                  j	                          t        d| j                         | j                  r1| j                  j	                          t        d| j                         y y )	NdiffzOnly in:zIdentical files :zDiffering files :zTrouble with common files :zCommon subdirectories :zCommon funny cases :)printr8   r9   rU   rG   rV   rk   rl   rm   rY   r[   rH   s    r   reportzdircmp.report   sE   fdii,>>NN!)TYYT^^<??OO  ")TZZdoo>??OO  "%t7??OO  "%t7!!#/1A1AB!!#+T-=-=>""$($*;*;< r   c                     | j                          | j                  j                         D ]  }t                |j                           y r&   )r   rq   rw   r~   ry   s     r   report_partial_closurezdircmp.report_partial_closure   s5    ,,%%' 	BGIIK	r   c                     | j                          | j                  j                         D ]  }t                |j	                           y r&   )r   rq   rw   r~   report_full_closurery   s     r   r   zdircmp.report_full_closure   s8    ,,%%' 	%BG""$	%r   )rq   rk   rl   rm   rY   rZ   r[   rT   rU   rV   rE   rF   c                 v    || j                   vrt        |       | j                   |   |        t        | |      S r&   )	methodmapAttributeErrorgetattr)r>   attrs     r   __getattr__zdircmp.__getattr__   s9    t~~% &&tT"tT""r   )NN)__name__
__module____qualname____doc__rA   rI   rW   ri   ro   ru   rx   r   r   r   rK   r   r   classmethodr   __class_getitem__r   r   r   r   r   X   sx    !	F
!S!,F@	P 
=2% V &6v!'f6"f%&	:I# $L1r   r   c                     g g g f}|D ]a  }t         j                  j                  | |      }t         j                  j                  ||      }|t        |||         j	                  |       c |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   rN   r\   _cmpr^   )r?   r@   rT   r!   resra   axbxs           r   r   r     si     r2,C -WW\\!QWW\\!QDR!"))!,- Jr   c                 D    	  | || ||             S # t         $ r Y yw xY w)N   )r]   )r?   r@   shabsr   s        r   r   r     s/    s1a}%%% s    	c                 @    t        t        |j                  |             S r&   )rP   r   rS   )flistskips     r   rC   rC   &  s    D--u566r   c                     dd l } dd l}|j                  | j                  dd  d      \  }}t        |      dk7  r|j	                  dd       t        |d   |d         }d|v r|j                          y |j                          y )Nr   r   rr   zneed exactly two args)z-r )sysgetoptargvr   GetoptErrorr   r   r   )r   r   optionsargsdds        r   demor   ,  sv    MM#((12,4MGT
4yA~  !8$??	Qa	!BW
 
		r   __main__)T)r   r   r   	itertoolsr   typesr   __all__r   r.   r	   r   r   r   r   r   r   r   r   rC   r   r   r   r   r   <module>r      s   
 
  ! 
I	
J&P
	g2 g2T6  7
 zF r   