1# Copyright 2024 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import hashlib 6import logging 7import os 8import pathlib 9import sys 10 11_SRC_ROOT = pathlib.Path(__file__).resolve().parents[2] 12 13 14def _find_deps(): 15 module_paths = (os.path.abspath(m.__file__) for m in sys.modules.values() 16 if m and getattr(m, '__file__', None)) 17 ret = set() 18 for path in module_paths: 19 if path.startswith(str(_SRC_ROOT)): 20 if (path.endswith('.pyc') 21 or (path.endswith('c') and not os.path.splitext(path)[1])): 22 path = path[:-1] 23 ret.add(path) 24 return list(ret) 25 26 27def compute(extra_paths=None): 28 """Compute a hash of loaded Python modules and given |extra_paths|.""" 29 all_paths = _find_deps() + (extra_paths or []) 30 all_paths = [os.path.relpath(p, _SRC_ROOT) for p in all_paths] 31 all_paths.sort() 32 md5 = hashlib.md5() 33 for path in all_paths: 34 md5.update((_SRC_ROOT / path).read_bytes()) 35 md5.update(path.encode('utf-8')) 36 logging.info('Script hash from: \n%s\n', '\n'.join(all_paths)) 37 return md5.hexdigest()[:10] 38