|
| 1 | +"""This module contains a code example related to |
| 2 | +
|
| 3 | +Think Python, 2nd Edition |
| 4 | +by Allen Downey |
| 5 | +http://thinkpython2.com |
| 6 | +
|
| 7 | +Copyright 2015 Allen Downey |
| 8 | +
|
| 9 | +License: http://creativecommons.org/licenses/by/4.0/ |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import print_function, division |
| 13 | + |
| 14 | +import os |
| 15 | + |
| 16 | + |
| 17 | +def walk(dirname): |
| 18 | + """Finds the names of all files in dirname and its subdirectories. |
| 19 | +
|
| 20 | + dirname: string name of directory |
| 21 | + """ |
| 22 | + names = [] |
| 23 | + for name in os.listdir(dirname): |
| 24 | + path = os.path.join(dirname, name) |
| 25 | + |
| 26 | + if os.path.isfile(path): |
| 27 | + names.append(path) |
| 28 | + else: |
| 29 | + names.extend(walk(path)) |
| 30 | + return names |
| 31 | + |
| 32 | + |
| 33 | +def compute_checksum(filename): |
| 34 | + """Computes the MD5 checksum of the contents of a file. |
| 35 | +
|
| 36 | + filename: string |
| 37 | + """ |
| 38 | + cmd = 'md5sum ' + filename |
| 39 | + return pipe(cmd) |
| 40 | + |
| 41 | + |
| 42 | +def check_diff(name1, name2): |
| 43 | + """Computes the difference between the contents of two files. |
| 44 | +
|
| 45 | + name1, name2: string filenames |
| 46 | + """ |
| 47 | + cmd = 'diff %s %s' % (name1, name2) |
| 48 | + return pipe(cmd) |
| 49 | + |
| 50 | + |
| 51 | +def pipe(cmd): |
| 52 | + """Runs a command in a subprocess. |
| 53 | +
|
| 54 | + cmd: string Unix command |
| 55 | +
|
| 56 | + Returns (res, stat), the output of the subprocess and the exit status. |
| 57 | + """ |
| 58 | + # Note: os.popen is deprecated |
| 59 | + # now, which means we are supposed to stop using it and start using |
| 60 | + # the subprocess module. But for simple cases, I find |
| 61 | + # subprocess more complicated than necessary. So I am going |
| 62 | + # to keep using os.popen until they take it away. |
| 63 | + |
| 64 | + fp = os.popen(cmd) |
| 65 | + res = fp.read() |
| 66 | + stat = fp.close() |
| 67 | + assert stat is None |
| 68 | + return res, stat |
| 69 | + |
| 70 | + |
| 71 | +def compute_checksums(dirname, suffix): |
| 72 | + """Computes checksums for all files with the given suffix. |
| 73 | +
|
| 74 | + dirname: string name of directory to search |
| 75 | + suffix: string suffix to match |
| 76 | +
|
| 77 | + Returns: map from checksum to list of files with that checksum |
| 78 | + """ |
| 79 | + names = walk(dirname) |
| 80 | + |
| 81 | + d = {} |
| 82 | + for name in names: |
| 83 | + if name.endswith(suffix): |
| 84 | + res, stat = compute_checksum(name) |
| 85 | + checksum, _ = res.split() |
| 86 | + |
| 87 | + if checksum in d: |
| 88 | + d[checksum].append(name) |
| 89 | + else: |
| 90 | + d[checksum] = [name] |
| 91 | + |
| 92 | + return d |
| 93 | + |
| 94 | + |
| 95 | +def check_pairs(names): |
| 96 | + """Checks whether any in a list of files differs from the others. |
| 97 | +
|
| 98 | + names: list of string filenames |
| 99 | + """ |
| 100 | + for name1 in names: |
| 101 | + for name2 in names: |
| 102 | + if name1 < name2: |
| 103 | + res, stat = check_diff(name1, name2) |
| 104 | + if res: |
| 105 | + return False |
| 106 | + return True |
| 107 | + |
| 108 | + |
| 109 | +def print_duplicates(d): |
| 110 | + """Checks for duplicate files. |
| 111 | +
|
| 112 | + Reports any files with the same checksum and checks whether they |
| 113 | + are, in fact, identical. |
| 114 | +
|
| 115 | + d: map from checksum to list of files with that checksum |
| 116 | + """ |
| 117 | + for key, names in d.iteritems(): |
| 118 | + if len(names) > 1: |
| 119 | + print('The following files have the same checksum:') |
| 120 | + for name in names: |
| 121 | + print(name) |
| 122 | + |
| 123 | + if check_pairs(names): |
| 124 | + print('And they are identical.') |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + d = compute_checksums(dirname='.', suffix='.py') |
| 129 | + print_duplicates(d) |
0 commit comments