2 # Copyright (c) 2014 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 import simplejson as json
39 def save_hashes(hashes_file_path, hashes):
41 with open(hashes_file_path, "wt") as hashes_file:
42 json.dump(hashes, hashes_file, indent=4, separators=(",", ": "))
44 print "ERROR: Failed to write %s" % hashes_file_path
48 def load_hashes(hashes_file_path):
50 with open(hashes_file_path, "r") as hashes_file:
51 hashes = json.load(hashes_file)
57 def calculate_file_hash(file_path):
58 with open(file_path) as file:
60 md5_hash = hashlib.md5(data).hexdigest()
64 def files_with_invalid_hashes(hash_file_path, file_paths):
65 hashes = load_hashes(hash_file_path)
67 for file_path in file_paths:
68 file_name = os.path.basename(file_path)
69 if calculate_file_hash(file_path) != hashes.get(file_name, ""):
70 result.append(file_path)
74 def update_file_hashes(hash_file_path, file_paths):
76 for file_path in file_paths:
77 file_name = os.path.basename(file_path)
78 hashes[file_name] = calculate_file_hash(file_path)
79 save_hashes(hash_file_path, hashes)