Chromium Code Reviews| 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 difflib |
| 6 import hashlib | 6 import hashlib |
| 7 import os | 7 import os |
| 8 import re | 8 import re |
| 9 import sys | 9 import sys |
| 10 | 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 | 11 |
| 18 # When set and a difference is detected, a diff of what changed is printed. | 12 # 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)) | 13 _PRINT_MD5_DIFFS = int(os.environ.get('PRINT_MD5_DIFFS', 0)) |
| 20 | 14 |
| 21 # Used to strip off temp dir prefix. | 15 # Used to strip off temp dir prefix. |
| 22 _TEMP_DIR_PATTERN = re.compile(r'^/tmp/.*?/') | 16 _TEMP_DIR_PATTERN = re.compile(r'^/tmp/.*?/') |
| 23 | 17 |
| 24 | 18 |
| 25 def CallAndRecordIfStale( | 19 def CallAndRecordIfStale( |
| 26 function, record_path=None, input_paths=None, input_strings=None, | 20 function, record_path=None, input_paths=None, input_strings=None, |
| 27 force=False): | 21 output_paths=None, force=False): |
| 28 """Calls function if the md5sum of the input paths/strings has changed. | 22 """Calls function if outputs are stale. |
| 29 | 23 |
| 30 The md5sum of the inputs is compared with the one stored in record_path. If | 24 Outputs are considered stale if: |
| 31 this has changed (or the record doesn't exist), function will be called and | 25 - any output_paths are missing, or |
| 32 the new md5sum will be recorded. | 26 - the contents of any file within input_paths has changed, or |
| 27 - the contents of input_strings has changed. | |
| 33 | 28 |
| 34 If force is True, the function will be called regardless of whether the | 29 To debug which files are out-of-date, set the environment variable: |
| 35 md5sum is out of date. | 30 PRINT_MD5_DIFFS=1 |
| 31 | |
| 32 Args: | |
| 33 function: The function to call. | |
| 34 record_path: Path to record metadata. | |
| 35 Defaults to output_paths[0] + '.md5.stamp' | |
| 36 input_paths: List of paths to calcualte an md5 sum on. | |
| 37 input_strings: List of strings to record verbatim. | |
| 38 output_paths: List of output paths. | |
| 39 force: When True, function is always called. | |
| 36 """ | 40 """ |
| 37 if not input_paths: | 41 input_paths = input_paths or [] |
| 38 input_paths = [] | 42 input_strings = input_strings or [] |
| 39 if not input_strings: | 43 output_paths = output_paths or [] |
| 40 input_strings = [] | 44 record_path = record_path or output_paths[0] + '.md5.stamp' |
|
jbudorick
2015/09/24 15:16:48
This looks like it doesn't work with the default p
agrieve
2015/09/24 15:40:44
Right. I want an exception to be thrown in this ca
jbudorick
2015/09/24 15:42:57
Yes, an assert makes it clear that it's intentiona
| |
| 41 md5_checker = _Md5Checker( | 45 md5_checker = _Md5Checker( |
| 42 record_path=record_path, | 46 record_path=record_path, |
| 43 input_paths=input_paths, | 47 input_paths=input_paths, |
| 44 input_strings=input_strings) | 48 input_strings=input_strings) |
| 45 | 49 |
| 50 missing_outputs = [x for x in output_paths if not os.path.exists(x)] | |
| 46 is_stale = md5_checker.old_digest != md5_checker.new_digest | 51 is_stale = md5_checker.old_digest != md5_checker.new_digest |
| 47 if force or is_stale: | 52 |
| 48 if is_stale and _PRINT_MD5_DIFFS: | 53 if force or missing_outputs or is_stale: |
| 49 print '%sDifference found in %s:%s' % ( | 54 if _PRINT_MD5_DIFFS: |
| 50 colorama.Fore.YELLOW, record_path, colorama.Fore.RESET) | 55 print '=' * 80 |
| 51 print md5_checker.DescribeDifference() | 56 print 'Difference found in %s:' % record_path |
| 57 if missing_outputs: | |
| 58 print 'Outputs do not exist:\n' + '\n'.join(missing_outputs) | |
| 59 elif force: | |
| 60 print 'force=True' | |
| 61 else: | |
| 62 print md5_checker.DescribeDifference() | |
| 63 print '=' * 80 | |
| 52 function() | 64 function() |
| 53 md5_checker.Write() | 65 md5_checker.Write() |
| 54 | 66 |
| 55 | 67 |
| 56 def _UpdateMd5ForFile(md5, path, block_size=2**16): | 68 def _UpdateMd5ForFile(md5, path, block_size=2**16): |
| 57 with open(path, 'rb') as infile: | 69 with open(path, 'rb') as infile: |
| 58 while True: | 70 while True: |
| 59 data = infile.read(block_size) | 71 data = infile.read(block_size) |
| 60 if not data: | 72 if not data: |
| 61 break | 73 break |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 extended_info = [] | 111 extended_info = [] |
| 100 outer_md5 = hashlib.md5() | 112 outer_md5 = hashlib.md5() |
| 101 for i in sorted(input_paths): | 113 for i in sorted(input_paths): |
| 102 inner_md5 = hashlib.md5() | 114 inner_md5 = hashlib.md5() |
| 103 _UpdateMd5ForPath(inner_md5, i) | 115 _UpdateMd5ForPath(inner_md5, i) |
| 104 i = _TrimPathPrefix(i) | 116 i = _TrimPathPrefix(i) |
| 105 extended_info.append(i + '=' + inner_md5.hexdigest()) | 117 extended_info.append(i + '=' + inner_md5.hexdigest()) |
| 106 # Include the digest in the overall diff, but not the path | 118 # Include the digest in the overall diff, but not the path |
| 107 outer_md5.update(inner_md5.hexdigest()) | 119 outer_md5.update(inner_md5.hexdigest()) |
| 108 | 120 |
| 109 for s in input_strings: | 121 for s in map(str, input_strings): |
|
jbudorick
2015/09/24 15:16:48
nit: we tend to avoid map, filter, etc in favor of
agrieve
2015/09/24 15:40:44
Agree that reads better :) Done.
| |
| 110 outer_md5.update(s) | 122 outer_md5.update(s) |
| 111 extended_info.append(s) | 123 extended_info.append(s) |
| 112 | 124 |
| 113 self.new_digest = outer_md5.hexdigest() | 125 self.new_digest = outer_md5.hexdigest() |
| 114 self.new_extended_info = extended_info | 126 self.new_extended_info = extended_info |
| 115 | 127 |
| 116 self.old_digest = '' | 128 self.old_digest = '' |
| 117 self.old_extended_info = [] | 129 self.old_extended_info = [] |
| 118 if os.path.exists(self.record_path): | 130 if os.path.exists(self.record_path): |
| 119 with open(self.record_path, 'r') as old_record: | 131 with open(self.record_path, 'r') as old_record: |
| 120 self.old_extended_info = [line.strip() for line in old_record] | 132 self.old_extended_info = [line.strip() for line in old_record] |
| 121 self.old_digest = self.old_extended_info.pop(0) | 133 if self.old_extended_info: |
| 134 self.old_digest = self.old_extended_info.pop(0) | |
| 122 | 135 |
| 123 def Write(self): | 136 def Write(self): |
| 124 with open(self.record_path, 'w') as new_record: | 137 with open(self.record_path, 'w') as new_record: |
| 125 new_record.write(self.new_digest) | 138 new_record.write(self.new_digest) |
| 126 new_record.write('\n' + '\n'.join(self.new_extended_info) + '\n') | 139 new_record.write('\n' + '\n'.join(self.new_extended_info) + '\n') |
| 127 | 140 |
| 128 def DescribeDifference(self): | 141 def DescribeDifference(self): |
| 129 if self.old_digest == self.new_digest: | 142 if self.old_digest == self.new_digest: |
| 130 return "There's no difference." | 143 return "There's no difference." |
| 131 if not self.old_digest: | 144 if not self.old_digest: |
| 132 return 'Previous stamp file not found.' | 145 return 'Previous stamp file not found.' |
| 133 if not self.old_extended_info: | 146 if not self.old_extended_info: |
| 134 return 'Previous stamp file lacks extended info.' | 147 return 'Previous stamp file lacks extended info.' |
| 135 diff = difflib.unified_diff(self.old_extended_info, self.new_extended_info) | 148 diff = difflib.unified_diff(self.old_extended_info, self.new_extended_info) |
| 136 return '\n'.join(diff) | 149 return '\n'.join(diff) |
| OLD | NEW |