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=[], force=False): | 10 function, record_path=None, input_paths=None, input_strings=None, |
| 11 force=False): |
11 """Calls function if the md5sum of the input paths/strings has changed. | 12 """Calls function if the md5sum of the input paths/strings has changed. |
12 | 13 |
13 The md5sum of the inputs is compared with the one stored in record_path. If | 14 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 | 15 this has changed (or the record doesn't exist), function will be called and |
15 the new md5sum will be recorded. | 16 the new md5sum will be recorded. |
16 | 17 |
17 If force is True, the function will be called regardless of whether the | 18 If force is True, the function will be called regardless of whether the |
18 md5sum is out of date. | 19 md5sum is out of date. |
19 """ | 20 """ |
| 21 if not input_paths: |
| 22 input_paths = [] |
| 23 if not input_strings: |
| 24 input_strings = [] |
20 md5_checker = _Md5Checker( | 25 md5_checker = _Md5Checker( |
21 record_path=record_path, | 26 record_path=record_path, |
22 input_paths=input_paths, | 27 input_paths=input_paths, |
23 input_strings=input_strings) | 28 input_strings=input_strings) |
24 if force or md5_checker.IsStale(): | 29 if force or md5_checker.IsStale(): |
25 function() | 30 function() |
26 md5_checker.Write() | 31 md5_checker.Write() |
27 | 32 |
28 | 33 |
29 def _UpdateMd5ForFile(md5, path, block_size=2**16): | 34 def _UpdateMd5ForFile(md5, path, block_size=2**16): |
(...skipping 12 matching lines...) Expand all Loading... |
42 | 47 |
43 | 48 |
44 def _UpdateMd5ForPath(md5, path): | 49 def _UpdateMd5ForPath(md5, path): |
45 if os.path.isdir(path): | 50 if os.path.isdir(path): |
46 _UpdateMd5ForDirectory(md5, path) | 51 _UpdateMd5ForDirectory(md5, path) |
47 else: | 52 else: |
48 _UpdateMd5ForFile(md5, path) | 53 _UpdateMd5ForFile(md5, path) |
49 | 54 |
50 | 55 |
51 class _Md5Checker(object): | 56 class _Md5Checker(object): |
52 def __init__(self, record_path=None, input_paths=[], input_strings=[]): | 57 def __init__(self, record_path=None, input_paths=None, input_strings=None): |
| 58 if not input_paths: |
| 59 input_paths = [] |
| 60 if not input_strings: |
| 61 input_strings = [] |
| 62 |
53 assert record_path.endswith('.stamp'), ( | 63 assert record_path.endswith('.stamp'), ( |
54 'record paths must end in \'.stamp\' so that they are easy to find ' | 64 'record paths must end in \'.stamp\' so that they are easy to find ' |
55 'and delete') | 65 'and delete') |
56 | 66 |
57 self.record_path = record_path | 67 self.record_path = record_path |
58 | 68 |
59 md5 = hashlib.md5() | 69 md5 = hashlib.md5() |
60 for i in sorted(input_paths): | 70 for i in sorted(input_paths): |
61 _UpdateMd5ForPath(md5, i) | 71 _UpdateMd5ForPath(md5, i) |
62 for s in input_strings: | 72 for s in input_strings: |
63 md5.update(s) | 73 md5.update(s) |
64 self.new_digest = md5.hexdigest() | 74 self.new_digest = md5.hexdigest() |
65 | 75 |
66 self.old_digest = '' | 76 self.old_digest = '' |
67 if os.path.exists(self.record_path): | 77 if os.path.exists(self.record_path): |
68 with open(self.record_path, 'r') as old_record: | 78 with open(self.record_path, 'r') as old_record: |
69 self.old_digest = old_record.read() | 79 self.old_digest = old_record.read() |
70 | 80 |
71 def IsStale(self): | 81 def IsStale(self): |
72 return self.old_digest != self.new_digest | 82 return self.old_digest != self.new_digest |
73 | 83 |
74 def Write(self): | 84 def Write(self): |
75 with open(self.record_path, 'w') as new_record: | 85 with open(self.record_path, 'w') as new_record: |
76 new_record.write(self.new_digest) | 86 new_record.write(self.new_digest) |
OLD | NEW |