Path: blob/main/tools/maint/heuristic_clear_cache.py
6162 views
#!/usr/bin/env python31# Copyright 2025 The Emscripten Authors. All rights reserved.2# Emscripten is available under two separate licenses, the MIT license and the3# University of Illinois/NCSA Open Source License. Both these licenses can be4# found in the LICENSE file.56# This script can be used to clear the cache based on a heuristic detection of7# whether any source files generating the cache have changed.8# This can help avoid redundant cache clears in a workflow, but note that this9# script is only a heuristic, and not necessarily perfect.1011import os12import sys1314__scriptdir__ = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))15__rootdir__ = os.path.dirname(__scriptdir__)16sys.path.insert(0, __rootdir__)1718from tools import cache, utils1920# Modifying the following files/paths can often disturb Emscripten generated cache.21# If any of those are modified, then the Emscripten cache should be cleared.22FILES_THAT_CAN_DISTURB_CACHE = [23utils.path_from_root('system'),24utils.path_from_root('tools', 'system_libs.py'),25]262728def recursive_mtime(path):29mtime = os.path.getmtime(path)30if os.path.isfile(path):31return mtime3233with os.scandir(path) as it:34for entry in it:35mtime = max(mtime, recursive_mtime(entry.path) if entry.is_dir() else os.path.getmtime(entry.path))36return mtime373839def newest_mtime(paths):40return max((recursive_mtime(path) for path in paths), default=0)414243def heuristic_clear_cache():44mtime_file = cache.get_path('system_libs_mtime.txt')45try:46system_libs_mtime = open(mtime_file).read()47except Exception:48system_libs_mtime = 04950newest_system_libs_mtime = str(newest_mtime(FILES_THAT_CAN_DISTURB_CACHE))5152if newest_system_libs_mtime != system_libs_mtime:53print(f'Cache timestamp {system_libs_mtime} does not match with current timestamp {newest_system_libs_mtime}. Clearing cache...')54cache.erase()55open(mtime_file, 'w').write(str(newest_system_libs_mtime))56else:57print('Cache timestamp is up to date, no clear needed.')585960if __name__ == '__main__':61sys.exit(heuristic_clear_cache())626364