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.
9 def CallAndRecordIfStale(
10 function
, record_path
=None, input_paths
=None, input_strings
=None,
12 """Calls function if the md5sum of the input paths/strings has changed.
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.
18 If force is True, the function will be called regardless of whether the
19 md5sum is out of date.
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():
34 def _UpdateMd5ForFile(md5
, path
, block_size
=2**16):
35 with
open(path
, 'rb') as infile
:
37 data
= infile
.read(block_size
)
43 def _UpdateMd5ForDirectory(md5
, dir_path
):
44 for root
, _
, files
in os
.walk(dir_path
):
46 _UpdateMd5ForFile(md5
, os
.path
.join(root
, f
))
49 def _UpdateMd5ForPath(md5
, path
):
50 if os
.path
.isdir(path
):
51 _UpdateMd5ForDirectory(md5
, path
)
53 _UpdateMd5ForFile(md5
, path
)
56 class _Md5Checker(object):
57 def __init__(self
, record_path
=None, input_paths
=None, input_strings
=None):
63 assert record_path
.endswith('.stamp'), (
64 'record paths must end in \'.stamp\' so that they are easy to find '
67 self
.record_path
= record_path
70 for i
in sorted(input_paths
):
71 _UpdateMd5ForPath(md5
, i
)
72 for s
in input_strings
:
74 self
.new_digest
= md5
.hexdigest()
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()
82 return self
.old_digest
!= self
.new_digest
85 with
open(self
.record_path
, 'w') as new_record
:
86 new_record
.write(self
.new_digest
)