Ignore the path when comparing filenames from the md5 files.
[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     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)
18     else:
19         md5sum = make_md5sum_unix(filename, md5_cache)
20     return md5sum
21     
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()
27     retcode = p.wait()
28     logger.debug("Finished {0} retcode {1}".format(" ".join(cmd), retcode))
29     if retcode != 0:
30         logger.error("Trouble with md5sum for {0}".format(filename))
31         return None
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")
37         stream.write(stdin)
38         stream.close()
39     return md5sum
40
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))
48         return None
49     return md5sum
50