blob: 089c6674969ae3d83a30f7378641052fe616058e [file] [log] [blame]
Guido van Rossum63b08ac2000-06-29 14:13:28 +00001"""Utilities for comparing files and directories.
Guido van Rossum2d726871999-10-26 14:02:01 +00002
Guido van Rossum63b08ac2000-06-29 14:13:28 +00003Classes:
4 dircmp
5
6Functions:
Andrew M. Kuchling83e879d2003-02-06 19:38:45 +00007 cmp(f1, f2, shallow=1) -> int
Guido van Rossum63b08ac2000-06-29 14:13:28 +00008 cmpfiles(a, b, common) -> ([], [], [])
9
10"""
11
12import os
13import stat
Andrew M. Kuchling83e879d2003-02-06 19:38:45 +000014import warnings
Raymond Hettingereeca37e2003-09-02 05:42:02 +000015from itertools import ifilter, ifilterfalse, imap, izip
Guido van Rossum2d726871999-10-26 14:02:01 +000016
Skip Montanaroeccd02a2001-01-20 23:34:12 +000017__all__ = ["cmp","dircmp","cmpfiles"]
18
Guido van Rossum2d726871999-10-26 14:02:01 +000019_cache = {}
20BUFSIZE=8*1024
21
Andrew M. Kuchling83e879d2003-02-06 19:38:45 +000022def cmp(f1, f2, shallow=1, use_statcache=None):
Guido van Rossum63b08ac2000-06-29 14:13:28 +000023 """Compare two files.
Guido van Rossum2d726871999-10-26 14:02:01 +000024
Guido van Rossum63b08ac2000-06-29 14:13:28 +000025 Arguments:
Guido van Rossum2d726871999-10-26 14:02:01 +000026
Guido van Rossum63b08ac2000-06-29 14:13:28 +000027 f1 -- First file name
Guido van Rossum2d726871999-10-26 14:02:01 +000028
Guido van Rossum63b08ac2000-06-29 14:13:28 +000029 f2 -- Second file name
Guido van Rossum2d726871999-10-26 14:02:01 +000030
Guido van Rossum63b08ac2000-06-29 14:13:28 +000031 shallow -- Just check stat signature (do not read the files).
32 defaults to 1.
Guido van Rossum2d726871999-10-26 14:02:01 +000033
Andrew M. Kuchling8eb40442003-02-06 17:50:01 +000034 use_statcache -- obsolete argument.
Guido van Rossum2d726871999-10-26 14:02:01 +000035
Guido van Rossum63b08ac2000-06-29 14:13:28 +000036 Return value:
Guido van Rossum2d726871999-10-26 14:02:01 +000037
Tim Petersbc0e9102002-04-04 22:55:58 +000038 True if the files are the same, False otherwise.
Guido van Rossum2d726871999-10-26 14:02:01 +000039
Guido van Rossum63b08ac2000-06-29 14:13:28 +000040 This function uses a cache for past comparisons and the results,
41 with a cache invalidation mechanism relying on stale signatures.
Guido van Rossum2d726871999-10-26 14:02:01 +000042
Guido van Rossum63b08ac2000-06-29 14:13:28 +000043 """
Andrew M. Kuchling83e879d2003-02-06 19:38:45 +000044 if use_statcache is not None:
45 warnings.warn("use_statcache argument is deprecated",
46 DeprecationWarning)
47
Andrew M. Kuchling8eb40442003-02-06 17:50:01 +000048 s1 = _sig(os.stat(f1))
49 s2 = _sig(os.stat(f2))
Guido van Rossum63b08ac2000-06-29 14:13:28 +000050 if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
Tim Petersbc0e9102002-04-04 22:55:58 +000051 return False
Guido van Rossum63b08ac2000-06-29 14:13:28 +000052 if shallow and s1 == s2:
Tim Petersbc0e9102002-04-04 22:55:58 +000053 return True
Guido van Rossum63b08ac2000-06-29 14:13:28 +000054 if s1[1] != s2[1]:
Tim Petersbc0e9102002-04-04 22:55:58 +000055 return False
Guido van Rossum2d726871999-10-26 14:02:01 +000056
Guido van Rossum63b08ac2000-06-29 14:13:28 +000057 result = _cache.get((f1, f2))
58 if result and (s1, s2) == result[:2]:
59 return result[2]
60 outcome = _do_cmp(f1, f2)
61 _cache[f1, f2] = s1, s2, outcome
62 return outcome
Guido van Rossum2d726871999-10-26 14:02:01 +000063
64def _sig(st):
Raymond Hettinger32200ae2002-06-01 19:51:15 +000065 return (stat.S_IFMT(st.st_mode),
66 st.st_size,
67 st.st_mtime)
Guido van Rossum2d726871999-10-26 14:02:01 +000068
69def _do_cmp(f1, f2):
Guido van Rossum63b08ac2000-06-29 14:13:28 +000070 bufsize = BUFSIZE
71 fp1 = open(f1, 'rb')
72 fp2 = open(f2, 'rb')
Raymond Hettinger05595e92003-02-27 00:05:31 +000073 while True:
Guido van Rossum63b08ac2000-06-29 14:13:28 +000074 b1 = fp1.read(bufsize)
75 b2 = fp2.read(bufsize)
76 if b1 != b2:
Raymond Hettinger05595e92003-02-27 00:05:31 +000077 return False
Guido van Rossum63b08ac2000-06-29 14:13:28 +000078 if not b1:
Raymond Hettinger05595e92003-02-27 00:05:31 +000079 return True
Guido van Rossum63b08ac2000-06-29 14:13:28 +000080
81# Directory comparison class.
82#
83class dircmp:
84 """A class that manages the comparison of 2 directories.
85
86 dircmp(a,b,ignore=None,hide=None)
87 A and B are directories.
88 IGNORE is a list of names to ignore,
89 defaults to ['RCS', 'CVS', 'tags'].
90 HIDE is a list of names to hide,
91 defaults to [os.curdir, os.pardir].
92
93 High level usage:
94 x = dircmp(dir1, dir2)
95 x.report() -> prints a report on the differences between dir1 and dir2
96 or
97 x.report_partial_closure() -> prints report on differences between dir1
98 and dir2, and reports on common immediate subdirectories.
99 x.report_full_closure() -> like report_partial_closure,
100 but fully recursive.
101
102 Attributes:
103 left_list, right_list: The files in dir1 and dir2,
104 filtered by hide and ignore.
105 common: a list of names in both dir1 and dir2.
106 left_only, right_only: names only in dir1, dir2.
107 common_dirs: subdirectories in both dir1 and dir2.
108 common_files: files in both dir1 and dir2.
109 common_funny: names in both dir1 and dir2 where the type differs between
110 dir1 and dir2, or the name is not stat-able.
111 same_files: list of identical files.
112 diff_files: list of filenames which differ.
113 funny_files: list of files which could not be compared.
114 subdirs: a dictionary of dircmp objects, keyed by names in common_dirs.
115 """
116
117 def __init__(self, a, b, ignore=None, hide=None): # Initialize
118 self.left = a
119 self.right = b
120 if hide is None:
121 self.hide = [os.curdir, os.pardir] # Names never to be shown
122 else:
123 self.hide = hide
124 if ignore is None:
125 self.ignore = ['RCS', 'CVS', 'tags'] # Names ignored in comparison
126 else:
127 self.ignore = ignore
128
129 def phase0(self): # Compare everything except common subdirectories
130 self.left_list = _filter(os.listdir(self.left),
131 self.hide+self.ignore)
132 self.right_list = _filter(os.listdir(self.right),
133 self.hide+self.ignore)
134 self.left_list.sort()
135 self.right_list.sort()
136
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000137 def phase1(self): # Compute common names
Raymond Hettingereeca37e2003-09-02 05:42:02 +0000138 a = dict(izip(imap(os.path.normcase, self.left_list), self.left_list))
139 b = dict(izip(imap(os.path.normcase, self.right_list), self.right_list))
140 self.common = map(a.__getitem__, ifilter(b.has_key, a))
141 self.left_only = map(a.__getitem__, ifilterfalse(b.has_key, a))
142 self.right_only = map(b.__getitem__, ifilterfalse(a.has_key, b))
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000143
144 def phase2(self): # Distinguish files, directories, funnies
145 self.common_dirs = []
146 self.common_files = []
147 self.common_funny = []
148
149 for x in self.common:
150 a_path = os.path.join(self.left, x)
151 b_path = os.path.join(self.right, x)
152
153 ok = 1
154 try:
Andrew M. Kuchling8eb40442003-02-06 17:50:01 +0000155 a_stat = os.stat(a_path)
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000156 except os.error, why:
157 # print 'Can\'t stat', a_path, ':', why[1]
158 ok = 0
159 try:
Andrew M. Kuchling8eb40442003-02-06 17:50:01 +0000160 b_stat = os.stat(b_path)
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000161 except os.error, why:
162 # print 'Can\'t stat', b_path, ':', why[1]
163 ok = 0
164
165 if ok:
Raymond Hettinger32200ae2002-06-01 19:51:15 +0000166 a_type = stat.S_IFMT(a_stat.st_mode)
167 b_type = stat.S_IFMT(b_stat.st_mode)
Fred Drake8152d322000-12-12 23:20:45 +0000168 if a_type != b_type:
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000169 self.common_funny.append(x)
170 elif stat.S_ISDIR(a_type):
171 self.common_dirs.append(x)
172 elif stat.S_ISREG(a_type):
173 self.common_files.append(x)
174 else:
175 self.common_funny.append(x)
176 else:
177 self.common_funny.append(x)
178
179 def phase3(self): # Find out differences between common files
180 xx = cmpfiles(self.left, self.right, self.common_files)
181 self.same_files, self.diff_files, self.funny_files = xx
182
183 def phase4(self): # Find out differences between common subdirectories
184 # A new dircmp object is created for each common subdirectory,
185 # these are stored in a dictionary indexed by filename.
186 # The hide and ignore properties are inherited from the parent
187 self.subdirs = {}
188 for x in self.common_dirs:
189 a_x = os.path.join(self.left, x)
190 b_x = os.path.join(self.right, x)
191 self.subdirs[x] = dircmp(a_x, b_x, self.ignore, self.hide)
192
193 def phase4_closure(self): # Recursively call phase4() on subdirectories
194 self.phase4()
Raymond Hettingere0d49722002-06-02 18:55:56 +0000195 for sd in self.subdirs.itervalues():
196 sd.phase4_closure()
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000197
198 def report(self): # Print a report on the differences between a and b
199 # Output format is purposely lousy
200 print 'diff', self.left, self.right
201 if self.left_only:
202 self.left_only.sort()
203 print 'Only in', self.left, ':', self.left_only
204 if self.right_only:
205 self.right_only.sort()
206 print 'Only in', self.right, ':', self.right_only
207 if self.same_files:
208 self.same_files.sort()
209 print 'Identical files :', self.same_files
210 if self.diff_files:
211 self.diff_files.sort()
212 print 'Differing files :', self.diff_files
213 if self.funny_files:
214 self.funny_files.sort()
215 print 'Trouble with common files :', self.funny_files
216 if self.common_dirs:
217 self.common_dirs.sort()
218 print 'Common subdirectories :', self.common_dirs
219 if self.common_funny:
220 self.common_funny.sort()
221 print 'Common funny cases :', self.common_funny
222
223 def report_partial_closure(self): # Print reports on self and on subdirs
224 self.report()
Raymond Hettingere0d49722002-06-02 18:55:56 +0000225 for sd in self.subdirs.itervalues():
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000226 print
Raymond Hettingere0d49722002-06-02 18:55:56 +0000227 sd.report()
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000228
229 def report_full_closure(self): # Report on self and subdirs recursively
230 self.report()
Raymond Hettingere0d49722002-06-02 18:55:56 +0000231 for sd in self.subdirs.itervalues():
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000232 print
Raymond Hettingere0d49722002-06-02 18:55:56 +0000233 sd.report_full_closure()
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000234
Raymond Hettinger05595e92003-02-27 00:05:31 +0000235 methodmap = dict(subdirs=phase4,
236 same_files=phase3, diff_files=phase3, funny_files=phase3,
237 common_dirs = phase2, common_files=phase2, common_funny=phase2,
238 common=phase1, left_only=phase1, right_only=phase1,
239 left_list=phase0, right_list=phase0)
240
241 def __getattr__(self, attr):
242 if attr not in self.methodmap:
243 raise AttributeError, attr
244 self.methodmap[attr](self)
245 return getattr(self, attr)
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000246
Andrew M. Kuchling83e879d2003-02-06 19:38:45 +0000247def cmpfiles(a, b, common, shallow=1, use_statcache=None):
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000248 """Compare common files in two directories.
249
Fred Drake2b0d98b2000-07-03 08:18:47 +0000250 a, b -- directory names
251 common -- list of file names found in both directories
252 shallow -- if true, do comparison based solely on stat() information
Andrew M. Kuchling8eb40442003-02-06 17:50:01 +0000253 use_statcache -- obsolete argument
Fred Drake2b0d98b2000-07-03 08:18:47 +0000254
255 Returns a tuple of three lists:
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000256 files that compare equal
257 files that are different
Fred Drake2b0d98b2000-07-03 08:18:47 +0000258 filenames that aren't regular files.
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000259
Fred Drake2b0d98b2000-07-03 08:18:47 +0000260 """
Andrew M. Kuchling83e879d2003-02-06 19:38:45 +0000261 if use_statcache is not None:
262 warnings.warn("use_statcache argument is deprecated",
263 DeprecationWarning)
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000264 res = ([], [], [])
265 for x in common:
Fred Drake2b0d98b2000-07-03 08:18:47 +0000266 ax = os.path.join(a, x)
267 bx = os.path.join(b, x)
Andrew M. Kuchling8eb40442003-02-06 17:50:01 +0000268 res[_cmp(ax, bx, shallow)].append(x)
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000269 return res
270
271
272# Compare two files.
273# Return:
Tim Peters88869f92001-01-14 23:36:06 +0000274# 0 for equal
275# 1 for different
276# 2 for funny cases (can't stat, etc.)
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000277#
Raymond Hettinger05595e92003-02-27 00:05:31 +0000278def _cmp(a, b, sh, abs=abs, cmp=cmp):
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000279 try:
Andrew M. Kuchling8eb40442003-02-06 17:50:01 +0000280 return not abs(cmp(a, b, sh))
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000281 except os.error:
282 return 2
283
284
285# Return a copy with items that occur in skip removed.
286#
Raymond Hettinger05595e92003-02-27 00:05:31 +0000287def _filter(flist, skip):
288 return list(ifilterfalse(skip.__contains__, flist))
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000289
290
291# Demonstration and testing.
292#
293def demo():
294 import sys
295 import getopt
296 options, args = getopt.getopt(sys.argv[1:], 'r')
Fred Drake8152d322000-12-12 23:20:45 +0000297 if len(args) != 2:
Andrew M. Kuchling83e879d2003-02-06 19:38:45 +0000298 raise getopt.GetoptError('need exactly two args', None)
Guido van Rossum63b08ac2000-06-29 14:13:28 +0000299 dd = dircmp(args[0], args[1])
300 if ('-r', '') in options:
301 dd.report_full_closure()
302 else:
303 dd.report()
304
305if __name__ == '__main__':
306 demo()