+++ /dev/null
-"""Utilities for comparing files and directories.\r
-\r
-Classes:\r
- dircmp\r
-\r
-Functions:\r
- cmp(f1, f2, shallow=1) -> int\r
- cmpfiles(a, b, common) -> ([], [], [])\r
-\r
-"""\r
-\r
-import os\r
-import stat\r
-from itertools import ifilter, ifilterfalse, imap, izip\r
-\r
-__all__ = ["cmp","dircmp","cmpfiles"]\r
-\r
-_cache = {}\r
-BUFSIZE=8*1024\r
-\r
-def cmp(f1, f2, shallow=1):\r
- """Compare two files.\r
-\r
- Arguments:\r
-\r
- f1 -- First file name\r
-\r
- f2 -- Second file name\r
-\r
- shallow -- Just check stat signature (do not read the files).\r
- defaults to 1.\r
-\r
- Return value:\r
-\r
- True if the files are the same, False otherwise.\r
-\r
- This function uses a cache for past comparisons and the results,\r
- with a cache invalidation mechanism relying on stale signatures.\r
-\r
- """\r
-\r
- s1 = _sig(os.stat(f1))\r
- s2 = _sig(os.stat(f2))\r
- if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:\r
- return False\r
- if shallow and s1 == s2:\r
- return True\r
- if s1[1] != s2[1]:\r
- return False\r
-\r
- result = _cache.get((f1, f2))\r
- if result and (s1, s2) == result[:2]:\r
- return result[2]\r
- outcome = _do_cmp(f1, f2)\r
- _cache[f1, f2] = s1, s2, outcome\r
- return outcome\r
-\r
-def _sig(st):\r
- return (stat.S_IFMT(st.st_mode),\r
- st.st_size,\r
- st.st_mtime)\r
-\r
-def _do_cmp(f1, f2):\r
- bufsize = BUFSIZE\r
- with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:\r
- while True:\r
- b1 = fp1.read(bufsize)\r
- b2 = fp2.read(bufsize)\r
- if b1 != b2:\r
- return False\r
- if not b1:\r
- return True\r
-\r
-# Directory comparison class.\r
-#\r
-class dircmp:\r
- """A class that manages the comparison of 2 directories.\r
-\r
- dircmp(a,b,ignore=None,hide=None)\r
- A and B are directories.\r
- IGNORE is a list of names to ignore,\r
- defaults to ['RCS', 'CVS', 'tags'].\r
- HIDE is a list of names to hide,\r
- defaults to [os.curdir, os.pardir].\r
-\r
- High level usage:\r
- x = dircmp(dir1, dir2)\r
- x.report() -> prints a report on the differences between dir1 and dir2\r
- or\r
- x.report_partial_closure() -> prints report on differences between dir1\r
- and dir2, and reports on common immediate subdirectories.\r
- x.report_full_closure() -> like report_partial_closure,\r
- but fully recursive.\r
-\r
- Attributes:\r
- left_list, right_list: The files in dir1 and dir2,\r
- filtered by hide and ignore.\r
- common: a list of names in both dir1 and dir2.\r
- left_only, right_only: names only in dir1, dir2.\r
- common_dirs: subdirectories in both dir1 and dir2.\r
- common_files: files in both dir1 and dir2.\r
- common_funny: names in both dir1 and dir2 where the type differs between\r
- dir1 and dir2, or the name is not stat-able.\r
- same_files: list of identical files.\r
- diff_files: list of filenames which differ.\r
- funny_files: list of files which could not be compared.\r
- subdirs: a dictionary of dircmp objects, keyed by names in common_dirs.\r
- """\r
-\r
- def __init__(self, a, b, ignore=None, hide=None): # Initialize\r
- self.left = a\r
- self.right = b\r
- if hide is None:\r
- self.hide = [os.curdir, os.pardir] # Names never to be shown\r
- else:\r
- self.hide = hide\r
- if ignore is None:\r
- self.ignore = ['RCS', 'CVS', 'tags'] # Names ignored in comparison\r
- else:\r
- self.ignore = ignore\r
-\r
- def phase0(self): # Compare everything except common subdirectories\r
- self.left_list = _filter(os.listdir(self.left),\r
- self.hide+self.ignore)\r
- self.right_list = _filter(os.listdir(self.right),\r
- self.hide+self.ignore)\r
- self.left_list.sort()\r
- self.right_list.sort()\r
-\r
- def phase1(self): # Compute common names\r
- a = dict(izip(imap(os.path.normcase, self.left_list), self.left_list))\r
- b = dict(izip(imap(os.path.normcase, self.right_list), self.right_list))\r
- self.common = map(a.__getitem__, ifilter(b.__contains__, a))\r
- self.left_only = map(a.__getitem__, ifilterfalse(b.__contains__, a))\r
- self.right_only = map(b.__getitem__, ifilterfalse(a.__contains__, b))\r
-\r
- def phase2(self): # Distinguish files, directories, funnies\r
- self.common_dirs = []\r
- self.common_files = []\r
- self.common_funny = []\r
-\r
- for x in self.common:\r
- a_path = os.path.join(self.left, x)\r
- b_path = os.path.join(self.right, x)\r
-\r
- ok = 1\r
- try:\r
- a_stat = os.stat(a_path)\r
- except os.error, why:\r
- # print 'Can\'t stat', a_path, ':', why[1]\r
- ok = 0\r
- try:\r
- b_stat = os.stat(b_path)\r
- except os.error, why:\r
- # print 'Can\'t stat', b_path, ':', why[1]\r
- ok = 0\r
-\r
- if ok:\r
- a_type = stat.S_IFMT(a_stat.st_mode)\r
- b_type = stat.S_IFMT(b_stat.st_mode)\r
- if a_type != b_type:\r
- self.common_funny.append(x)\r
- elif stat.S_ISDIR(a_type):\r
- self.common_dirs.append(x)\r
- elif stat.S_ISREG(a_type):\r
- self.common_files.append(x)\r
- else:\r
- self.common_funny.append(x)\r
- else:\r
- self.common_funny.append(x)\r
-\r
- def phase3(self): # Find out differences between common files\r
- xx = cmpfiles(self.left, self.right, self.common_files)\r
- self.same_files, self.diff_files, self.funny_files = xx\r
-\r
- def phase4(self): # Find out differences between common subdirectories\r
- # A new dircmp object is created for each common subdirectory,\r
- # these are stored in a dictionary indexed by filename.\r
- # The hide and ignore properties are inherited from the parent\r
- self.subdirs = {}\r
- for x in self.common_dirs:\r
- a_x = os.path.join(self.left, x)\r
- b_x = os.path.join(self.right, x)\r
- self.subdirs[x] = dircmp(a_x, b_x, self.ignore, self.hide)\r
-\r
- def phase4_closure(self): # Recursively call phase4() on subdirectories\r
- self.phase4()\r
- for sd in self.subdirs.itervalues():\r
- sd.phase4_closure()\r
-\r
- def report(self): # Print a report on the differences between a and b\r
- # Output format is purposely lousy\r
- print 'diff', self.left, self.right\r
- if self.left_only:\r
- self.left_only.sort()\r
- print 'Only in', self.left, ':', self.left_only\r
- if self.right_only:\r
- self.right_only.sort()\r
- print 'Only in', self.right, ':', self.right_only\r
- if self.same_files:\r
- self.same_files.sort()\r
- print 'Identical files :', self.same_files\r
- if self.diff_files:\r
- self.diff_files.sort()\r
- print 'Differing files :', self.diff_files\r
- if self.funny_files:\r
- self.funny_files.sort()\r
- print 'Trouble with common files :', self.funny_files\r
- if self.common_dirs:\r
- self.common_dirs.sort()\r
- print 'Common subdirectories :', self.common_dirs\r
- if self.common_funny:\r
- self.common_funny.sort()\r
- print 'Common funny cases :', self.common_funny\r
-\r
- def report_partial_closure(self): # Print reports on self and on subdirs\r
- self.report()\r
- for sd in self.subdirs.itervalues():\r
- print\r
- sd.report()\r
-\r
- def report_full_closure(self): # Report on self and subdirs recursively\r
- self.report()\r
- for sd in self.subdirs.itervalues():\r
- print\r
- sd.report_full_closure()\r
-\r
- methodmap = dict(subdirs=phase4,\r
- same_files=phase3, diff_files=phase3, funny_files=phase3,\r
- common_dirs = phase2, common_files=phase2, common_funny=phase2,\r
- common=phase1, left_only=phase1, right_only=phase1,\r
- left_list=phase0, right_list=phase0)\r
-\r
- def __getattr__(self, attr):\r
- if attr not in self.methodmap:\r
- raise AttributeError, attr\r
- self.methodmap[attr](self)\r
- return getattr(self, attr)\r
-\r
-def cmpfiles(a, b, common, shallow=1):\r
- """Compare common files in two directories.\r
-\r
- a, b -- directory names\r
- common -- list of file names found in both directories\r
- shallow -- if true, do comparison based solely on stat() information\r
-\r
- Returns a tuple of three lists:\r
- files that compare equal\r
- files that are different\r
- filenames that aren't regular files.\r
-\r
- """\r
- res = ([], [], [])\r
- for x in common:\r
- ax = os.path.join(a, x)\r
- bx = os.path.join(b, x)\r
- res[_cmp(ax, bx, shallow)].append(x)\r
- return res\r
-\r
-\r
-# Compare two files.\r
-# Return:\r
-# 0 for equal\r
-# 1 for different\r
-# 2 for funny cases (can't stat, etc.)\r
-#\r
-def _cmp(a, b, sh, abs=abs, cmp=cmp):\r
- try:\r
- return not abs(cmp(a, b, sh))\r
- except os.error:\r
- return 2\r
-\r
-\r
-# Return a copy with items that occur in skip removed.\r
-#\r
-def _filter(flist, skip):\r
- return list(ifilterfalse(skip.__contains__, flist))\r
-\r
-\r
-# Demonstration and testing.\r
-#\r
-def demo():\r
- import sys\r
- import getopt\r
- options, args = getopt.getopt(sys.argv[1:], 'r')\r
- if len(args) != 2:\r
- raise getopt.GetoptError('need exactly two args', None)\r
- dd = dircmp(args[0], args[1])\r
- if ('-r', '') in options:\r
- dd.report_full_closure()\r
- else:\r
- dd.report()\r
-\r
-if __name__ == '__main__':\r
- demo()\r