aboutsummaryrefslogtreecommitdiff
path: root/lib/cmpcache.py
diff options
context:
space:
mode:
authorSkip Montanaro <[email protected]>2021-02-16 14:40:46 -0600
committerSkip Montanaro <[email protected]>2021-02-16 14:40:46 -0600
commita19a216bc60160c162e616145ef091dd18ce4e61 (patch)
treefa4bdff21f9b04a125c84a2bfab8a1c738359e15 /lib/cmpcache.py
downloadpython-0.9.1-patched-QoL-a19a216bc60160c162e616145ef091dd18ce4e61.tar.xz
python-0.9.1-patched-QoL-a19a216bc60160c162e616145ef091dd18ce4e61.zip
Python 0.9.1 as posted in alt.sources
Diffstat (limited to 'lib/cmpcache.py')
-rw-r--r--lib/cmpcache.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/cmpcache.py b/lib/cmpcache.py
new file mode 100644
index 0000000..4aea356
--- /dev/null
+++ b/lib/cmpcache.py
@@ -0,0 +1,68 @@
+# Module 'cmpcache'
+#
+# Efficiently compare files, boolean outcome only (equal / not equal).
+#
+# Tricks (used in this order):
+# - Use the statcache module to avoid statting files more than once
+# - Files with identical type, size & mtime are assumed to be clones
+# - Files with different type or size cannot be identical
+# - We keep a cache of outcomes of earlier comparisons
+# - We don't fork a process to run 'cmp' but read the files ourselves
+
+import posix
+from stat import *
+import statcache
+
+
+# The cache.
+#
+cache = {}
+
+
+# Compare two files, use the cache if possible.
+# May raise posix.error if a stat or open of either fails.
+#
+def cmp(f1, f2):
+ # Return 1 for identical files, 0 for different.
+ # Raise exceptions if either file could not be statted, read, etc.
+ s1, s2 = sig(statcache.stat(f1)), sig(statcache.stat(f2))
+ if not S_ISREG(s1[0]) or not S_ISREG(s2[0]):
+ # Either is a not a plain file -- always report as different
+ return 0
+ if s1 = s2:
+ # type, size & mtime match -- report same
+ return 1
+ if s1[:2] <> s2[:2]: # Types or sizes differ, don't bother
+ # types or sizes differ -- report different
+ return 0
+ # same type and size -- look in the cache
+ key = f1 + ' ' + f2
+ if cache.has_key(key):
+ cs1, cs2, outcome = cache[key]
+ # cache hit
+ if s1 = cs1 and s2 = cs2:
+ # cached signatures match
+ return outcome
+ # stale cached signature(s)
+ # really compare
+ outcome = do_cmp(f1, f2)
+ cache[key] = s1, s2, outcome
+ return outcome
+
+# Return signature (i.e., type, size, mtime) from raw stat data.
+#
+def sig(st):
+ return S_IFMT(st[ST_MODE]), st[ST_SIZE], st[ST_MTIME]
+
+# Compare two files, really.
+#
+def do_cmp(f1, f2):
+ #print ' cmp', f1, f2 # XXX remove when debugged
+ bufsize = 8096 # Could be tuned
+ fp1 = open(f1, 'r')
+ fp2 = open(f2, 'r')
+ while 1:
+ b1 = fp1.read(bufsize)
+ b2 = fp2.read(bufsize)
+ if b1 <> b2: return 0
+ if not b1: return 1