Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 logging | 6 import logging |
| 7 import platform | |
| 6 import subprocess | 8 import subprocess |
| 7 import sys | 9 import sys |
| 8 | 10 |
| 9 # pylint: disable=E0611 | 11 # pylint: disable=E0611 |
| 10 from hashlib import sha256 | 12 from hashlib import sha256 |
| 11 # pylint: enable=E0611 | 13 # pylint: enable=E0611 |
| 12 from os.path import basename, realpath | 14 from os.path import basename, realpath |
| 13 | 15 |
| 14 _logging = logging.getLogger() | 16 _logging = logging.getLogger() |
| 15 | 17 |
| 16 # pylint: disable=C0301 | 18 # pylint: disable=C0301 |
| 17 # Based on/taken from | 19 # Based on/taken from |
| 18 # http://code.activestate.com/recipes/578231-probably-the-fastest-memoization- decorator-in-the-/ | 20 # http://code.activestate.com/recipes/578231-probably-the-fastest-memoization- decorator-in-the-/ |
| 19 # (with cosmetic changes). | 21 # (with cosmetic changes). |
| 20 # pylint: enable=C0301 | 22 # pylint: enable=C0301 |
| 21 def _memoize(f): | 23 def _memoize(f): |
| 22 """Memoization decorator for a function taking a single argument.""" | 24 """Memoization decorator for a function taking a single argument.""" |
| 23 class Memoize(dict): | 25 class Memoize(dict): |
| 24 def __missing__(self, key): | 26 def __missing__(self, key): |
| 25 rv = self[key] = f(key) | 27 rv = self[key] = f(key) |
| 26 return rv | 28 return rv |
| 27 return Memoize().__getitem__ | 29 return Memoize().__getitem__ |
| 28 | 30 |
| 29 @_memoize | 31 @_memoize |
| 30 def _file_hash(filename): | 32 def _file_hash(filename): |
| 31 """Returns a string representing the hash of the given file.""" | 33 """Returns a string representing the hash of the given file.""" |
| 32 _logging.debug("Hashing %s ...", filename) | 34 _logging.debug("Hashing %s ...", filename) |
| 33 rv = subprocess.check_output(['sha256sum', '-b', filename]).split(None, 1)[0] | 35 with open(filename, mode='rb') as f: |
|
viettrungluu
2014/11/11 18:35:38
What's the performance of this versus running sha2
jam
2014/11/11 20:09:32
it's about 10% faster. probably because it doesn't
| |
| 34 _logging.debug(" => %s", rv) | 36 m = hashlib.sha256() |
| 35 return rv | 37 while True: |
| 38 block = f.read(4096) | |
| 39 if not block: | |
| 40 break | |
| 41 _logging.debug(" => %s", m.hexdigest()) | |
| 42 return m.hexdigest() | |
| 36 | 43 |
| 37 @_memoize | 44 @_memoize |
| 38 def _get_dependencies(filename): | 45 def _get_dependencies(filename): |
| 39 """Returns a list of filenames for files that the given file depends on.""" | 46 """Returns a list of filenames for files that the given file depends on.""" |
| 47 if platform.system() == 'Windows': | |
| 48 # There's no ldd on Windows. We can try to bundle or require depends, but | |
| 49 # given that we're not supporting component build this seems low priority. | |
| 50 return [] | |
| 40 _logging.debug("Getting dependencies for %s ...", filename) | 51 _logging.debug("Getting dependencies for %s ...", filename) |
| 41 lines = subprocess.check_output(['ldd', filename]).splitlines() | 52 lines = subprocess.check_output(['ldd', filename]).splitlines() |
| 42 rv = [] | 53 rv = [] |
| 43 for line in lines: | 54 for line in lines: |
| 44 i = line.find('/') | 55 i = line.find('/') |
| 45 if i < 0: | 56 if i < 0: |
| 46 _logging.debug(" => no file found in line: %s", line) | 57 _logging.debug(" => no file found in line: %s", line) |
| 47 continue | 58 continue |
| 48 rv.append(line[i:].split(None, 1)[0]) | 59 rv.append(line[i:].split(None, 1)[0]) |
| 49 _logging.debug(" => %s", rv) | 60 _logging.debug(" => %s", rv) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 for filename in argv[1:]: | 95 for filename in argv[1:]: |
| 85 try: | 96 try: |
| 86 print transitive_hash(filename), filename | 97 print transitive_hash(filename), filename |
| 87 except subprocess.CalledProcessError: | 98 except subprocess.CalledProcessError: |
| 88 print "ERROR", filename | 99 print "ERROR", filename |
| 89 rv = 1 | 100 rv = 1 |
| 90 return rv | 101 return rv |
| 91 | 102 |
| 92 if __name__ == '__main__': | 103 if __name__ == '__main__': |
| 93 sys.exit(main(sys.argv)) | 104 sys.exit(main(sys.argv)) |
| OLD | NEW |