| OLD | NEW |
| (Empty) |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import logging | |
| 6 import platform | |
| 7 import subprocess | |
| 8 import sys | |
| 9 | |
| 10 # pylint: disable=E0611 | |
| 11 from hashlib import sha256 | |
| 12 # pylint: enable=E0611 | |
| 13 from os.path import basename, realpath | |
| 14 | |
| 15 from mopy.file_hash import file_hash | |
| 16 from mopy.memoize import memoize | |
| 17 | |
| 18 _logging = logging.getLogger() | |
| 19 | |
| 20 @memoize | |
| 21 def _get_dependencies(filename): | |
| 22 """Returns a list of filenames for files that the given file depends on.""" | |
| 23 if platform.system() == 'Windows': | |
| 24 # There's no ldd on Windows. We can try to bundle or require depends, but | |
| 25 # given that we're not supporting component build this seems low priority. | |
| 26 return [] | |
| 27 _logging.debug("Getting dependencies for %s ...", filename) | |
| 28 lines = subprocess.check_output(['ldd', filename]).splitlines() | |
| 29 rv = [] | |
| 30 for line in lines: | |
| 31 i = line.find('/') | |
| 32 if i < 0: | |
| 33 _logging.debug(" => no file found in line: %s", line) | |
| 34 continue | |
| 35 rv.append(line[i:].split(None, 1)[0]) | |
| 36 _logging.debug(" => %s", rv) | |
| 37 return rv | |
| 38 | |
| 39 def transitive_hash(filename): | |
| 40 """Returns a string that represents the "transitive" hash of the given | |
| 41 file. The transitive hash is a hash of the file and all the shared libraries | |
| 42 on which it depends (done in an order-independent way).""" | |
| 43 hashes = set() | |
| 44 to_hash = [filename] | |
| 45 while to_hash: | |
| 46 current_filename = realpath(to_hash.pop()) | |
| 47 current_hash = file_hash(current_filename) | |
| 48 if current_hash in hashes: | |
| 49 _logging.debug("Already seen %s (%s) ...", current_filename, current_hash) | |
| 50 continue | |
| 51 _logging.debug("Haven't seen %s (%s) ...", current_filename, current_hash) | |
| 52 hashes.add(current_hash) | |
| 53 to_hash.extend(_get_dependencies(current_filename)) | |
| 54 return sha256('|'.join(sorted(hashes))).hexdigest() | |
| 55 | |
| 56 def main(argv): | |
| 57 logging.basicConfig() | |
| 58 # Uncomment to debug: | |
| 59 # _logging.setLevel(logging.DEBUG) | |
| 60 | |
| 61 if len(argv) < 2: | |
| 62 print """\ | |
| 63 Usage: %s [file] ... | |
| 64 | |
| 65 Prints the \"transitive\" hash of each (executable) file. The transitive | |
| 66 hash is a hash of the file and all the shared libraries on which it | |
| 67 depends (done in an order-independent way).""" % basename(argv[0]) | |
| 68 return 0 | |
| 69 | |
| 70 rv = 0 | |
| 71 for filename in argv[1:]: | |
| 72 try: | |
| 73 print transitive_hash(filename), filename | |
| 74 except subprocess.CalledProcessError: | |
| 75 print "ERROR", filename | |
| 76 rv = 1 | |
| 77 return rv | |
| 78 | |
| 79 if __name__ == '__main__': | |
| 80 sys.exit(main(sys.argv)) | |
| OLD | NEW |