Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2014 Google Inc. All rights reserved. | 2 # Copyright (c) 2014 Google Inc. All rights reserved. |
| 3 # | 3 # |
| 4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
| 6 # met: | 6 # met: |
| 7 # | 7 # |
| 8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | 29 |
| 30 import hashlib | 30 import hashlib |
| 31 import re | 31 import os.path |
|
apavlov
2014/10/22 06:32:42
from os import path
could let you use just
path.
eustas
2014/10/22 08:08:09
Personally for me this will be less readable.
| |
| 32 | 32 |
| 33 try: | 33 try: |
| 34 import json | 34 import json |
| 35 except ImportError: | 35 except ImportError: |
| 36 import simplejson as json | 36 import simplejson as json |
| 37 | 37 |
| 38 | 38 |
| 39 def save_hashes(hashes_file_path, hashes): | 39 def save_hashes(hashes_file_path, hashes): |
| 40 try: | 40 try: |
| 41 with open(hashes_file_path, "wt") as hashes_file: | 41 with open(hashes_file_path, "wt") as hashes_file: |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 58 with open(file_path) as file: | 58 with open(file_path) as file: |
| 59 data = file.read() | 59 data = file.read() |
| 60 md5_hash = hashlib.md5(data).hexdigest() | 60 md5_hash = hashlib.md5(data).hexdigest() |
| 61 return md5_hash | 61 return md5_hash |
| 62 | 62 |
| 63 | 63 |
| 64 def files_with_invalid_hashes(hash_file_path, file_paths): | 64 def files_with_invalid_hashes(hash_file_path, file_paths): |
| 65 hashes = load_hashes(hash_file_path) | 65 hashes = load_hashes(hash_file_path) |
| 66 result = [] | 66 result = [] |
| 67 for file_path in file_paths: | 67 for file_path in file_paths: |
| 68 file_name = re.sub(".*/", "", file_path) | 68 file_name = os.path.basename(file_path) |
| 69 if calculate_file_hash(file_path) != hashes.get(file_name, ""): | 69 if calculate_file_hash(file_path) != hashes.get(file_name, ""): |
| 70 result.append(file_path) | 70 result.append(file_path) |
| 71 return result | 71 return result |
| 72 | 72 |
| 73 | 73 |
| 74 def update_file_hashes(hash_file_path, file_paths): | 74 def update_file_hashes(hash_file_path, file_paths): |
| 75 hashes = {} | 75 hashes = {} |
| 76 for file_path in file_paths: | 76 for file_path in file_paths: |
| 77 file_name = re.sub(".*/", "", file_path) | 77 file_name = os.path.basename(file_path) |
| 78 hashes[file_name] = calculate_file_hash(file_path) | 78 hashes[file_name] = calculate_file_hash(file_path) |
| 79 save_hashes(hash_file_path, hashes) | 79 save_hashes(hash_file_path, hashes) |
| OLD | NEW |