1#! /usr/bin/env python
2# encoding: utf-8
3
4"""
5Re-calculate md5 hashes of files only when the file times or the file
6size have changed.
7
8The hashes can also reflect either the file contents (STRONGEST=True) or the
9file time and file size.
10
11The performance benefits of this module are usually insignificant.
12"""
13
14import os, stat
15from waflib import Utils, Build, Node
16
17STRONGEST = True
18
19Build.SAVED_ATTRS.append('hashes_md5_tstamp')
20def h_file(self):
21	filename = self.abspath()
22	st = os.stat(filename)
23
24	cache = self.ctx.hashes_md5_tstamp
25	if filename in cache and cache[filename][0] == st.st_mtime:
26		return cache[filename][1]
27
28	if STRONGEST:
29		ret = Utils.h_file(filename)
30	else:
31		if stat.S_ISDIR(st[stat.ST_MODE]):
32			raise IOError('Not a file')
33		ret = Utils.md5(str((st.st_mtime, st.st_size)).encode()).digest()
34
35	cache[filename] = (st.st_mtime, ret)
36	return ret
37h_file.__doc__ = Node.Node.h_file.__doc__
38Node.Node.h_file = h_file
39
40