1 """This module contains helper functions for working with checksums."""
2
3 import hashlib
4 from .output import instant_assert, instant_debug, instant_error
5
7 """
8 Get the checksum value of filename
9 modified based on Python24\Tools\Scripts\md5.py
10 """
11 instant_assert(isinstance(text, str), "Expecting string.")
12 instant_assert(isinstance(filenames, (list,tuple)), "Expecting sequence.")
13
14 m = hashlib.new('sha1')
15 if text:
16 m.update(text)
17
18 for filename in sorted(filenames):
19 instant_debug("Adding file '%s' to checksum." % filename)
20 try:
21 fp = open(filename, 'rb')
22 except IOError as e:
23 instant_error("Can't open file '%s': %s" % (filename, e))
24
25 try:
26 while 1:
27 data = fp.read()
28 if not data:
29 break
30 m.update(data)
31 except IOError as e:
32 instant_error("I/O error reading '%s': %s" % (filename, e))
33
34 fp.close()
35
36 return m.hexdigest().lower()
37
38
40 signature = "(Test signature)"
41 files = ["signatures.py", "__init__.py"]
42 print()
43 print("Signature:", repr(signature))
44 print("Checksum:", compute_checksum(signature, []))
45 print()
46 print("Files:", files)
47 print("Checksum:", compute_checksum("", files))
48 print()
49
50 if __name__ == "__main__":
51 _test()
52