master
py 45 lines 1.26 KB
Raw
1 #!/usr/bin/env python3
2 #
3 # SPDX-License-Identifier: GPL-2.0-or-later
4 #
5 """Delete stale assets from the download cache of the functional tests"""
6
7 import os
8 import stat
9 import sys
10 import time
11 from pathlib import Path
12
13
14 cache_dir_env = os.getenv('QEMU_TEST_CACHE_DIR')
15 if cache_dir_env:
16 cache_dir = Path(cache_dir_env, "download")
17 else:
18 cache_dir = Path(Path("~").expanduser(), ".cache", "qemu", "download")
19
20 if not cache_dir.exists():
21 print(f"Cache dir {cache_dir} does not exist!", file=sys.stderr)
22 sys.exit(1)
23
24 os.chdir(cache_dir)
25
26 for file in cache_dir.iterdir():
27 # Only consider the files that use a sha256 as filename:
28 if len(file.name) != 64:
29 continue
30
31 try:
32 timestamp = int(file.with_suffix(".stamp").read_text())
33 except FileNotFoundError:
34 # Assume it's an old file that was already in the cache before we
35 # added the code for evicting stale assets. Use the release date
36 # of QEMU v10.1 as a default timestamp.
37 timestamp = time.mktime((2025, 8, 26, 0, 0, 0, 0, 0, 0))
38
39 age = time.time() - timestamp
40
41 # Delete files older than half of a year (183 days * 24h * 60m * 60s)
42 if age > 15811200:
43 print(f"Removing {cache_dir}/{file.name}.")
44 file.chmod(stat.S_IWRITE)
45 file.unlink()