summaryrefslogtreecommitdiff
path: root/Lib/filecmp.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-10-26 14:02:01 +0000
committerGuido van Rossum <guido@python.org>1999-10-26 14:02:01 +0000
commite459ae5231612fd7e8fee49b47e216d5605370ae (patch)
treef070b9b900e93c13d919947c0354e972eba2f52e /Lib/filecmp.py
parentccf70d6f0bcc1f2571c828a5ff2e262b0817f44d (diff)
downloadcpython-e459ae5231612fd7e8fee49b47e216d5605370ae.tar.gz
New module by Moshe Zadka (submitted on Sept. 25). This unifies the
functionality of cmp.py and cmpcache.py, which are hereby declared obsolescent.
Diffstat (limited to 'Lib/filecmp.py')
-rw-r--r--Lib/filecmp.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/Lib/filecmp.py b/Lib/filecmp.py
new file mode 100644
index 0000000000..e1a30bc5f0
--- /dev/null
+++ b/Lib/filecmp.py
@@ -0,0 +1,57 @@
+"""Compare files."""
+
+import os, stat, statcache
+
+_cache = {}
+BUFSIZE=8*1024
+
+def cmp(f1, f2, shallow=1,use_statcache=0):
+ """Compare two files.
+
+ Arguments:
+
+ f1 -- First file name
+
+ f2 -- Second file name
+
+ shallow -- Just check stat signature (do not read the files).
+ defaults to 1.
+
+ use_statcache -- Do not stat() each file directly: go through
+ the statcache module for more efficiency.
+
+ Return value:
+
+ integer -- 1 if the files are the same, 0 otherwise.
+
+ This function uses a cache for past comparisons and the results,
+ with a cache invalidation mechanism relying on stale signatures.
+ Of course, if 'use_statcache' is true, this mechanism is defeated,
+ and the cache will never grow stale.
+
+ """
+ stat_function = (os.stat, statcache.stat)[use_statcache]
+ s1, s2 = _sig(stat_function(f1)), _sig(stat_function(f2))
+ if s1[0]!=stat.S_IFREG or s2[0]!=stat.S_IFREG: return 0
+ if shallow and s1 == s2: return 1
+ if s1[1]!=s2[1]: return 0
+
+ result = _cache.get((f1, f2))
+ if result and (s1, s2)==result[:2]:
+ return result[2]
+ outcome = _do_cmp(f1, f2)
+ _cache[f1, f2] = s1, s2, outcome
+ return outcome
+
+def _sig(st):
+ return (stat.S_IFMT(st[stat.ST_MODE]),
+ st[stat.ST_SIZE],
+ st[stat.ST_MTIME])
+
+def _do_cmp(f1, f2):
+ bufsize = BUFSIZE
+ fp1 , fp2 = open(f1, 'rb'), open(f2, 'rb')
+ while 1:
+ b1, b2 = fp1.read(bufsize), fp2.read(bufsize)
+ if b1!=b2: return 0
+ if not b1: return 1