1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import hashlib
6import os
7
8
9def CallAndRecordIfStale(
10    function, record_path=None, input_paths=None, input_strings=None,
11    force=False):
12  """Calls function if the md5sum of the input paths/strings has changed.
13
14  The md5sum of the inputs is compared with the one stored in record_path. If
15  this has changed (or the record doesn't exist), function will be called and
16  the new md5sum will be recorded.
17
18  If force is True, the function will be called regardless of whether the
19  md5sum is out of date.
20  """
21  if not input_paths:
22    input_paths = []
23  if not input_strings:
24    input_strings = []
25  md5_checker = _Md5Checker(
26      record_path=record_path,
27      input_paths=input_paths,
28      input_strings=input_strings)
29  if force or md5_checker.IsStale():
30    function()
31    md5_checker.Write()
32
33
34def _UpdateMd5ForFile(md5, path, block_size=2**16):
35  with open(path, 'rb') as infile:
36    while True:
37      data = infile.read(block_size)
38      if not data:
39        break
40      md5.update(data)
41
42
43def _UpdateMd5ForDirectory(md5, dir_path):
44  for root, _, files in os.walk(dir_path):
45    for f in files:
46      _UpdateMd5ForFile(md5, os.path.join(root, f))
47
48
49def _UpdateMd5ForPath(md5, path):
50  if os.path.isdir(path):
51    _UpdateMd5ForDirectory(md5, path)
52  else:
53    _UpdateMd5ForFile(md5, path)
54
55
56class _Md5Checker(object):
57  def __init__(self, record_path=None, input_paths=None, input_strings=None):
58    if not input_paths:
59      input_paths = []
60    if not input_strings:
61      input_strings = []
62
63    assert record_path.endswith('.stamp'), (
64        'record paths must end in \'.stamp\' so that they are easy to find '
65        'and delete')
66
67    self.record_path = record_path
68
69    md5 = hashlib.md5()
70    for i in sorted(input_paths):
71      _UpdateMd5ForPath(md5, i)
72    for s in input_strings:
73      md5.update(s)
74    self.new_digest = md5.hexdigest()
75
76    self.old_digest = ''
77    if os.path.exists(self.record_path):
78      with open(self.record_path, 'r') as old_record:
79        self.old_digest = old_record.read()
80
81  def IsStale(self):
82    return self.old_digest != self.new_digest
83
84  def Write(self):
85    with open(self.record_path, 'w') as new_record:
86      new_record.write(self.new_digest)
87