2010-03-11 18:53:45 -04:00
|
|
|
#! /usr/bin/env python3
|
2001-06-22 13:05:48 -03:00
|
|
|
|
|
|
|
"""Python utility to print MD5 checksums of argument files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2003-05-13 14:39:26 -03:00
|
|
|
bufsize = 8096
|
|
|
|
fnfilter = None
|
2003-08-18 20:52:09 -03:00
|
|
|
rmode = 'rb'
|
2001-06-22 13:05:48 -03:00
|
|
|
|
2003-05-13 14:39:26 -03:00
|
|
|
usage = """
|
2014-07-11 14:42:18 -03:00
|
|
|
usage: md5sum.py [-b] [-t] [-l] [-s bufsize] [file ...]
|
2003-08-18 20:52:09 -03:00
|
|
|
-b : read files in binary mode (default)
|
|
|
|
-t : read files in text mode (you almost certainly don't want this!)
|
2003-05-13 14:39:26 -03:00
|
|
|
-l : print last pathname component only
|
|
|
|
-s bufsize: read buffer size (default %d)
|
|
|
|
file ... : files to sum; '-' or no files means stdin
|
|
|
|
""" % bufsize
|
|
|
|
|
2014-07-11 14:42:18 -03:00
|
|
|
import io
|
2003-05-13 14:39:26 -03:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import getopt
|
2012-04-04 22:28:14 -03:00
|
|
|
from hashlib import md5
|
2003-05-13 14:39:26 -03:00
|
|
|
|
|
|
|
def sum(*files):
|
|
|
|
sts = 0
|
2014-07-11 14:42:18 -03:00
|
|
|
if files and isinstance(files[-1], io.IOBase):
|
2003-05-13 14:39:26 -03:00
|
|
|
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-18 20:52:09 -03:00
|
|
|
def printsum(filename, out=sys.stdout):
|
2003-05-13 14:39:26 -03:00
|
|
|
try:
|
|
|
|
fp = open(filename, rmode)
|
2007-01-10 12:19:56 -04:00
|
|
|
except IOError as msg:
|
2003-05-13 14:39:26 -03: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-18 20:52:09 -03:00
|
|
|
def printsumfp(fp, filename, out=sys.stdout):
|
2014-07-11 14:42:18 -03:00
|
|
|
m = md5()
|
2003-05-13 14:39:26 -03:00
|
|
|
try:
|
2001-06-22 13:05:48 -03:00
|
|
|
while 1:
|
2003-05-13 14:39:26 -03:00
|
|
|
data = fp.read(bufsize)
|
2003-08-18 20:52:09 -03:00
|
|
|
if not data:
|
|
|
|
break
|
2014-07-11 14:42:18 -03:00
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode(fp.encoding)
|
2003-05-13 14:39:26 -03:00
|
|
|
m.update(data)
|
2007-01-10 12:19:56 -04:00
|
|
|
except IOError as msg:
|
2003-05-13 14:39:26 -03: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-18 20:52:09 -03:00
|
|
|
def main(args = sys.argv[1:], out=sys.stdout):
|
2003-05-13 14:39:26 -03:00
|
|
|
global fnfilter, rmode, bufsize
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(args, 'blts:')
|
2007-01-10 12:19:56 -04:00
|
|
|
except getopt.error as msg:
|
2003-05-13 14:39:26 -03: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-18 20:52:09 -03:00
|
|
|
elif o == '-b':
|
2003-05-13 14:39:26 -03:00
|
|
|
rmode = 'rb'
|
2003-08-18 20:52:09 -03:00
|
|
|
elif o == '-t':
|
2003-05-13 14:39:26 -03:00
|
|
|
rmode = 'r'
|
2003-08-18 20:52:09 -03:00
|
|
|
elif o == '-s':
|
2003-05-13 14:39:26 -03: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))
|