Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(729)

Side by Side Diff: build/android/gyp/util/md5_check.py

Issue 1356873003: CallAndRecordIfStale(): Add knownledge of output_files, depfiles, stamp (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@incremental-script-dep
Patch Set: add assert and remove map() Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/gyp/util/build_utils.py ('k') | build/android/gyp/util/md5_check_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 assert record_path or output_paths
38 input_paths = [] 42 input_paths = input_paths or []
39 if not input_strings: 43 input_strings = input_strings or []
40 input_strings = [] 44 output_paths = output_paths or []
45 record_path = record_path or output_paths[0] + '.md5.stamp'
41 md5_checker = _Md5Checker( 46 md5_checker = _Md5Checker(
42 record_path=record_path, 47 record_path=record_path,
43 input_paths=input_paths, 48 input_paths=input_paths,
44 input_strings=input_strings) 49 input_strings=input_strings)
45 50
51 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 52 is_stale = md5_checker.old_digest != md5_checker.new_digest
47 if force or is_stale: 53
48 if is_stale and _PRINT_MD5_DIFFS: 54 if force or missing_outputs or is_stale:
49 print '%sDifference found in %s:%s' % ( 55 if _PRINT_MD5_DIFFS:
50 colorama.Fore.YELLOW, record_path, colorama.Fore.RESET) 56 print '=' * 80
51 print md5_checker.DescribeDifference() 57 print 'Difference found in %s:' % record_path
58 if missing_outputs:
59 print 'Outputs do not exist:\n' + '\n'.join(missing_outputs)
60 elif force:
61 print 'force=True'
62 else:
63 print md5_checker.DescribeDifference()
64 print '=' * 80
52 function() 65 function()
53 md5_checker.Write() 66 md5_checker.Write()
54 67
55 68
56 def _UpdateMd5ForFile(md5, path, block_size=2**16): 69 def _UpdateMd5ForFile(md5, path, block_size=2**16):
57 with open(path, 'rb') as infile: 70 with open(path, 'rb') as infile:
58 while True: 71 while True:
59 data = infile.read(block_size) 72 data = infile.read(block_size)
60 if not data: 73 if not data:
61 break 74 break
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 extended_info = [] 112 extended_info = []
100 outer_md5 = hashlib.md5() 113 outer_md5 = hashlib.md5()
101 for i in sorted(input_paths): 114 for i in sorted(input_paths):
102 inner_md5 = hashlib.md5() 115 inner_md5 = hashlib.md5()
103 _UpdateMd5ForPath(inner_md5, i) 116 _UpdateMd5ForPath(inner_md5, i)
104 i = _TrimPathPrefix(i) 117 i = _TrimPathPrefix(i)
105 extended_info.append(i + '=' + inner_md5.hexdigest()) 118 extended_info.append(i + '=' + inner_md5.hexdigest())
106 # Include the digest in the overall diff, but not the path 119 # Include the digest in the overall diff, but not the path
107 outer_md5.update(inner_md5.hexdigest()) 120 outer_md5.update(inner_md5.hexdigest())
108 121
109 for s in input_strings: 122 for s in (str(s) for s in input_strings):
110 outer_md5.update(s) 123 outer_md5.update(s)
111 extended_info.append(s) 124 extended_info.append(s)
112 125
113 self.new_digest = outer_md5.hexdigest() 126 self.new_digest = outer_md5.hexdigest()
114 self.new_extended_info = extended_info 127 self.new_extended_info = extended_info
115 128
116 self.old_digest = '' 129 self.old_digest = ''
117 self.old_extended_info = [] 130 self.old_extended_info = []
118 if os.path.exists(self.record_path): 131 if os.path.exists(self.record_path):
119 with open(self.record_path, 'r') as old_record: 132 with open(self.record_path, 'r') as old_record:
120 self.old_extended_info = [line.strip() for line in old_record] 133 self.old_extended_info = [line.strip() for line in old_record]
121 self.old_digest = self.old_extended_info.pop(0) 134 if self.old_extended_info:
135 self.old_digest = self.old_extended_info.pop(0)
122 136
123 def Write(self): 137 def Write(self):
124 with open(self.record_path, 'w') as new_record: 138 with open(self.record_path, 'w') as new_record:
125 new_record.write(self.new_digest) 139 new_record.write(self.new_digest)
126 new_record.write('\n' + '\n'.join(self.new_extended_info) + '\n') 140 new_record.write('\n' + '\n'.join(self.new_extended_info) + '\n')
127 141
128 def DescribeDifference(self): 142 def DescribeDifference(self):
129 if self.old_digest == self.new_digest: 143 if self.old_digest == self.new_digest:
130 return "There's no difference." 144 return "There's no difference."
131 if not self.old_digest: 145 if not self.old_digest:
132 return 'Previous stamp file not found.' 146 return 'Previous stamp file not found.'
133 if not self.old_extended_info: 147 if not self.old_extended_info:
134 return 'Previous stamp file lacks extended info.' 148 return 'Previous stamp file lacks extended info.'
135 diff = difflib.unified_diff(self.old_extended_info, self.new_extended_info) 149 diff = difflib.unified_diff(self.old_extended_info, self.new_extended_info)
136 return '\n'.join(diff) 150 return '\n'.join(diff)
OLDNEW
« no previous file with comments | « build/android/gyp/util/build_utils.py ('k') | build/android/gyp/util/md5_check_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698