| [3225] | 1 | """Utilities for comparing files and directories.
|
|---|
| 2 |
|
|---|
| 3 | Classes:
|
|---|
| 4 | dircmp
|
|---|
| 5 |
|
|---|
| 6 | Functions:
|
|---|
| 7 | cmp(f1, f2, shallow=1) -> int
|
|---|
| 8 | cmpfiles(a, b, common) -> ([], [], [])
|
|---|
| 9 |
|
|---|
| 10 | """
|
|---|
| 11 |
|
|---|
| 12 | import os
|
|---|
| 13 | import stat
|
|---|
| 14 | import warnings
|
|---|
| 15 | from itertools import ifilter, ifilterfalse, imap, izip
|
|---|
| 16 |
|
|---|
| 17 | __all__ = ["cmp","dircmp","cmpfiles"]
|
|---|
| 18 |
|
|---|
| 19 | _cache = {}
|
|---|
| 20 | BUFSIZE=8*1024
|
|---|
| 21 |
|
|---|
| 22 | def cmp(f1, f2, shallow=1):
|
|---|
| 23 | """Compare two files.
|
|---|
| 24 |
|
|---|
| 25 | Arguments:
|
|---|
| 26 |
|
|---|
| 27 | f1 -- First file name
|
|---|
| 28 |
|
|---|
| 29 | f2 -- Second file name
|
|---|
| 30 |
|
|---|
| 31 | shallow -- Just check stat signature (do not read the files).
|
|---|
| 32 | defaults to 1.
|
|---|
| 33 |
|
|---|
| 34 | Return value:
|
|---|
| 35 |
|
|---|
| 36 | True if the files are the same, False otherwise.
|
|---|
| 37 |
|
|---|
| 38 | This function uses a cache for past comparisons and the results,
|
|---|
| 39 | with a cache invalidation mechanism relying on stale signatures.
|
|---|
| 40 |
|
|---|
| 41 | """
|
|---|
| 42 |
|
|---|
| 43 | s1 = _sig(os.stat(f1))
|
|---|
| 44 | s2 = _sig(os.stat(f2))
|
|---|
| 45 | if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
|
|---|
| 46 | return False
|
|---|
| 47 | if shallow and s1 == s2:
|
|---|
| 48 | return True
|
|---|
| 49 | if s1[1] != s2[1]:
|
|---|
| 50 | return False
|
|---|
| 51 |
|
|---|
| 52 | result = _cache.get((f1, f2))
|
|---|
| 53 | if result and (s1, s2) == result[:2]:
|
|---|
| 54 | return result[2]
|
|---|
| 55 | outcome = _do_cmp(f1, f2)
|
|---|
| 56 | _cache[f1, f2] = s1, s2, outcome
|
|---|
| 57 | return outcome
|
|---|
| 58 |
|
|---|
| 59 | def _sig(st):
|
|---|
| 60 | return (stat.S_IFMT(st.st_mode),
|
|---|
| 61 | st.st_size,
|
|---|
| 62 | st.st_mtime)
|
|---|
| 63 |
|
|---|
| 64 | def _do_cmp(f1, f2):
|
|---|
| 65 | bufsize = BUFSIZE
|
|---|
| 66 | fp1 = open(f1, 'rb')
|
|---|
| 67 | fp2 = open(f2, 'rb')
|
|---|
| 68 | while True:
|
|---|
| 69 | b1 = fp1.read(bufsize)
|
|---|
| 70 | b2 = fp2.read(bufsize)
|
|---|
| 71 | if b1 != b2:
|
|---|
| 72 | return False
|
|---|
| 73 | if not b1:
|
|---|
| 74 | return True
|
|---|
| 75 |
|
|---|
| 76 | # Directory comparison class.
|
|---|
| 77 | #
|
|---|
| 78 | class dircmp:
|
|---|
| 79 | """A class that manages the comparison of 2 directories.
|
|---|
| 80 |
|
|---|
| 81 | dircmp(a,b,ignore=None,hide=None)
|
|---|
| 82 | A and B are directories.
|
|---|
| 83 | IGNORE is a list of names to ignore,
|
|---|
| 84 | defaults to ['RCS', 'CVS', 'tags'].
|
|---|
| 85 | HIDE is a list of names to hide,
|
|---|
| 86 | defaults to [os.curdir, os.pardir].
|
|---|
| 87 |
|
|---|
| 88 | High level usage:
|
|---|
| 89 | x = dircmp(dir1, dir2)
|
|---|
| 90 | x.report() -> prints a report on the differences between dir1 and dir2
|
|---|
| 91 | or
|
|---|
| 92 | x.report_partial_closure() -> prints report on differences between dir1
|
|---|
| 93 | and dir2, and reports on common immediate subdirectories.
|
|---|
| 94 | x.report_full_closure() -> like report_partial_closure,
|
|---|
| 95 | but fully recursive.
|
|---|
| 96 |
|
|---|
| 97 | Attributes:
|
|---|
| 98 | left_list, right_list: The files in dir1 and dir2,
|
|---|
| 99 | filtered by hide and ignore.
|
|---|
| 100 | common: a list of names in both dir1 and dir2.
|
|---|
| 101 | left_only, right_only: names only in dir1, dir2.
|
|---|
| 102 | common_dirs: subdirectories in both dir1 and dir2.
|
|---|
| 103 | common_files: files in both dir1 and dir2.
|
|---|
| 104 | common_funny: names in both dir1 and dir2 where the type differs between
|
|---|
| 105 | dir1 and dir2, or the name is not stat-able.
|
|---|
| 106 | same_files: list of identical files.
|
|---|
| 107 | diff_files: list of filenames which differ.
|
|---|
| 108 | funny_files: list of files which could not be compared.
|
|---|
| 109 | subdirs: a dictionary of dircmp objects, keyed by names in common_dirs.
|
|---|
| 110 | """
|
|---|
| 111 |
|
|---|
| 112 | def __init__(self, a, b, ignore=None, hide=None): # Initialize
|
|---|
| 113 | self.left = a
|
|---|
| 114 | self.right = b
|
|---|
| 115 | if hide is None:
|
|---|
| 116 | self.hide = [os.curdir, os.pardir] # Names never to be shown
|
|---|
| 117 | else:
|
|---|
| 118 | self.hide = hide
|
|---|
| 119 | if ignore is None:
|
|---|
| 120 | self.ignore = ['RCS', 'CVS', 'tags'] # Names ignored in comparison
|
|---|
| 121 | else:
|
|---|
| 122 | self.ignore = ignore
|
|---|
| 123 |
|
|---|
| 124 | def phase0(self): # Compare everything except common subdirectories
|
|---|
| 125 | self.left_list = _filter(os.listdir(self.left),
|
|---|
| 126 | self.hide+self.ignore)
|
|---|
| 127 | self.right_list = _filter(os.listdir(self.right),
|
|---|
| 128 | self.hide+self.ignore)
|
|---|
| 129 | self.left_list.sort()
|
|---|
| 130 | self.right_list.sort()
|
|---|
| 131 |
|
|---|
| 132 | def phase1(self): # Compute common names
|
|---|
| 133 | a = dict(izip(imap(os.path.normcase, self.left_list), self.left_list))
|
|---|
| 134 | b = dict(izip(imap(os.path.normcase, self.right_list), self.right_list))
|
|---|
| 135 | self.common = map(a.__getitem__, ifilter(b.has_key, a))
|
|---|
| 136 | self.left_only = map(a.__getitem__, ifilterfalse(b.has_key, a))
|
|---|
| 137 | self.right_only = map(b.__getitem__, ifilterfalse(a.has_key, b))
|
|---|
| 138 |
|
|---|
| 139 | def phase2(self): # Distinguish files, directories, funnies
|
|---|
| 140 | self.common_dirs = []
|
|---|
| 141 | self.common_files = []
|
|---|
| 142 | self.common_funny = []
|
|---|
| 143 |
|
|---|
| 144 | for x in self.common:
|
|---|
| 145 | a_path = os.path.join(self.left, x)
|
|---|
| 146 | b_path = os.path.join(self.right, x)
|
|---|
| 147 |
|
|---|
| 148 | ok = 1
|
|---|
| 149 | try:
|
|---|
| 150 | a_stat = os.stat(a_path)
|
|---|
| 151 | except os.error, why:
|
|---|
| 152 | # print 'Can\'t stat', a_path, ':', why[1]
|
|---|
| 153 | ok = 0
|
|---|
| 154 | try:
|
|---|
| 155 | b_stat = os.stat(b_path)
|
|---|
| 156 | except os.error, why:
|
|---|
| 157 | # print 'Can\'t stat', b_path, ':', why[1]
|
|---|
| 158 | ok = 0
|
|---|
| 159 |
|
|---|
| 160 | if ok:
|
|---|
| 161 | a_type = stat.S_IFMT(a_stat.st_mode)
|
|---|
| 162 | b_type = stat.S_IFMT(b_stat.st_mode)
|
|---|
| 163 | if a_type != b_type:
|
|---|
| 164 | self.common_funny.append(x)
|
|---|
| 165 | elif stat.S_ISDIR(a_type):
|
|---|
| 166 | self.common_dirs.append(x)
|
|---|
| 167 | elif stat.S_ISREG(a_type):
|
|---|
| 168 | self.common_files.append(x)
|
|---|
| 169 | else:
|
|---|
| 170 | self.common_funny.append(x)
|
|---|
| 171 | else:
|
|---|
| 172 | self.common_funny.append(x)
|
|---|
| |
|---|