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