Further clean up ddf generation.
[htsworkflow.git] / htsworkflow / util / hashfile.py
1 """Utility to make md5sums of a file caching as a parallel file
2 """
3 import logging
4 import os
5 from subprocess import Popen, PIPE
6
7 logger = logging.getLogger(__name__)
8
9 def make_md5sum(filename):
10     """Quickly find the md5sum of a file
11     """
12     md5_cache = os.path.join(filename+".md5")
13     print md5_cache
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)
19     else:
20         md5sum = make_md5sum_unix(filename, md5_cache)
21     return md5sum
22     
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()
28     retcode = p.wait()
29     logger.debug("Finished {0} retcode {1}".format(" ".join(cmd), retcode))
30     if retcode != 0:
31         logger.error("Trouble with md5sum for {0}".format(filename))
32         return None
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")
38         stream.write(stdin)
39         stream.close()
40     return md5sum
41
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))
47         return None
48     return md5sum
49