1 """Utility to make md5sums of a file caching as a parallel file
5 from subprocess import Popen, PIPE
7 logger = logging.getLogger(__name__)
9 def make_md5sum(filename):
10 """Quickly find the md5sum of a file
12 md5_cache = os.path.join(filename+".md5")
13 if os.path.exists(md5_cache):
14 logger.debug("Found md5sum in {0}".format(md5_cache))
15 stream = open(md5_cache,'r')
16 lines = stream.readlines()
17 md5sum = parse_md5sum_line(lines, filename)
19 md5sum = make_md5sum_unix(filename, md5_cache)
22 def make_md5sum_unix(filename, md5_cache):
23 cmd = ["md5sum", filename]
24 logger.debug("Running {0}".format(" ".join(cmd)))
25 p = Popen(cmd, stdout=PIPE)
26 stdin, stdout = p.communicate()
28 logger.debug("Finished {0} retcode {1}".format(" ".join(cmd), retcode))
30 logger.error("Trouble with md5sum for {0}".format(filename))
32 lines = stdin.split(os.linesep)
33 md5sum = parse_md5sum_line(lines, filename)
34 if md5sum is not None:
35 logger.debug("Caching sum in {0}".format(md5_cache))
36 stream = open(md5_cache, "w")
41 def parse_md5sum_line(lines, filename):
42 md5sum, md5sum_filename = lines[0].split()
43 md5sum_filename = os.path.basename(md5sum_filename)
44 filename = os.path.basename(filename)
45 if md5sum_filename != filename:
46 errmsg = "MD5sum and I disagre about filename. {0} != {1}"
47 logger.error(errmsg.format(filename, md5sum_filename))