Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/maint/heuristic_clear_cache.py
6162 views
1
#!/usr/bin/env python3
2
# Copyright 2025 The Emscripten Authors. All rights reserved.
3
# Emscripten is available under two separate licenses, the MIT license and the
4
# University of Illinois/NCSA Open Source License. Both these licenses can be
5
# found in the LICENSE file.
6
7
# This script can be used to clear the cache based on a heuristic detection of
8
# whether any source files generating the cache have changed.
9
# This can help avoid redundant cache clears in a workflow, but note that this
10
# script is only a heuristic, and not necessarily perfect.
11
12
import os
13
import sys
14
15
__scriptdir__ = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16
__rootdir__ = os.path.dirname(__scriptdir__)
17
sys.path.insert(0, __rootdir__)
18
19
from tools import cache, utils
20
21
# Modifying the following files/paths can often disturb Emscripten generated cache.
22
# If any of those are modified, then the Emscripten cache should be cleared.
23
FILES_THAT_CAN_DISTURB_CACHE = [
24
utils.path_from_root('system'),
25
utils.path_from_root('tools', 'system_libs.py'),
26
]
27
28
29
def recursive_mtime(path):
30
mtime = os.path.getmtime(path)
31
if os.path.isfile(path):
32
return mtime
33
34
with os.scandir(path) as it:
35
for entry in it:
36
mtime = max(mtime, recursive_mtime(entry.path) if entry.is_dir() else os.path.getmtime(entry.path))
37
return mtime
38
39
40
def newest_mtime(paths):
41
return max((recursive_mtime(path) for path in paths), default=0)
42
43
44
def heuristic_clear_cache():
45
mtime_file = cache.get_path('system_libs_mtime.txt')
46
try:
47
system_libs_mtime = open(mtime_file).read()
48
except Exception:
49
system_libs_mtime = 0
50
51
newest_system_libs_mtime = str(newest_mtime(FILES_THAT_CAN_DISTURB_CACHE))
52
53
if newest_system_libs_mtime != system_libs_mtime:
54
print(f'Cache timestamp {system_libs_mtime} does not match with current timestamp {newest_system_libs_mtime}. Clearing cache...')
55
cache.erase()
56
open(mtime_file, 'w').write(str(newest_system_libs_mtime))
57
else:
58
print('Cache timestamp is up to date, no clear needed.')
59
60
61
if __name__ == '__main__':
62
sys.exit(heuristic_clear_cache())
63
64