Compare two files. Arguments: f1 -- First file name f2 -- Second file name shallow -- treat files as identical if their stat signatures (type, size, mtime) are identical. Otherwise, files are considered different if their sizes or contents differ. [
(f1, f2, shallow=True)
| 28 | _cache.clear() |
| 29 | |
| 30 | def cmp(f1, f2, shallow=True): |
| 31 | """Compare two files. |
| 32 | |
| 33 | Arguments: |
| 34 | |
| 35 | f1 -- First file name |
| 36 | |
| 37 | f2 -- Second file name |
| 38 | |
| 39 | shallow -- treat files as identical if their stat signatures (type, size, |
| 40 | mtime) are identical. Otherwise, files are considered different |
| 41 | if their sizes or contents differ. [default: True] |
| 42 | |
| 43 | Return value: |
| 44 | |
| 45 | True if the files are the same, False otherwise. |
| 46 | |
| 47 | This function uses a cache for past comparisons and the results, |
| 48 | with cache entries invalidated if their stat information |
| 49 | changes. The cache may be cleared by calling clear_cache(). |
| 50 | |
| 51 | """ |
| 52 | |
| 53 | s1 = _sig(os.stat(f1)) |
| 54 | s2 = _sig(os.stat(f2)) |
| 55 | if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG: |
| 56 | return False |
| 57 | if shallow and s1 == s2: |
| 58 | return True |
| 59 | if s1[1] != s2[1]: |
| 60 | return False |
| 61 | |
| 62 | outcome = _cache.get((f1, f2, s1, s2)) |
| 63 | if outcome is None: |
| 64 | outcome = _do_cmp(f1, f2) |
| 65 | if len(_cache) > 100: # limit the maximum size of the cache |
| 66 | clear_cache() |
| 67 | _cache[f1, f2, s1, s2] = outcome |
| 68 | return outcome |
| 69 | |
| 70 | def _sig(st): |
| 71 | return (stat.S_IFMT(st.st_mode), |
no test coverage detected