2010-03-11 23:53:45 +01:00
|
|
|
#! /usr/bin/env python3
|
2001-06-22 18:05:48 +02:00
|
|
|
|
|
|
|
"""Python utility to print MD5 checksums of argument files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2003-05-13 19:39:26 +02:00
|
|
|
bufsize = 8096
|
|
|
|
fnfilter = None
|
2003-08-19 01:52:09 +02:00
|
|
|
rmode = 'rb'
|
2001-06-22 18:05:48 +02:00
|
|
|
|
2003-05-13 19:39:26 +02:00
|
|
|
usage = """
|
|
|
|
usage: sum5 [-b] [-t] [-l] [-s bufsize] [file ...]
|
2003-08-19 01:52:09 +02:00
|
|
|
-b : read files in binary mode (default)
|
|
|
|
-t : read files in text mode (you almost certainly don't want this!)
|
2003-05-13 19:39:26 +02:00
|
|
|
-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 os
|
|
|
|
import getopt
|
|
|
|
import md5
|
|
|
|
|
|
|
|
def sum(*files):
|
|
|
|
sts = 0
|
|
|
|
if files and isinstance(files[-1], file):
|
|
|
|
out, files = files[-1], files[:-1]
|
|
|
|
else:
|
|
|
|
out = sys.stdout
|
|
|
|
if len(files) == 1 and not isinstance(files[0], str):
|
|
|
|
files = files[0]
|
|
|
|
for f in files:
|
|
|
|
if isinstance(f, str):
|
|
|
|
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
|
|
|
|
|
2003-08-19 01:52:09 +02:00
|
|
|
def printsum(filename, out=sys.stdout):
|
2003-05-13 19:39:26 +02:00
|
|
|
try:
|
|
|
|
fp = open(filename, rmode)
|
2007-01-10 17:19:56 +01:00
|
|
|
except IOError as msg:
|
2003-05-13 19:39:26 +02:00
|
|
|
sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg))
|
|
|
|
return 1
|
|
|
|
if fnfilter:
|
|
|
|
filename = fnfilter(filename)
|
|
|
|
sts = printsumfp(fp, filename, out)
|
|
|
|
fp.close()
|
|
|
|
return sts
|
|
|
|
|
2003-08-19 01:52:09 +02:00
|
|
|
def printsumfp(fp, filename, out=sys.stdout):
|
2003-05-13 19:39:26 +02:00
|
|
|
m = md5.new()
|
|
|
|
try:
|
2001-06-22 18:05:48 +02:00
|
|
|
while 1:
|
2003-05-13 19:39:26 +02:00
|
|
|
data = fp.read(bufsize)
|
2003-08-19 01:52:09 +02:00
|
|
|
if not data:
|
|
|
|
break
|
2003-05-13 19:39:26 +02:00
|
|
|
m.update(data)
|
2007-01-10 17:19:56 +01:00
|
|
|
except IOError as msg:
|
2003-05-13 19:39:26 +02:00
|
|
|
sys.stderr.write('%s: I/O error: %s\n' % (filename, msg))
|
|
|
|
return 1
|
|
|
|
out.write('%s %s\n' % (m.hexdigest(), filename))
|
|
|
|
return 0
|
|
|
|
|
2003-08-19 01:52:09 +02:00
|
|
|
def main(args = sys.argv[1:], out=sys.stdout):
|
2003-05-13 19:39:26 +02:00
|
|
|
global fnfilter, rmode, bufsize
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(args, 'blts:')
|
2007-01-10 17:19:56 +01:00
|
|
|
except getopt.error as msg:
|
2003-05-13 19:39:26 +02:00
|
|
|
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
|
2003-08-19 01:52:09 +02:00
|
|
|
elif o == '-b':
|
2003-05-13 19:39:26 +02:00
|
|
|
rmode = 'rb'
|
2003-08-19 01:52:09 +02:00
|
|
|
elif o == '-t':
|
2003-05-13 19:39:26 +02:00
|
|
|
rmode = 'r'
|
2003-08-19 01:52:09 +02:00
|
|
|
elif o == '-s':
|
2003-05-13 19:39:26 +02:00
|
|
|
bufsize = int(a)
|
|
|
|
if not args:
|
|
|
|
args = ['-']
|
|
|
|
return sum(args, out)
|
|
|
|
|
|
|
|
if __name__ == '__main__' or __name__ == sys.argv[0]:
|
|
|
|
sys.exit(main(sys.argv[1:], sys.stdout))
|