1996-11-27 15:52:01 -04:00
|
|
|
#! /usr/bin/env python
|
1994-10-03 13:45:35 -03:00
|
|
|
|
|
|
|
# print md5 checksum for files
|
|
|
|
|
|
|
|
bufsize = 8096
|
|
|
|
fnfilter = None
|
|
|
|
rmode = 'r'
|
|
|
|
|
|
|
|
usage = """
|
|
|
|
usage: sum5 [-b] [-t] [-l] [-s bufsize] [file ...]
|
|
|
|
-b : read files in binary mode
|
|
|
|
-t : read files in text mode (default)
|
|
|
|
-l : print last pathname component only
|
|
|
|
-s bufsize: read buffer size (default %d)
|
|
|
|
file ... : files to sum; '-' or no files means stdin
|
|
|
|
""" % bufsize
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
import os
|
|
|
|
import md5
|
|
|
|
import regsub
|
|
|
|
|
|
|
|
StringType = type('')
|
|
|
|
FileType = type(sys.stdin)
|
|
|
|
|
|
|
|
def sum(*files):
|
2001-01-17 04:48:39 -04:00
|
|
|
sts = 0
|
|
|
|
if files and type(files[-1]) == FileType:
|
|
|
|
out, files = files[-1], files[:-1]
|
|
|
|
else:
|
|
|
|
out = sys.stdout
|
|
|
|
if len(files) == 1 and type(files[0]) != StringType:
|
|
|
|
files = files[0]
|
|
|
|
for f in files:
|
|
|
|
if type(f) == StringType:
|
|
|
|
if f == '-':
|
|
|
|
sts = printsumfp(sys.stdin, '<stdin>', out) or sts
|
|
|
|
else:
|
|
|
|
sts = printsum(f, out) or sts
|
|
|
|
else:
|
|
|
|
sts = sum(f, out) or sts
|
|
|
|
return sts
|
1994-10-03 13:45:35 -03:00
|
|
|
|
|
|
|
def printsum(file, out = sys.stdout):
|
2001-01-17 04:48:39 -04:00
|
|
|
try:
|
|
|
|
fp = open(file, rmode)
|
|
|
|
except IOError, msg:
|
|
|
|
sys.stderr.write('%s: Can\'t open: %s\n' % (file, msg))
|
|
|
|
return 1
|
|
|
|
if fnfilter:
|
|
|
|
file = fnfilter(file)
|
|
|
|
sts = printsumfp(fp, file, out)
|
|
|
|
fp.close()
|
|
|
|
return sts
|
1994-10-03 13:45:35 -03:00
|
|
|
|
|
|
|
def printsumfp(fp, file, out = sys.stdout):
|
2001-01-17 04:48:39 -04:00
|
|
|
m = md5.md5()
|
|
|
|
try:
|
|
|
|
while 1:
|
|
|
|
data = fp.read(bufsize)
|
|
|
|
if not data: break
|
|
|
|
m.update(data)
|
|
|
|
except IOError, msg:
|
|
|
|
sys.stderr.write('%s: I/O error: %s\n' % (file, msg))
|
|
|
|
return 1
|
|
|
|
out.write('%s %s\n' % (hexify(m.digest()), file))
|
|
|
|
return 0
|
1994-10-03 13:45:35 -03:00
|
|
|
|
|
|
|
def hexify(s):
|
2001-01-17 04:48:39 -04:00
|
|
|
res = ''
|
|
|
|
for c in s:
|
|
|
|
res = res + '%02x' % ord(c)
|
|
|
|
return res
|
1994-10-03 13:45:35 -03:00
|
|
|
|
|
|
|
def main(args = sys.argv[1:], out = sys.stdout):
|
2001-01-17 04:48:39 -04:00
|
|
|
global fnfilter, rmode, bufsize
|
|
|
|
import getopt
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(args, 'blts:')
|
|
|
|
except getopt.error, msg:
|
|
|
|
sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage))
|
|
|
|
return 2
|
|
|
|
for o, a in opts:
|
|
|
|
if o == '-l':
|
|
|
|
fnfilter = os.path.basename
|
|
|
|
if o == '-b':
|
|
|
|
rmode = 'rb'
|
|
|
|
if o == '-t':
|
|
|
|
rmode = 'r'
|
|
|
|
if o == '-s':
|
|
|
|
bufsize = string.atoi(a)
|
|
|
|
if not args: args = ['-']
|
|
|
|
return sum(args, out)
|
1994-10-03 13:45:35 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__' or __name__ == sys.argv[0]:
|
2001-01-17 04:48:39 -04:00
|
|
|
sys.exit(main(sys.argv[1:], sys.stdout))
|