summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew M. Kuchling <amk@amk.ca>2003-05-13 17:39:26 (GMT)
committerAndrew M. Kuchling <amk@amk.ca>2003-05-13 17:39:26 (GMT)
commit4693d46fefe378312b73c62ed07bb61cf12a83de (patch)
tree86c2a7dddc17cbe5f935244fbd10f065ab085c79
parentfb9653a63f7b7b29060283d4cec79f20a6254d1c (diff)
downloadcpython-4693d46fefe378312b73c62ed07bb61cf12a83de.zip
cpython-4693d46fefe378312b73c62ed07bb61cf12a83de.tar.gz
cpython-4693d46fefe378312b73c62ed07bb61cf12a83de.tar.bz2
Rename sum5.py to md5sum.py, because sum5.py is more interesting
as an example program
-rw-r--r--Tools/scripts/md5sum.py102
-rwxr-xr-xTools/scripts/sum5.py89
2 files changed, 81 insertions, 110 deletions
diff --git a/Tools/scripts/md5sum.py b/Tools/scripts/md5sum.py
index 1bacdf2..0711db8 100644
--- a/Tools/scripts/md5sum.py
+++ b/Tools/scripts/md5sum.py
@@ -1,29 +1,89 @@
#! /usr/bin/env python
"""Python utility to print MD5 checksums of argument files.
-
-Works with Python 2.1 and later.
"""
-import sys, md5
-BLOCKSIZE = 1024*1024
+bufsize = 8096
+fnfilter = None
+rmode = 'r'
-def main():
- args = sys.argv[1:]
- if not args:
- sys.stderr.write("usage: %s file ...\n" % sys.argv[0])
- sys.exit(2)
- for file in sys.argv[1:]:
- f = open(file, "rb")
- sum = md5.new()
+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 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
+
+def printsum(filename, out = sys.stdout):
+ try:
+ fp = open(filename, rmode)
+ except IOError, msg:
+ 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
+
+def printsumfp(fp, filename, out = sys.stdout):
+ m = md5.new()
+ try:
while 1:
- block = f.read(BLOCKSIZE)
- if not block:
- break
- sum.update(block)
- f.close()
- print sum.hexdigest(), file
-
-if __name__ == "__main__":
- main()
+ data = fp.read(bufsize)
+ if not data: break
+ m.update(data)
+ except IOError, msg:
+ sys.stderr.write('%s: I/O error: %s\n' % (filename, msg))
+ return 1
+ out.write('%s %s\n' % (m.hexdigest(), filename))
+ return 0
+
+def main(args = sys.argv[1:], out = sys.stdout):
+ global fnfilter, rmode, bufsize
+ 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 = 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))
diff --git a/Tools/scripts/sum5.py b/Tools/scripts/sum5.py
deleted file mode 100755
index 0711db8..0000000
--- a/Tools/scripts/sum5.py
+++ /dev/null
@@ -1,89 +0,0 @@
-#! /usr/bin/env python
-
-"""Python utility to print MD5 checksums of argument 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 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
-
-def printsum(filename, out = sys.stdout):
- try:
- fp = open(filename, rmode)
- except IOError, msg:
- 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
-
-def printsumfp(fp, filename, out = sys.stdout):
- m = md5.new()
- 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' % (filename, msg))
- return 1
- out.write('%s %s\n' % (m.hexdigest(), filename))
- return 0
-
-def main(args = sys.argv[1:], out = sys.stdout):
- global fnfilter, rmode, bufsize
- 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 = 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))