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