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