cmpcache.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » lib-old » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Lib » lib old » cmpcache.py
"""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 os
from stat import *
import statcache


# The cache.
#
cache = {}


def cmp(f1, f2, shallow=1):
    """Compare two files, use the cache if possible.
    May raise os.error if a stat or open of either fails.
    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 shallow and 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

def sig(st):
    """Return signature (i.e., type, size, mtime) from raw stat data."""
    return S_IFMT(st[ST_MODE]), st[ST_SIZE], st[ST_MTIME]

def do_cmp(f1, f2):
    """Compare two files, really."""
    #print '    cmp', f1, f2 # XXX remove when debugged
    bufsize = 8*1024 # Could be tuned
    fp1 = open(f1, 'rb')
    fp2 = open(f2, 'rb')
    while 1:
        b1 = fp1.read(bufsize)
        b2 = fp2.read(bufsize)
        if b1 != b2: return 0
        if not b1: return 1
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.