xref: /openbmc/qemu/scripts/clean_functional_cache.py (revision 18f6f30b0089b470f3e737637a86dfb81ebd6eae)
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
7import os
8import stat
9import sys
10import time
11from pathlib import Path
12
13
14cache_dir_env = os.getenv('QEMU_TEST_CACHE_DIR')
15if cache_dir_env:
16    cache_dir = Path(cache_dir_env, "download")
17else:
18    cache_dir = Path(Path("~").expanduser(), ".cache", "qemu", "download")
19
20if not cache_dir.exists():
21    print(f"Cache dir {cache_dir} does not exist!", file=sys.stderr)
22    sys.exit(1)
23
24os.chdir(cache_dir)
25
26for 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()
46