o w[e'@sdZddlZddlZddlmZddlmZgdZiZdZ gdZ dd Z dd d Z d dZ ddZGdddZdddZee fddZddZddZedkrWedSdS)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_cachecmpdircmpcmpfilesDEFAULT_IGNORESi )RCSCVStagsz.gitz.hgz.bzr_darcs __pycache__cCs tdS)zClear the filecmp cache.N)_cacheclearrr/usr/lib/python3.10/filecmp.pyrs rTcCstt|}tt|}|dtjks|dtjkrdS|r&||kr&dS|d|dkr0dSt||||f}|durSt||}ttdkrKt|t||||f<|S)aMCompare 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(). rFTNd) _sigosstatS_IFREGrget_do_cmplenr)f1f2shallows1s2outcomerrrrs   rcCst|j|j|jfSN)rS_IFMTst_modest_sizest_mtime)strrrrFs rc Cst}t|dG}t|d1} ||}||}||kr, WdWddS|s= WdWddSq1sAwYWddS1sQwYdS)NrbTF)BUFSIZEopenread)rrbufsizefp1fp2b1b2rrrrKs  4rc@seZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ee eeeeeeeeeeed ZddZeeZdS)raA 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. NcCsF||_||_|durtjtjg|_n||_|durt|_dS||_dSr!)leftrightrcurdirpardirhiderignore)selfabr5r4rrr__init__|s  zdircmp.__init__cCsPtt|j|j|j|_tt|j|j|j|_|j |j dSr!) _filterrlistdirr0r4r5 left_listr1 right_listsortr6rrrphase0s     z dircmp.phase0cCsttttjj|j|j}ttttjj|j|j}tt|j t |j ||_ tt|j t |j ||_tt|j t |j ||_dSr!)dictzipmaprpathnormcaser<r=list __getitem__filter __contains__commonr left_only right_only)r6r7r8rrrphase1s z dircmp.phase1c Csg|_g|_g|_|jD]x}tj|j|}tj|j|}d}zt |}Wn t y2d}Ynwzt |}Wn t yEd}Ynw|r~t |j }t |j }||kr_|j |q t |rk|j |q t |rw|j |q |j |q |j |q dS)Nrr) common_dirs common_files common_funnyrJrrDjoinr0r1rOSErrorr"r#appendS_ISDIRS_ISREG) r6xa_pathb_pathoka_statb_stata_typeb_typerrrphase2s:       z dircmp.phase2cCs&t|j|j|j}|\|_|_|_dSr!)rr0r1rO same_files diff_files funny_files)r6xxrrrphase3sz dircmp.phase3cCsPi|_|jD]}tj|j|}tj|j|}||||j|j |j|<qdSr!) subdirsrNrrDrQr0r1 __class__r5r4)r6rVa_xb_xrrrphase4s  z dircmp.phase4cCs$||jD]}|q dSr!)rhrdvaluesphase4_closurer6sdrrrrjs zdircmp.phase4_closurecCstd|j|j|jr|jtd|jd|j|jr*|jtd|jd|j|jr8|jtd|j|jrF|jtd|j|jrT|jtd|j|j rb|j td|j |j rr|j td|j dSdS) NdiffzOnly in:zIdentical files :zDiffering files :zTrouble with common files :zCommon subdirectories :zCommon funny cases :) printr0r1rKr>rLr_r`rarNrPr?rrrreports.           z dircmp.reportcCs*||jD] }t|q dSr!)rprdrirorkrrrreport_partial_closure  zdircmp.report_partial_closurecCs*||jD] }t|q dSr!)rprdriroreport_full_closurerkrrrrsrrzdircmp.report_full_closure) rdr_r`rarNrOrPrJrKrLr<r=cCs*||jvr t||j||t||Sr!) methodmapAttributeErrorgetattr)r6attrrrr __getattr__s  zdircmp.__getattr__)NN)__name__ __module__ __qualname____doc__r9r@rMr^rcrhrjrprqrsrArtrx classmethodr__class_getitem__rrrrrXs( # #  rcCsJgggf}|D]}tj||}tj||}|t||||q|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. )rrDrQ_cmprS)r7r8rJrresrVaxbxrrrrs rcCs*z ||||| WStyYdSw)N)rR)r7r8shabsrrrrrs  rcCstt|j|Sr!)rFrrI)flistskiprrrr:&sr:cCstddl}ddl}||jddd\}}t|dkr!|ddt|d|d}d|vr4|dS|dS)Nrrrrzneed exactly two args)z-r)sysgetoptargvr GetoptErrorrrsrp)rroptionsargsddrrrdemo,s    r__main__)T)r|rr itertoolsrtypesr__all__rr(rrrrrrrrrr:rryrrrrs,    (  +