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 |
| 9 | rmode = 'r' |
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 ...] |
| 13 | -b : read files in binary mode |
| 14 | -t : read files in text mode (default) |
| 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 | |
| 43 | def printsum(filename, out = sys.stdout): |
| 44 | try: |
| 45 | fp = open(filename, rmode) |
| 46 | except IOError, msg: |
| 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 | |
| 55 | def printsumfp(fp, filename, out = sys.stdout): |
| 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) |
| 60 | if not data: break |
| 61 | m.update(data) |
| 62 | except IOError, msg: |
| 63 | sys.stderr.write('%s: I/O error: %s\n' % (filename, msg)) |
| 64 | return 1 |
| 65 | out.write('%s %s\n' % (m.hexdigest(), filename)) |
| 66 | return 0 |
Guido van Rossum | 9966e2c | 2001-06-22 16:05:48 +0000 | [diff] [blame] | 67 | |
Andrew M. Kuchling | 4693d46 | 2003-05-13 17:39:26 +0000 | [diff] [blame] | 68 | def main(args = sys.argv[1:], out = sys.stdout): |
| 69 | global fnfilter, rmode, bufsize |
| 70 | try: |
| 71 | opts, args = getopt.getopt(args, 'blts:') |
| 72 | except getopt.error, msg: |
| 73 | sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage)) |
| 74 | return 2 |
| 75 | for o, a in opts: |
| 76 | if o == '-l': |
| 77 | fnfilter = os.path.basename |
| 78 | if o == '-b': |
| 79 | rmode = 'rb' |
| 80 | if o == '-t': |
| 81 | rmode = 'r' |
| 82 | if o == '-s': |
| 83 | bufsize = int(a) |
| 84 | if not args: |
| 85 | args = ['-'] |
| 86 | return sum(args, out) |
| 87 | |
| 88 | if __name__ == '__main__' or __name__ == sys.argv[0]: |
| 89 | sys.exit(main(sys.argv[1:], sys.stdout)) |