Guido van Rossum | 9966e2c | 2001-06-22 16:05:48 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Python utility to print MD5 checksums of argument files. |
Guido van Rossum | 9966e2c | 2001-06-22 16:05:48 +0000 | [diff] [blame] | 4 | """ |
| 5 | |
Guido van Rossum | 9966e2c | 2001-06-22 16:05:48 +0000 | [diff] [blame] | 6 | |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 7 | bufsize = 8096 |
| 8 | fnfilter = None |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 9 | rmode = 'rb' |
Guido van Rossum | 9966e2c | 2001-06-22 16:05:48 +0000 | [diff] [blame] | 10 | |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 11 | usage = """ |
| 12 | usage: sum5 [-b] [-t] [-l] [-s bufsize] [file ...] |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 13 | -b : read files in binary mode (default) |
| 14 | -t : read files in text mode (you almost certainly don't want this!) |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 15 | -l : print last pathname component only |
| 16 | -s bufsize: read buffer size (default %d) |
| 17 | file ... : files to sum; '-' or no files means stdin |
| 18 | """ % bufsize |
| 19 | |
| 20 | import sys |
| 21 | import os |
| 22 | import getopt |
| 23 | import md5 |
| 24 | |
| 25 | def sum(*files): |
| 26 | sts = 0 |
| 27 | if files and isinstance(files[-1], file): |
| 28 | out, files = files[-1], files[:-1] |
| 29 | else: |
| 30 | out = sys.stdout |
| 31 | if len(files) == 1 and not isinstance(files[0], str): |
| 32 | files = files[0] |
| 33 | for f in files: |
| 34 | if isinstance(f, str): |
| 35 | if f == '-': |
| 36 | sts = printsumfp(sys.stdin, '<stdin>', out) or sts |
| 37 | else: |
| 38 | sts = printsum(f, out) or sts |
| 39 | else: |
| 40 | sts = sum(f, out) or sts |
| 41 | return sts |
| 42 | |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 43 | def printsum(filename, out=sys.stdout): |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 44 | try: |
| 45 | fp = open(filename, rmode) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 46 | except IOError as msg: |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 47 | sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg)) |
| 48 | return 1 |
| 49 | if fnfilter: |
| 50 | filename = fnfilter(filename) |
| 51 | sts = printsumfp(fp, filename, out) |
| 52 | fp.close() |
| 53 | return sts |
| 54 | |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 55 | def printsumfp(fp, filename, out=sys.stdout): |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 56 | m = md5.new() |
| 57 | try: |
Guido van Rossum | 9966e2c | 2001-06-22 16:05:48 +0000 | [diff] [blame] | 58 | while 1: |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 59 | data = fp.read(bufsize) |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 60 | if not data: |
| 61 | break |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 62 | m.update(data) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 63 | except IOError as msg: |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 64 | sys.stderr.write('%s: I/O error: %s\n' % (filename, msg)) |
| 65 | return 1 |
| 66 | out.write('%s %s\n' % (m.hexdigest(), filename)) |
| 67 | return 0 |
Guido van Rossum | 9966e2c | 2001-06-22 16:05:48 +0000 | [diff] [blame] | 68 | |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 69 | def main(args = sys.argv[1:], out=sys.stdout): |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 70 | global fnfilter, rmode, bufsize |
| 71 | try: |
| 72 | opts, args = getopt.getopt(args, 'blts:') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 73 | except getopt.error as msg: |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 74 | sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage)) |
| 75 | return 2 |
| 76 | for o, a in opts: |
| 77 | if o == '-l': |
| 78 | fnfilter = os.path.basename |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 79 | elif o == '-b': |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 80 | rmode = 'rb' |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 81 | elif o == '-t': |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 82 | rmode = 'r' |
Tim Peters | 1a3abcb | 2003-08-18 23:52:09 +0000 | [diff] [blame] | 83 | elif o == '-s': |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 84 | bufsize = int(a) |
| 85 | if not args: |
| 86 | args = ['-'] |
| 87 | return sum(args, out) |
| 88 | |
| 89 | if __name__ == '__main__' or __name__ == sys.argv[0]: |
| 90 | sys.exit(main(sys.argv[1:], sys.stdout)) |