Back to index

python3.2  3.2.2
Classes | Functions | Variables
filecmp Namespace Reference

Classes

class  dircmp

Functions

def cmp
def _sig
def _do_cmp
def cmpfiles
def _cmp
def _filter
def demo

Variables

list __all__ = ["cmp", "dircmp", "cmpfiles"]
dictionary _cache = {}
int BUFSIZE = 8

Detailed Description

Utilities for comparing files and directories.

Classes:
    dircmp

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

Function Documentation

def filecmp._cmp (   a,
  b,
  sh,
  abs = abs,
  cmp = cmp 
) [private]

Definition at line 268 of file filecmp.py.

00268 
00269 def _cmp(a, b, sh, abs=abs, cmp=cmp):
00270     try:
00271         return not abs(cmp(a, b, sh))
00272     except os.error:
00273         return 2
00274 
00275 
00276 # Return a copy with items that occur in skip removed.
#

Here is the call graph for this function:

Here is the caller graph for this function:

def filecmp._do_cmp (   f1,
  f2 
) [private]

Definition at line 64 of file filecmp.py.

00064 
00065 def _do_cmp(f1, f2):
00066     bufsize = BUFSIZE
00067     with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:
00068         while True:
00069             b1 = fp1.read(bufsize)
00070             b2 = fp2.read(bufsize)
00071             if b1 != b2:
00072                 return False
00073             if not b1:
00074                 return True
00075 
00076 # Directory comparison class.
#

Here is the caller graph for this function:

def filecmp._filter (   flist,
  skip 
) [private]

Definition at line 277 of file filecmp.py.

00277 
00278 def _filter(flist, skip):
00279     return list(filterfalse(skip.__contains__, flist))
00280 
00281 
00282 # Demonstration and testing.
#

Here is the caller graph for this function:

def filecmp._sig (   st) [private]

Definition at line 59 of file filecmp.py.

00059 
00060 def _sig(st):
00061     return (stat.S_IFMT(st.st_mode),
00062             st.st_size,
00063             st.st_mtime)

Here is the call graph for this function:

Here is the caller graph for this function:

def filecmp.cmp (   f1,
  f2,
  shallow = True 
)
Compare two files.

Arguments:

f1 -- First file name

f2 -- Second file name

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

Return value:

True if the files are the same, False otherwise.

This function uses a cache for past comparisons and the results,
with a cache invalidation mechanism relying on stale signatures.

Definition at line 21 of file filecmp.py.

00021 
00022 def cmp(f1, f2, shallow=True):
00023     """Compare two files.
00024 
00025     Arguments:
00026 
00027     f1 -- First file name
00028 
00029     f2 -- Second file name
00030 
00031     shallow -- Just check stat signature (do not read the files).
00032                defaults to 1.
00033 
00034     Return value:
00035 
00036     True if the files are the same, False otherwise.
00037 
00038     This function uses a cache for past comparisons and the results,
00039     with a cache invalidation mechanism relying on stale signatures.
00040 
00041     """
00042 
00043     s1 = _sig(os.stat(f1))
00044     s2 = _sig(os.stat(f2))
00045     if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
00046         return False
00047     if shallow and s1 == s2:
00048         return True
00049     if s1[1] != s2[1]:
00050         return False
00051 
00052     outcome = _cache.get((f1, f2, s1, s2))
00053     if outcome is None:
00054         outcome = _do_cmp(f1, f2)
00055         if len(_cache) > 100:      # limit the maximum size of the cache
00056             _cache.clear()
00057         _cache[f1, f2, s1, s2] = outcome
00058     return outcome

Here is the call graph for this function:

Here is the caller graph for this function:

def filecmp.cmpfiles (   a,
  b,
  common,
  shallow = True 
)
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.

Definition at line 241 of file filecmp.py.

00241 
00242 def cmpfiles(a, b, common, shallow=True):
00243     """Compare common files in two directories.
00244 
00245     a, b -- directory names
00246     common -- list of file names found in both directories
00247     shallow -- if true, do comparison based solely on stat() information
00248 
00249     Returns a tuple of three lists:
00250       files that compare equal
00251       files that are different
00252       filenames that aren't regular files.
00253 
00254     """
00255     res = ([], [], [])
00256     for x in common:
00257         ax = os.path.join(a, x)
00258         bx = os.path.join(b, x)
00259         res[_cmp(ax, bx, shallow)].append(x)
00260     return res
00261 
00262 
00263 # Compare two files.
00264 # Return:
00265 #       0 for equal
00266 #       1 for different
00267 #       2 for funny cases (can't stat, etc.)
#

Here is the call graph for this function:

Here is the caller graph for this function:

def filecmp.demo ( )

Definition at line 283 of file filecmp.py.

00283 
00284 def demo():
00285     import sys
00286     import getopt
00287     options, args = getopt.getopt(sys.argv[1:], 'r')
00288     if len(args) != 2:
00289         raise getopt.GetoptError('need exactly two args', None)
00290     dd = dircmp(args[0], args[1])
00291     if ('-r', '') in options:
00292         dd.report_full_closure()
00293     else:
00294         dd.report()

Here is the call graph for this function:


Variable Documentation

list filecmp.__all__ = ["cmp", "dircmp", "cmpfiles"]

Definition at line 16 of file filecmp.py.

Definition at line 18 of file filecmp.py.

Definition at line 19 of file filecmp.py.