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