blob: 140c0b3e1aaabe2b4b6b6ccfca8d4ed110836a3e [file] [log] [blame]
Guido van Rossum9966e2c2001-06-22 16:05:48 +00001#! /usr/bin/env python
2
3"""Python utility to print MD5 checksums of argument files.
Guido van Rossum9966e2c2001-06-22 16:05:48 +00004"""
5
Guido van Rossum9966e2c2001-06-22 16:05:48 +00006
Andrew M. Kuchling4693d462003-05-13 17:39:26 +00007bufsize = 8096
8fnfilter = None
Tim Peters1a3abcb2003-08-18 23:52:09 +00009rmode = 'rb'
Guido van Rossum9966e2c2001-06-22 16:05:48 +000010
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000011usage = """
12usage: sum5 [-b] [-t] [-l] [-s bufsize] [file ...]
Tim Peters1a3abcb2003-08-18 23:52:09 +000013-b : read files in binary mode (default)
14-t : read files in text mode (you almost certainly don't want this!)
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000015-l : print last pathname component only
16-s bufsize: read buffer size (default %d)
17file ... : files to sum; '-' or no files means stdin
18""" % bufsize
19
20import sys
21import os
22import getopt
23import md5
24
25def 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 Peters1a3abcb2003-08-18 23:52:09 +000043def printsum(filename, out=sys.stdout):
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000044 try:
45 fp = open(filename, rmode)
Guido van Rossumb940e112007-01-10 16:19:56 +000046 except IOError as msg:
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000047 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 Peters1a3abcb2003-08-18 23:52:09 +000055def printsumfp(fp, filename, out=sys.stdout):
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000056 m = md5.new()
57 try:
Guido van Rossum9966e2c2001-06-22 16:05:48 +000058 while 1:
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000059 data = fp.read(bufsize)
Tim Peters1a3abcb2003-08-18 23:52:09 +000060 if not data:
61 break
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000062 m.update(data)
Guido van Rossumb940e112007-01-10 16:19:56 +000063 except IOError as msg:
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000064 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 Rossum9966e2c2001-06-22 16:05:48 +000068
Tim Peters1a3abcb2003-08-18 23:52:09 +000069def main(args = sys.argv[1:], out=sys.stdout):
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000070 global fnfilter, rmode, bufsize
71 try:
72 opts, args = getopt.getopt(args, 'blts:')
Guido van Rossumb940e112007-01-10 16:19:56 +000073 except getopt.error as msg:
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000074 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 Peters1a3abcb2003-08-18 23:52:09 +000079 elif o == '-b':
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000080 rmode = 'rb'
Tim Peters1a3abcb2003-08-18 23:52:09 +000081 elif o == '-t':
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000082 rmode = 'r'
Tim Peters1a3abcb2003-08-18 23:52:09 +000083 elif o == '-s':
Andrew M. Kuchling4693d462003-05-13 17:39:26 +000084 bufsize = int(a)
85 if not args:
86 args = ['-']
87 return sum(args, out)
88
89if __name__ == '__main__' or __name__ == sys.argv[0]:
90 sys.exit(main(sys.argv[1:], sys.stdout))