File: clean_functional_cache.py

package info (click to toggle)
qemu 1%3A10.2.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 394,672 kB
  • sloc: ansic: 4,572,710; pascal: 114,932; python: 110,772; sh: 53,573; asm: 50,207; makefile: 26,351; perl: 17,092; cpp: 11,455; xml: 3,635; objc: 2,877; yacc: 2,505; php: 1,299; tcl: 1,296; lex: 1,110; sql: 71; sed: 35; awk: 8; javascript: 7
file content (45 lines) | stat: -rwxr-xr-x 1,294 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
"""Delete stale assets from the download cache of the functional tests"""

import os
import stat
import sys
import time
from pathlib import Path


cache_dir_env = os.getenv('QEMU_TEST_CACHE_DIR')
if cache_dir_env:
    cache_dir = Path(cache_dir_env, "download")
else:
    cache_dir = Path(Path("~").expanduser(), ".cache", "qemu", "download")

if not cache_dir.exists():
    print(f"Cache dir {cache_dir} does not exist!", file=sys.stderr)
    sys.exit(1)

os.chdir(cache_dir)

for file in cache_dir.iterdir():
    # Only consider the files that use a sha256 as filename:
    if len(file.name) != 64:
        continue

    try:
        timestamp = int(file.with_suffix(".stamp").read_text())
    except FileNotFoundError:
        # Assume it's an old file that was already in the cache before we
        # added the code for evicting stale assets. Use the release date
        # of QEMU v10.1 as a default timestamp.
        timestamp = time.mktime((2025, 8, 26, 0, 0, 0, 0, 0, 0))

    age = time.time() - timestamp

    # Delete files older than half of a year (183 days * 24h * 60m * 60s)
    if age > 15811200:
        print(f"Removing {cache_dir}/{file.name}.")
        file.chmod(stat.S_IWRITE)
        file.unlink()