cpython/Lib/cmpcache.py

69 lines
1.8 KiB
Python
Raw Normal View History

1990-10-14 03:23:40 +08:00
# 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
1992-04-01 03:04:48 +08:00
import os
1990-10-25 00:39:37 +08:00
from stat import *
1990-10-14 03:23:40 +08:00
import statcache
# The cache.
#
cache = {}
# Compare two files, use the cache if possible.
1992-04-01 03:04:48 +08:00
# May raise os.error if a stat or open of either fails.
1990-10-14 03:23:40 +08:00
#
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))
1990-10-25 00:39:37 +08:00
if not S_ISREG(s1[0]) or not S_ISREG(s2[0]):
1990-10-14 03:23:40 +08:00
# Either is a not a plain file -- always report as different
return 0
1992-01-02 03:35:13 +08:00
if s1 == s2:
1990-10-14 03:23:40 +08:00
# 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
1992-01-02 03:35:13 +08:00
if s1 == cs1 and s2 == cs2:
1990-10-14 03:23:40 +08:00
# 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):
1990-10-25 00:39:37 +08:00
return S_IFMT(st[ST_MODE]), st[ST_SIZE], st[ST_MTIME]
1990-10-14 03:23:40 +08:00
# Compare two files, really.
#
def do_cmp(f1, f2):
#print ' cmp', f1, f2 # XXX remove when debugged
1994-10-10 06:34:40 +08:00
bufsize = 8*1024 # Could be tuned
1990-10-14 03:23:40 +08:00
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