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.
11 from util
import build_utils
13 if build_utils
.COLORAMA_ROOT
not in sys
.path
:
14 sys
.path
.append(build_utils
.COLORAMA_ROOT
)
18 # When set and a difference is detected, a diff of what changed is printed.
19 _PRINT_MD5_DIFFS
= int(os
.environ
.get('PRINT_MD5_DIFFS', 0))
21 # Used to strip off temp dir prefix.
22 _TEMP_DIR_PATTERN
= re
.compile(r
'^/tmp/.*?/')
25 def CallAndRecordIfStale(
26 function
, record_path
=None, input_paths
=None, input_strings
=None,
28 """Calls function if the md5sum of the input paths/strings has changed.
30 The md5sum of the inputs is compared with the one stored in record_path. If
31 this has changed (or the record doesn't exist), function will be called and
32 the new md5sum will be recorded.
34 If force is True, the function will be called regardless of whether the
35 md5sum is out of date.
41 md5_checker
= _Md5Checker(
42 record_path
=record_path
,
43 input_paths
=input_paths
,
44 input_strings
=input_strings
)
46 is_stale
= md5_checker
.old_digest
!= md5_checker
.new_digest
48 if is_stale
and _PRINT_MD5_DIFFS
:
49 print '%sDifference found in %s:%s' % (
50 colorama
.Fore
.YELLOW
, record_path
, colorama
.Fore
.RESET
)
51 print md5_checker
.DescribeDifference()
56 def _UpdateMd5ForFile(md5
, path
, block_size
=2**16):
57 with
open(path
, 'rb') as infile
:
59 data
= infile
.read(block_size
)
65 def _UpdateMd5ForDirectory(md5
, dir_path
):
66 for root
, _
, files
in os
.walk(dir_path
):
68 _UpdateMd5ForFile(md5
, os
.path
.join(root
, f
))
71 def _UpdateMd5ForPath(md5
, path
):
72 if os
.path
.isdir(path
):
73 _UpdateMd5ForDirectory(md5
, path
)
75 _UpdateMd5ForFile(md5
, path
)
78 def _TrimPathPrefix(path
):
79 """Attempts to remove temp dir prefix from the path.
81 Use this only for extended_info (not for the actual md5).
83 return _TEMP_DIR_PATTERN
.sub('{TMP}', path
)
86 class _Md5Checker(object):
87 def __init__(self
, record_path
=None, input_paths
=None, input_strings
=None):
93 assert record_path
.endswith('.stamp'), (
94 'record paths must end in \'.stamp\' so that they are easy to find '
97 self
.record_path
= record_path
100 outer_md5
= hashlib
.md5()
101 for i
in sorted(input_paths
):
102 inner_md5
= hashlib
.md5()
103 _UpdateMd5ForPath(inner_md5
, i
)
104 i
= _TrimPathPrefix(i
)
105 extended_info
.append(i
+ '=' + inner_md5
.hexdigest())
106 # Include the digest in the overall diff, but not the path
107 outer_md5
.update(inner_md5
.hexdigest())
109 for s
in input_strings
:
111 extended_info
.append(s
)
113 self
.new_digest
= outer_md5
.hexdigest()
114 self
.new_extended_info
= extended_info
117 self
.old_extended_info
= []
118 if os
.path
.exists(self
.record_path
):
119 with
open(self
.record_path
, 'r') as old_record
:
120 self
.old_extended_info
= [line
.strip() for line
in old_record
]
121 self
.old_digest
= self
.old_extended_info
.pop(0)
124 with
open(self
.record_path
, 'w') as new_record
:
125 new_record
.write(self
.new_digest
)
126 new_record
.write('\n' + '\n'.join(self
.new_extended_info
) + '\n')
128 def DescribeDifference(self
):
129 if self
.old_digest
== self
.new_digest
:
130 return "There's no difference."
131 if not self
.old_digest
:
132 return 'Previous stamp file not found.'
133 if not self
.old_extended_info
:
134 return 'Previous stamp file lacks extended info.'
135 diff
= difflib
.unified_diff(self
.old_extended_info
, self
.new_extended_info
)
136 return '\n'.join(diff
)