tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

scripthash.py (1199B)


      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 
      5 import hashlib
      6 import logging
      7 import os
      8 import pathlib
      9 import sys
     10 
     11 _SRC_ROOT = pathlib.Path(__file__).resolve().parents[2]
     12 
     13 
     14 def _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 
     27 def 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]