Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / build / android / gyp / util / md5_check.py
blob09e86c26b4d12c3ae8e06b4763ef21bde7620b44
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.
5 import difflib
6 import hashlib
7 import os
8 import re
9 import sys
11 from util import build_utils
13 if build_utils.COLORAMA_ROOT not in sys.path:
14 sys.path.append(build_utils.COLORAMA_ROOT)
15 import colorama
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,
27 force=False):
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.
36 """
37 if not input_paths:
38 input_paths = []
39 if not input_strings:
40 input_strings = []
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
47 if force or is_stale:
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()
52 function()
53 md5_checker.Write()
56 def _UpdateMd5ForFile(md5, path, block_size=2**16):
57 with open(path, 'rb') as infile:
58 while True:
59 data = infile.read(block_size)
60 if not data:
61 break
62 md5.update(data)
65 def _UpdateMd5ForDirectory(md5, dir_path):
66 for root, _, files in os.walk(dir_path):
67 for f in files:
68 _UpdateMd5ForFile(md5, os.path.join(root, f))
71 def _UpdateMd5ForPath(md5, path):
72 if os.path.isdir(path):
73 _UpdateMd5ForDirectory(md5, path)
74 else:
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).
82 """
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):
88 if not input_paths:
89 input_paths = []
90 if not input_strings:
91 input_strings = []
93 assert record_path.endswith('.stamp'), (
94 'record paths must end in \'.stamp\' so that they are easy to find '
95 'and delete')
97 self.record_path = record_path
99 extended_info = []
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:
110 outer_md5.update(s)
111 extended_info.append(s)
113 self.new_digest = outer_md5.hexdigest()
114 self.new_extended_info = extended_info
116 self.old_digest = ''
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)
123 def Write(self):
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)