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")
14 if os.path.exists(md5_cache):
15 logger.debug("Found md5sum in {0}".format(md5_cache))
16 stream = open(md5_cache,'r')
17 lines = stream.readlines()
18 md5sum = parse_md5sum_line(lines, filename)
20 md5sum = make_md5sum_unix(filename, md5_cache)
23 def make_md5sum_unix(filename, md5_cache):
24 cmd = ["md5sum", filename]
25 logger.debug("Running {0}".format(" ".join(cmd)))
26 p = Popen(cmd, stdout=PIPE)
27 stdin, stdout = p.communicate()
29 logger.debug("Finished {0} retcode {1}".format(" ".join(cmd), retcode))
31 logger.error("Trouble with md5sum for {0}".format(filename))
33 lines = stdin.split(os.linesep)
34 md5sum = parse_md5sum_line(lines, filename)
35 if md5sum is not None:
36 logger.debug("Caching sum in {0}".format(md5_cache))
37 stream = open(md5_cache, "w")
42 def parse_md5sum_line(lines, filename):
43 md5sum, md5sum_filename = lines[0].split()
44 if md5sum_filename != filename:
45 errmsg = "MD5sum and I disagre about filename. {0} != {1}"
46 logger.error(errmsg.format(filename, md5sum_filename))