OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
| 5 import difflib |
5 import hashlib | 6 import hashlib |
6 import os | 7 import os |
| 8 import re |
| 9 import sys |
| 10 |
| 11 from util import build_utils |
| 12 |
| 13 if build_utils.COLORAMA_ROOT not in sys.path: |
| 14 sys.path.append(build_utils.COLORAMA_ROOT) |
| 15 import colorama |
| 16 |
| 17 |
| 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)) |
| 20 |
| 21 # Used to strip off temp dir prefix. |
| 22 _TEMP_DIR_PATTERN = re.compile(r'^/tmp/.*?/') |
7 | 23 |
8 | 24 |
9 def CallAndRecordIfStale( | 25 def CallAndRecordIfStale( |
10 function, record_path=None, input_paths=None, input_strings=None, | 26 function, record_path=None, input_paths=None, input_strings=None, |
11 force=False): | 27 force=False): |
12 """Calls function if the md5sum of the input paths/strings has changed. | 28 """Calls function if the md5sum of the input paths/strings has changed. |
13 | 29 |
14 The md5sum of the inputs is compared with the one stored in record_path. If | 30 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 | 31 this has changed (or the record doesn't exist), function will be called and |
16 the new md5sum will be recorded. | 32 the new md5sum will be recorded. |
17 | 33 |
18 If force is True, the function will be called regardless of whether the | 34 If force is True, the function will be called regardless of whether the |
19 md5sum is out of date. | 35 md5sum is out of date. |
20 """ | 36 """ |
21 if not input_paths: | 37 if not input_paths: |
22 input_paths = [] | 38 input_paths = [] |
23 if not input_strings: | 39 if not input_strings: |
24 input_strings = [] | 40 input_strings = [] |
25 md5_checker = _Md5Checker( | 41 md5_checker = _Md5Checker( |
26 record_path=record_path, | 42 record_path=record_path, |
27 input_paths=input_paths, | 43 input_paths=input_paths, |
28 input_strings=input_strings) | 44 input_strings=input_strings) |
29 if force or md5_checker.IsStale(): | 45 |
| 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() |
30 function() | 52 function() |
31 md5_checker.Write() | 53 md5_checker.Write() |
32 | 54 |
33 | 55 |
34 def _UpdateMd5ForFile(md5, path, block_size=2**16): | 56 def _UpdateMd5ForFile(md5, path, block_size=2**16): |
35 with open(path, 'rb') as infile: | 57 with open(path, 'rb') as infile: |
36 while True: | 58 while True: |
37 data = infile.read(block_size) | 59 data = infile.read(block_size) |
38 if not data: | 60 if not data: |
39 break | 61 break |
40 md5.update(data) | 62 md5.update(data) |
41 | 63 |
42 | 64 |
43 def _UpdateMd5ForDirectory(md5, dir_path): | 65 def _UpdateMd5ForDirectory(md5, dir_path): |
44 for root, _, files in os.walk(dir_path): | 66 for root, _, files in os.walk(dir_path): |
45 for f in files: | 67 for f in files: |
46 _UpdateMd5ForFile(md5, os.path.join(root, f)) | 68 _UpdateMd5ForFile(md5, os.path.join(root, f)) |
47 | 69 |
48 | 70 |
49 def _UpdateMd5ForPath(md5, path): | 71 def _UpdateMd5ForPath(md5, path): |
50 if os.path.isdir(path): | 72 if os.path.isdir(path): |
51 _UpdateMd5ForDirectory(md5, path) | 73 _UpdateMd5ForDirectory(md5, path) |
52 else: | 74 else: |
53 _UpdateMd5ForFile(md5, path) | 75 _UpdateMd5ForFile(md5, path) |
54 | 76 |
55 | 77 |
| 78 def _TrimPathPrefix(path): |
| 79 """Attempts to remove temp dir prefix from the path. |
| 80 |
| 81 Use this only for extended_info (not for the actual md5). |
| 82 """ |
| 83 return _TEMP_DIR_PATTERN.sub('{TMP}', path) |
| 84 |
| 85 |
56 class _Md5Checker(object): | 86 class _Md5Checker(object): |
57 def __init__(self, record_path=None, input_paths=None, input_strings=None): | 87 def __init__(self, record_path=None, input_paths=None, input_strings=None): |
58 if not input_paths: | 88 if not input_paths: |
59 input_paths = [] | 89 input_paths = [] |
60 if not input_strings: | 90 if not input_strings: |
61 input_strings = [] | 91 input_strings = [] |
62 | 92 |
63 assert record_path.endswith('.stamp'), ( | 93 assert record_path.endswith('.stamp'), ( |
64 'record paths must end in \'.stamp\' so that they are easy to find ' | 94 'record paths must end in \'.stamp\' so that they are easy to find ' |
65 'and delete') | 95 'and delete') |
66 | 96 |
67 self.record_path = record_path | 97 self.record_path = record_path |
68 | 98 |
69 md5 = hashlib.md5() | 99 extended_info = [] |
| 100 outer_md5 = hashlib.md5() |
70 for i in sorted(input_paths): | 101 for i in sorted(input_paths): |
71 _UpdateMd5ForPath(md5, i) | 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()) |
| 108 |
72 for s in input_strings: | 109 for s in input_strings: |
73 md5.update(s) | 110 outer_md5.update(s) |
74 self.new_digest = md5.hexdigest() | 111 extended_info.append(s) |
| 112 |
| 113 self.new_digest = outer_md5.hexdigest() |
| 114 self.new_extended_info = extended_info |
75 | 115 |
76 self.old_digest = '' | 116 self.old_digest = '' |
| 117 self.old_extended_info = [] |
77 if os.path.exists(self.record_path): | 118 if os.path.exists(self.record_path): |
78 with open(self.record_path, 'r') as old_record: | 119 with open(self.record_path, 'r') as old_record: |
79 self.old_digest = old_record.read() | 120 self.old_extended_info = [line.strip() for line in old_record] |
80 | 121 self.old_digest = self.old_extended_info.pop(0) |
81 def IsStale(self): | |
82 return self.old_digest != self.new_digest | |
83 | 122 |
84 def Write(self): | 123 def Write(self): |
85 with open(self.record_path, 'w') as new_record: | 124 with open(self.record_path, 'w') as new_record: |
86 new_record.write(self.new_digest) | 125 new_record.write(self.new_digest) |
| 126 new_record.write('\n' + '\n'.join(self.new_extended_info) + '\n') |
| 127 |
| 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) |
OLD | NEW |