Chromium Code Reviews| Index: build/android/gyp/util/md5_check.py |
| diff --git a/build/android/gyp/util/md5_check.py b/build/android/gyp/util/md5_check.py |
| index 47f1ec941544d97c8ec5500013537a0a3ac05691..f80543e8b6466da396467ac6644ef446a1684ecf 100644 |
| --- a/build/android/gyp/util/md5_check.py |
| +++ b/build/android/gyp/util/md5_check.py |
| @@ -6,7 +6,24 @@ import hashlib |
| import os |
| -def UpdateMd5ForFile(md5, path, block_size=2**16): |
| +def CallAndRecordIfStale( |
| + function, record_path=None, input_paths=[], input_strings=[]): |
| + """Calls function if the md5sum of the input paths/strings has changed. |
| + |
| + The md5sum of the inputs is compared with the one stored in record_path. If |
| + this has changed (or the record doesn't exist), function will be called and |
| + the new md5sum will be recorded. |
| + """ |
| + md5_checker = _Md5Checker( |
|
nilesh
2013/04/15 18:28:42
extra space before Md5Checker
cjhopman
2013/04/15 18:32:35
Done.
|
| + record_path=record_path, |
| + input_paths=input_paths, |
| + input_strings=input_strings) |
| + if md5_checker.IsStale(): |
| + function() |
| + md5_checker.Write() |
| + |
| + |
| +def _UpdateMd5ForFile(md5, path, block_size=2**16): |
| with open(path, 'rb') as infile: |
| while True: |
| data = infile.read(block_size) |
| @@ -15,38 +32,43 @@ def UpdateMd5ForFile(md5, path, block_size=2**16): |
| md5.update(data) |
| -def UpdateMd5ForDirectory(md5, dir_path): |
| +def _UpdateMd5ForDirectory(md5, dir_path): |
| for root, _, files in os.walk(dir_path): |
| for f in files: |
| - UpdateMd5ForFile(md5, os.path.join(root, f)) |
| + _UpdateMd5ForFile(md5, os.path.join(root, f)) |
| -def UpdateMd5ForPath(md5, path): |
| +def _UpdateMd5ForPath(md5, path): |
| if os.path.isdir(path): |
| - UpdateMd5ForDirectory(md5, path) |
| + _UpdateMd5ForDirectory(md5, path) |
| else: |
| - UpdateMd5ForFile(md5, path) |
| + _UpdateMd5ForFile(md5, path) |
| + |
| +class _Md5Checker(object): |
| + def __init__(self, record_path=None, input_paths=[], input_strings=[]): |
| + self.record_path = record_path |
| -class Md5Checker(object): |
| - def __init__(self, stamp=None, inputs=[], command=[]): |
| - self.stamp = stamp |
| + # Enforce that records end in .stamp so it is easier to find and delete |
| + # them. |
|
nilesh
2013/04/15 18:28:42
I think we should assert here instead of modifying
cjhopman
2013/04/15 18:32:35
Done.
|
| + if not self.record_path.endswith('.stamp'): |
| + self.record_path += '.stamp' |
| md5 = hashlib.md5() |
| - for i in inputs: |
| - UpdateMd5ForPath(md5, i) |
| - for s in command: |
| + for i in sorted(input_paths): |
| + _UpdateMd5ForPath(md5, i) |
| + for s in input_strings: |
| md5.update(s) |
| self.new_digest = md5.hexdigest() |
| self.old_digest = '' |
| - if os.path.exists(stamp): |
| - with open(stamp, 'r') as old_stamp: |
| - self.old_digest = old_stamp.read() |
| + if os.path.exists(self.record_path): |
| + with open(self.record_path, 'r') as old_record: |
| + self.old_digest = old_record.read() |
| def IsStale(self): |
| return self.old_digest != self.new_digest |
| def Write(self): |
| - with open(self.stamp, 'w') as new_stamp: |
| - new_stamp.write(self.new_digest) |
| + with open(self.record_path, 'w') as new_record: |
| + new_record.write(self.new_digest) |