1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """Python utility to print MD5 checksums of argument files. |
---|
4 | n/a | """ |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | bufsize = 8096 |
---|
8 | n/a | fnfilter = None |
---|
9 | n/a | rmode = 'rb' |
---|
10 | n/a | |
---|
11 | n/a | usage = """ |
---|
12 | n/a | usage: md5sum.py [-b] [-t] [-l] [-s bufsize] [file ...] |
---|
13 | n/a | -b : read files in binary mode (default) |
---|
14 | n/a | -t : read files in text mode (you almost certainly don't want this!) |
---|
15 | n/a | -l : print last pathname component only |
---|
16 | n/a | -s bufsize: read buffer size (default %d) |
---|
17 | n/a | file ... : files to sum; '-' or no files means stdin |
---|
18 | n/a | """ % bufsize |
---|
19 | n/a | |
---|
20 | n/a | import io |
---|
21 | n/a | import sys |
---|
22 | n/a | import os |
---|
23 | n/a | import getopt |
---|
24 | n/a | from hashlib import md5 |
---|
25 | n/a | |
---|
26 | n/a | def sum(*files): |
---|
27 | n/a | sts = 0 |
---|
28 | n/a | if files and isinstance(files[-1], io.IOBase): |
---|
29 | n/a | out, files = files[-1], files[:-1] |
---|
30 | n/a | else: |
---|
31 | n/a | out = sys.stdout |
---|
32 | n/a | if len(files) == 1 and not isinstance(files[0], str): |
---|
33 | n/a | files = files[0] |
---|
34 | n/a | for f in files: |
---|
35 | n/a | if isinstance(f, str): |
---|
36 | n/a | if f == '-': |
---|
37 | n/a | sts = printsumfp(sys.stdin, '<stdin>', out) or sts |
---|
38 | n/a | else: |
---|
39 | n/a | sts = printsum(f, out) or sts |
---|
40 | n/a | else: |
---|
41 | n/a | sts = sum(f, out) or sts |
---|
42 | n/a | return sts |
---|
43 | n/a | |
---|
44 | n/a | def printsum(filename, out=sys.stdout): |
---|
45 | n/a | try: |
---|
46 | n/a | fp = open(filename, rmode) |
---|
47 | n/a | except IOError as msg: |
---|
48 | n/a | sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg)) |
---|
49 | n/a | return 1 |
---|
50 | n/a | if fnfilter: |
---|
51 | n/a | filename = fnfilter(filename) |
---|
52 | n/a | sts = printsumfp(fp, filename, out) |
---|
53 | n/a | fp.close() |
---|
54 | n/a | return sts |
---|
55 | n/a | |
---|
56 | n/a | def printsumfp(fp, filename, out=sys.stdout): |
---|
57 | n/a | m = md5() |
---|
58 | n/a | try: |
---|
59 | n/a | while 1: |
---|
60 | n/a | data = fp.read(bufsize) |
---|
61 | n/a | if not data: |
---|
62 | n/a | break |
---|
63 | n/a | if isinstance(data, str): |
---|
64 | n/a | data = data.encode(fp.encoding) |
---|
65 | n/a | m.update(data) |
---|
66 | n/a | except IOError as msg: |
---|
67 | n/a | sys.stderr.write('%s: I/O error: %s\n' % (filename, msg)) |
---|
68 | n/a | return 1 |
---|
69 | n/a | out.write('%s %s\n' % (m.hexdigest(), filename)) |
---|
70 | n/a | return 0 |
---|
71 | n/a | |
---|
72 | n/a | def main(args = sys.argv[1:], out=sys.stdout): |
---|
73 | n/a | global fnfilter, rmode, bufsize |
---|
74 | n/a | try: |
---|
75 | n/a | opts, args = getopt.getopt(args, 'blts:') |
---|
76 | n/a | except getopt.error as msg: |
---|
77 | n/a | sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage)) |
---|
78 | n/a | return 2 |
---|
79 | n/a | for o, a in opts: |
---|
80 | n/a | if o == '-l': |
---|
81 | n/a | fnfilter = os.path.basename |
---|
82 | n/a | elif o == '-b': |
---|
83 | n/a | rmode = 'rb' |
---|
84 | n/a | elif o == '-t': |
---|
85 | n/a | rmode = 'r' |
---|
86 | n/a | elif o == '-s': |
---|
87 | n/a | bufsize = int(a) |
---|
88 | n/a | if not args: |
---|
89 | n/a | args = ['-'] |
---|
90 | n/a | return sum(args, out) |
---|
91 | n/a | |
---|
92 | n/a | if __name__ == '__main__' or __name__ == sys.argv[0]: |
---|
93 | n/a | sys.exit(main(sys.argv[1:], sys.stdout)) |
---|