| 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 hashlib | 5 import hashlib |
| 6 import os | 6 import os |
| 7 | 7 |
| 8 | 8 |
| 9 def CallAndRecordIfStale( | 9 def CallAndRecordIfStale( |
| 10 function, record_path=None, input_paths=[], input_strings=[]): | 10 function, record_path=None, input_paths=[], input_strings=[], force=False): |
| 11 """Calls function if the md5sum of the input paths/strings has changed. | 11 """Calls function if the md5sum of the input paths/strings has changed. |
| 12 | 12 |
| 13 The md5sum of the inputs is compared with the one stored in record_path. If | 13 The md5sum of the inputs is compared with the one stored in record_path. If |
| 14 this has changed (or the record doesn't exist), function will be called and | 14 this has changed (or the record doesn't exist), function will be called and |
| 15 the new md5sum will be recorded. | 15 the new md5sum will be recorded. |
| 16 |
| 17 If force is True, the function will be called regardless of whether the |
| 18 md5sum is out of date. |
| 16 """ | 19 """ |
| 17 md5_checker = _Md5Checker( | 20 md5_checker = _Md5Checker( |
| 18 record_path=record_path, | 21 record_path=record_path, |
| 19 input_paths=input_paths, | 22 input_paths=input_paths, |
| 20 input_strings=input_strings) | 23 input_strings=input_strings) |
| 21 if md5_checker.IsStale(): | 24 if force or md5_checker.IsStale(): |
| 22 function() | 25 function() |
| 23 md5_checker.Write() | 26 md5_checker.Write() |
| 24 | 27 |
| 25 | 28 |
| 26 def _UpdateMd5ForFile(md5, path, block_size=2**16): | 29 def _UpdateMd5ForFile(md5, path, block_size=2**16): |
| 27 with open(path, 'rb') as infile: | 30 with open(path, 'rb') as infile: |
| 28 while True: | 31 while True: |
| 29 data = infile.read(block_size) | 32 data = infile.read(block_size) |
| 30 if not data: | 33 if not data: |
| 31 break | 34 break |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 if os.path.exists(self.record_path): | 67 if os.path.exists(self.record_path): |
| 65 with open(self.record_path, 'r') as old_record: | 68 with open(self.record_path, 'r') as old_record: |
| 66 self.old_digest = old_record.read() | 69 self.old_digest = old_record.read() |
| 67 | 70 |
| 68 def IsStale(self): | 71 def IsStale(self): |
| 69 return self.old_digest != self.new_digest | 72 return self.old_digest != self.new_digest |
| 70 | 73 |
| 71 def Write(self): | 74 def Write(self): |
| 72 with open(self.record_path, 'w') as new_record: | 75 with open(self.record_path, 'w') as new_record: |
| 73 new_record.write(self.new_digest) | 76 new_record.write(self.new_digest) |
| OLD | NEW |