Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 2 | |
| 3 | # print md5 checksum for files |
| 4 | |
| 5 | bufsize = 8096 |
| 6 | fnfilter = None |
| 7 | rmode = 'r' |
| 8 | |
| 9 | usage = """ |
| 10 | usage: sum5 [-b] [-t] [-l] [-s bufsize] [file ...] |
| 11 | -b : read files in binary mode |
| 12 | -t : read files in text mode (default) |
| 13 | -l : print last pathname component only |
| 14 | -s bufsize: read buffer size (default %d) |
| 15 | file ... : files to sum; '-' or no files means stdin |
| 16 | """ % bufsize |
| 17 | |
| 18 | import sys |
| 19 | import string |
| 20 | import os |
| 21 | import md5 |
| 22 | import regsub |
| 23 | |
| 24 | StringType = type('') |
| 25 | FileType = type(sys.stdin) |
| 26 | |
| 27 | def sum(*files): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 28 | sts = 0 |
| 29 | if files and type(files[-1]) == FileType: |
| 30 | out, files = files[-1], files[:-1] |
| 31 | else: |
| 32 | out = sys.stdout |
| 33 | if len(files) == 1 and type(files[0]) != StringType: |
| 34 | files = files[0] |
| 35 | for f in files: |
| 36 | if type(f) == StringType: |
| 37 | if f == '-': |
| 38 | sts = printsumfp(sys.stdin, '<stdin>', out) or sts |
| 39 | else: |
| 40 | sts = printsum(f, out) or sts |
| 41 | else: |
| 42 | sts = sum(f, out) or sts |
| 43 | return sts |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 44 | |
| 45 | def printsum(file, out = sys.stdout): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 46 | try: |
| 47 | fp = open(file, rmode) |
| 48 | except IOError, msg: |
| 49 | sys.stderr.write('%s: Can\'t open: %s\n' % (file, msg)) |
| 50 | return 1 |
| 51 | if fnfilter: |
| 52 | file = fnfilter(file) |
| 53 | sts = printsumfp(fp, file, out) |
| 54 | fp.close() |
| 55 | return sts |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 56 | |
| 57 | def printsumfp(fp, file, out = sys.stdout): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 58 | m = md5.md5() |
| 59 | try: |
| 60 | while 1: |
| 61 | data = fp.read(bufsize) |
| 62 | if not data: break |
| 63 | m.update(data) |
| 64 | except IOError, msg: |
| 65 | sys.stderr.write('%s: I/O error: %s\n' % (file, msg)) |
| 66 | return 1 |
| 67 | out.write('%s %s\n' % (hexify(m.digest()), file)) |
| 68 | return 0 |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 69 | |
| 70 | def hexify(s): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 71 | res = '' |
| 72 | for c in s: |
| 73 | res = res + '%02x' % ord(c) |
| 74 | return res |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 75 | |
| 76 | def main(args = sys.argv[1:], out = sys.stdout): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 77 | global fnfilter, rmode, bufsize |
| 78 | import getopt |
| 79 | try: |
| 80 | opts, args = getopt.getopt(args, 'blts:') |
| 81 | except getopt.error, msg: |
| 82 | sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage)) |
| 83 | return 2 |
| 84 | for o, a in opts: |
| 85 | if o == '-l': |
| 86 | fnfilter = os.path.basename |
| 87 | if o == '-b': |
| 88 | rmode = 'rb' |
| 89 | if o == '-t': |
| 90 | rmode = 'r' |
| 91 | if o == '-s': |
| 92 | bufsize = string.atoi(a) |
| 93 | if not args: args = ['-'] |
| 94 | return sum(args, out) |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 95 | |
| 96 | if __name__ == '__main__' or __name__ == sys.argv[0]: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 97 | sys.exit(main(sys.argv[1:], sys.stdout)) |