| 1 | n/a | """Utilities for comparing files and directories. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Classes: |
|---|
| 4 | n/a | dircmp |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | Functions: |
|---|
| 7 | n/a | cmp(f1, f2, shallow=True) -> int |
|---|
| 8 | n/a | cmpfiles(a, b, common) -> ([], [], []) |
|---|
| 9 | n/a | clear_cache() |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | """ |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | import os |
|---|
| 14 | n/a | import stat |
|---|
| 15 | n/a | from itertools import filterfalse |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | __all__ = ['clear_cache', 'cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES'] |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | _cache = {} |
|---|
| 20 | n/a | BUFSIZE = 8*1024 |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | DEFAULT_IGNORES = [ |
|---|
| 23 | n/a | 'RCS', 'CVS', 'tags', '.git', '.hg', '.bzr', '_darcs', '__pycache__'] |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | def clear_cache(): |
|---|
| 26 | n/a | """Clear the filecmp cache.""" |
|---|
| 27 | n/a | _cache.clear() |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | def cmp(f1, f2, shallow=True): |
|---|
| 30 | n/a | """Compare two files. |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | Arguments: |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | f1 -- First file name |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | f2 -- Second file name |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | shallow -- Just check stat signature (do not read the files). |
|---|
| 39 | n/a | defaults to True. |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | Return value: |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | True if the files are the same, False otherwise. |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | This function uses a cache for past comparisons and the results, |
|---|
| 46 | n/a | with cache entries invalidated if their stat information |
|---|
| 47 | n/a | changes. The cache may be cleared by calling clear_cache(). |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | """ |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | s1 = _sig(os.stat(f1)) |
|---|
| 52 | n/a | s2 = _sig(os.stat(f2)) |
|---|
| 53 | n/a | if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG: |
|---|
| 54 | n/a | return False |
|---|
| 55 | n/a | if shallow and s1 == s2: |
|---|
| 56 | n/a | return True |
|---|
| 57 | n/a | if s1[1] != s2[1]: |
|---|
| 58 | n/a | return False |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | outcome = _cache.get((f1, f2, s1, s2)) |
|---|
| 61 | n/a | if outcome is None: |
|---|
| 62 | n/a | outcome = _do_cmp(f1, f2) |
|---|
| 63 | n/a | if len(_cache) > 100: # limit the maximum size of the cache |
|---|
| 64 | n/a | clear_cache() |
|---|
| 65 | n/a | _cache[f1, f2, s1, s2] = outcome |
|---|
| 66 | n/a | return outcome |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def _sig(st): |
|---|
| 69 | n/a | return (stat.S_IFMT(st.st_mode), |
|---|
| 70 | n/a | st.st_size, |
|---|
| 71 | n/a | st.st_mtime) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | def _do_cmp(f1, f2): |
|---|
| 74 | n/a | bufsize = BUFSIZE |
|---|
| 75 | n/a | with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2: |
|---|
| 76 | n/a | while True: |
|---|
| 77 | n/a | b1 = fp1.read(bufsize) |
|---|
| 78 | n/a | b2 = fp2.read(bufsize) |
|---|
| 79 | n/a | if b1 != b2: |
|---|
| 80 | n/a | return False |
|---|
| 81 | n/a | if not b1: |
|---|
| 82 | n/a | return True |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | # Directory comparison class. |
|---|
| 85 | n/a | # |
|---|
| 86 | n/a | class dircmp: |
|---|
| 87 | n/a | """A class that manages the comparison of 2 directories. |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | dircmp(a, b, ignore=None, hide=None) |
|---|
| 90 | n/a | A and B are directories. |
|---|
| 91 | n/a | IGNORE is a list of names to ignore, |
|---|
| 92 | n/a | defaults to DEFAULT_IGNORES. |
|---|
| 93 | n/a | HIDE is a list of names to hide, |
|---|
| 94 | n/a | defaults to [os.curdir, os.pardir]. |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | High level usage: |
|---|
| 97 | n/a | x = dircmp(dir1, dir2) |
|---|
| 98 | n/a | x.report() -> prints a report on the differences between dir1 and dir2 |
|---|
| 99 | n/a | or |
|---|
| 100 | n/a | x.report_partial_closure() -> prints report on differences between dir1 |
|---|
| 101 | n/a | and dir2, and reports on common immediate subdirectories. |
|---|
| 102 | n/a | x.report_full_closure() -> like report_partial_closure, |
|---|
| 103 | n/a | but fully recursive. |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | Attributes: |
|---|
| 106 | n/a | left_list, right_list: The files in dir1 and dir2, |
|---|
| 107 | n/a | filtered by hide and ignore. |
|---|
| 108 | n/a | common: a list of names in both dir1 and dir2. |
|---|
| 109 | n/a | left_only, right_only: names only in dir1, dir2. |
|---|
| 110 | n/a | common_dirs: subdirectories in both dir1 and dir2. |
|---|
| 111 | n/a | common_files: files in both dir1 and dir2. |
|---|
| 112 | n/a | common_funny: names in both dir1 and dir2 where the type differs between |
|---|
| 113 | n/a | dir1 and dir2, or the name is not stat-able. |
|---|
| 114 | n/a | same_files: list of identical files. |
|---|
| 115 | n/a | diff_files: list of filenames which differ. |
|---|
| 116 | n/a | funny_files: list of files which could not be compared. |
|---|
| 117 | n/a | subdirs: a dictionary of dircmp objects, keyed by names in common_dirs. |
|---|
| 118 | n/a | """ |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | def __init__(self, a, b, ignore=None, hide=None): # Initialize |
|---|
| 121 | n/a | self.left = a |
|---|
| 122 | n/a | self.right = b |
|---|
| 123 | n/a | if hide is None: |
|---|
| 124 | n/a | self.hide = [os.curdir, os.pardir] # Names never to be shown |
|---|
| 125 | n/a | else: |
|---|
| 126 | n/a | self.hide = hide |
|---|
| 127 | n/a | if ignore is None: |
|---|
| 128 | n/a | self.ignore = DEFAULT_IGNORES |
|---|
| 129 | n/a | else: |
|---|
| 130 | n/a | self.ignore = ignore |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | def phase0(self): # Compare everything except common subdirectories |
|---|
| 133 | n/a | self.left_list = _filter(os.listdir(self.left), |
|---|
| 134 | n/a | self.hide+self.ignore) |
|---|
| 135 | n/a | self.right_list = _filter(os.listdir(self.right), |
|---|
| 136 | n/a | self.hide+self.ignore) |
|---|
| 137 | n/a | self.left_list.sort() |
|---|
| 138 | n/a | self.right_list.sort() |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | def phase1(self): # Compute common names |
|---|
| 141 | n/a | a = dict(zip(map(os.path.normcase, self.left_list), self.left_list)) |
|---|
| 142 | n/a | b = dict(zip(map(os.path.normcase, self.right_list), self.right_list)) |
|---|
| 143 | n/a | self.common = list(map(a.__getitem__, filter(b.__contains__, a))) |
|---|
| 144 | n/a | self.left_only = list(map(a.__getitem__, filterfalse(b.__contains__, a))) |
|---|
| 145 | n/a | self.right_only = list(map(b.__getitem__, filterfalse(a.__contains__, b))) |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def phase2(self): # Distinguish files, directories, funnies |
|---|
| 148 | n/a | self.common_dirs = [] |
|---|
| 149 | n/a | self.common_files = [] |
|---|
| 150 | n/a | self.common_funny = [] |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | for x in self.common: |
|---|
| 153 | n/a | a_path = os.path.join(self.left, x) |
|---|
| 154 | n/a | b_path = os.path.join(self.right, x) |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | ok = 1 |
|---|
| 157 | n/a | try: |
|---|
| 158 | n/a | a_stat = os.stat(a_path) |
|---|
| 159 | n/a | except OSError as why: |
|---|
| 160 | n/a | # print('Can\'t stat', a_path, ':', why.args[1]) |
|---|
| 161 | n/a | ok = 0 |
|---|
| 162 | n/a | try: |
|---|
| 163 | n/a | b_stat = os.stat(b_path) |
|---|
| 164 | n/a | except OSError as why: |
|---|
| 165 | n/a | # print('Can\'t stat', b_path, ':', why.args[1]) |
|---|
| 166 | n/a | ok = 0 |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | if ok: |
|---|
| 169 | n/a | a_type = stat.S_IFMT(a_stat.st_mode) |
|---|
| 170 | n/a | b_type = stat.S_IFMT(b_stat.st_mode) |
|---|
| 171 | n/a | if a_type != b_type: |
|---|
| 172 | n/a | self.common_funny.append(x) |
|---|
| 173 | n/a | elif stat.S_ISDIR(a_type): |
|---|
| 174 | n/a | self.common_dirs.append(x) |
|---|
| 175 | n/a | elif stat.S_ISREG(a_type): |
|---|
| 176 | n/a | self.common_files.append(x) |
|---|
| 177 | n/a | else: |
|---|
| 178 | n/a | self.common_funny.append(x) |
|---|
| 179 | n/a | else: |
|---|
| 180 | n/a | self.common_funny.append(x) |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | def phase3(self): # Find out differences between common files |
|---|
| 183 | n/a | xx = cmpfiles(self.left, self.right, self.common_files) |
|---|
| 184 | n/a | self.same_files, self.diff_files, self.funny_files = xx |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | def phase4(self): # Find out differences between common subdirectories |
|---|
| 187 | n/a | # A new dircmp object is created for each common subdirectory, |
|---|
| 188 | n/a | # these are stored in a dictionary indexed by filename. |
|---|
| 189 | n/a | # The hide and ignore properties are inherited from the parent |
|---|
| 190 | n/a | self.subdirs = {} |
|---|
| 191 | n/a | for x in self.common_dirs: |
|---|
| 192 | n/a | a_x = os.path.join(self.left, x) |
|---|
| 193 | n/a | b_x = os.path.join(self.right, x) |
|---|
| 194 | n/a | self.subdirs[x] = dircmp(a_x, b_x, self.ignore, self.hide) |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | def phase4_closure(self): # Recursively call phase4() on subdirectories |
|---|
| 197 | n/a | self.phase4() |
|---|
| 198 | n/a | for sd in self.subdirs.values(): |
|---|
| 199 | n/a | sd.phase4_closure() |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | def report(self): # Print a report on the differences between a and b |
|---|
| 202 | n/a | # Output format is purposely lousy |
|---|
| 203 | n/a | print('diff', self.left, self.right) |
|---|
| 204 | n/a | if self.left_only: |
|---|
| 205 | n/a | self.left_only.sort() |
|---|
| 206 | n/a | print('Only in', self.left, ':', self.left_only) |
|---|
| 207 | n/a | if self.right_only: |
|---|
| 208 | n/a | self.right_only.sort() |
|---|
| 209 | n/a | print('Only in', self.right, ':', self.right_only) |
|---|
| 210 | n/a | if self.same_files: |
|---|
| 211 | n/a | self.same_files.sort() |
|---|
| 212 | n/a | print('Identical files :', self.same_files) |
|---|
| 213 | n/a | if self.diff_files: |
|---|
| 214 | n/a | self.diff_files.sort() |
|---|
| 215 | n/a | print('Differing files :', self.diff_files) |
|---|
| 216 | n/a | if self.funny_files: |
|---|
| 217 | n/a | self.funny_files.sort() |
|---|
| 218 | n/a | print('Trouble with common files :', self.funny_files) |
|---|
| 219 | n/a | if self.common_dirs: |
|---|
| 220 | n/a | self.common_dirs.sort() |
|---|
| 221 | n/a | print('Common subdirectories :', self.common_dirs) |
|---|
| 222 | n/a | if self.common_funny: |
|---|
| 223 | n/a | self.common_funny.sort() |
|---|
| 224 | n/a | print('Common funny cases :', self.common_funny) |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | def report_partial_closure(self): # Print reports on self and on subdirs |
|---|
| 227 | n/a | self.report() |
|---|
| 228 | n/a | for sd in self.subdirs.values(): |
|---|
| 229 | n/a | print() |
|---|
| 230 | n/a | sd.report() |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | def report_full_closure(self): # Report on self and subdirs recursively |
|---|
| 233 | n/a | self.report() |
|---|
| 234 | n/a | for sd in self.subdirs.values(): |
|---|
| 235 | n/a | print() |
|---|
| 236 | n/a | sd.report_full_closure() |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | methodmap = dict(subdirs=phase4, |
|---|
| 239 | n/a | same_files=phase3, diff_files=phase3, funny_files=phase3, |
|---|
| 240 | n/a | common_dirs = phase2, common_files=phase2, common_funny=phase2, |
|---|
| 241 | n/a | common=phase1, left_only=phase1, right_only=phase1, |
|---|
| 242 | n/a | left_list=phase0, right_list=phase0) |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | def __getattr__(self, attr): |
|---|
| 245 | n/a | if attr not in self.methodmap: |
|---|
| 246 | n/a | raise AttributeError(attr) |
|---|
| 247 | n/a | self.methodmap[attr](self) |
|---|
| 248 | n/a | return getattr(self, attr) |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | def cmpfiles(a, b, common, shallow=True): |
|---|
| 251 | n/a | """Compare common files in two directories. |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | a, b -- directory names |
|---|
| 254 | n/a | common -- list of file names found in both directories |
|---|
| 255 | n/a | shallow -- if true, do comparison based solely on stat() information |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | Returns a tuple of three lists: |
|---|
| 258 | n/a | files that compare equal |
|---|
| 259 | n/a | files that are different |
|---|
| 260 | n/a | filenames that aren't regular files. |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | """ |
|---|
| 263 | n/a | res = ([], [], []) |
|---|
| 264 | n/a | for x in common: |
|---|
| 265 | n/a | ax = os.path.join(a, x) |
|---|
| 266 | n/a | bx = os.path.join(b, x) |
|---|
| 267 | n/a | res[_cmp(ax, bx, shallow)].append(x) |
|---|
| 268 | n/a | return res |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | |
|---|
| 271 | n/a | # Compare two files. |
|---|
| 272 | n/a | # Return: |
|---|
| 273 | n/a | # 0 for equal |
|---|
| 274 | n/a | # 1 for different |
|---|
| 275 | n/a | # 2 for funny cases (can't stat, etc.) |
|---|
| 276 | n/a | # |
|---|
| 277 | n/a | def _cmp(a, b, sh, abs=abs, cmp=cmp): |
|---|
| 278 | n/a | try: |
|---|
| 279 | n/a | return not abs(cmp(a, b, sh)) |
|---|
| 280 | n/a | except OSError: |
|---|
| 281 | n/a | return 2 |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | # Return a copy with items that occur in skip removed. |
|---|
| 285 | n/a | # |
|---|
| 286 | n/a | def _filter(flist, skip): |
|---|
| 287 | n/a | return list(filterfalse(skip.__contains__, flist)) |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | # Demonstration and testing. |
|---|
| 291 | n/a | # |
|---|
| 292 | n/a | def demo(): |
|---|
| 293 | n/a | import sys |
|---|
| 294 | n/a | import getopt |
|---|
| 295 | n/a | options, args = getopt.getopt(sys.argv[1:], 'r') |
|---|
| 296 | n/a | if len(args) != 2: |
|---|
| 297 | n/a | raise getopt.GetoptError('need exactly two args', None) |
|---|
| 298 | n/a | dd = dircmp(args[0], args[1]) |
|---|
| 299 | n/a | if ('-r', '') in options: |
|---|
| 300 | n/a | dd.report_full_closure() |
|---|
| 301 | n/a | else: |
|---|
| 302 | n/a | dd.report() |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | if __name__ == '__main__': |
|---|
| 305 | n/a | demo() |
|---|