qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Thomas Huth <thuth@redhat.com>
Cc: qemu-devel@nongnu.org, "John Snow" <jsnow@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: Re: [PATCH 2/2] tests: Evict stale files in the functional download cache after a while
Date: Fri, 10 Oct 2025 10:50:41 +0100	[thread overview]
Message-ID: <aOjW8aJKxNtUf3Py@redhat.com> (raw)
In-Reply-To: <20251010093244.807544-3-thuth@redhat.com>

On Fri, Oct 10, 2025 at 11:32:43AM +0200, Thomas Huth wrote:
> From: Thomas Huth <thuth@redhat.com>
> 
> The download cache of the functional tests is currently only growing.
> But sometimes tests get removed or changed to use different assets,
> thus we should clean up the stale old assets after a while when they
> are not in use anymore. So add a script that looks at the time stamps
> of the assets and removes them if they haven't been touched for more
> than half of a year. Since there might also be some assets around that
> have been added to the cache before we added the time stamp files,
> assume a default time stamp that is close to the creation date of this
> patch, so that we don't delete these files too early.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  MAINTAINERS                       |  1 +
>  scripts/clean_functional_cache.py | 47 +++++++++++++++++++++++++++++++
>  tests/Makefile.include            |  1 +
>  3 files changed, 49 insertions(+)
>  create mode 100755 scripts/clean_functional_cache.py
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 84cfd85e1fa..4c468d45337 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -4398,6 +4398,7 @@ M: Thomas Huth <thuth@redhat.com>
>  R: Philippe Mathieu-Daudé <philmd@linaro.org>
>  R: Daniel P. Berrange <berrange@redhat.com>
>  F: docs/devel/testing/functional.rst
> +F: scripts/clean_functional_cache.py
>  F: tests/functional/qemu_test/
>  
>  Windows Hosted Continuous Integration
> diff --git a/scripts/clean_functional_cache.py b/scripts/clean_functional_cache.py
> new file mode 100755
> index 00000000000..e5c4d1acaf3
> --- /dev/null
> +++ b/scripts/clean_functional_cache.py
> @@ -0,0 +1,47 @@
> +#!/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")

This creates a Path object but then doesn't take advantage of
any of its functionality, calling os. functions still....

> +
> +if not os.path.exists(cache_dir):

  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 os.listdir(cache_dir):

  for file in cache_dir.iterdir():

> +    filename = os.fsdecode(file)

Wouldn't be required since 'file' would be a Path object

> +    # Only consider the files that use a sha256 as filename:
> +    if len(filename) != 64:

    if len(file.name) != 64

> +        continue
> +
> +    try:
> +        with open(filename + ".stamp", "r", encoding='utf-8') as fh:
> +            timestamp = int(fh.read())

   timestamp = file.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))

The prev patch will make the precache task create the .stamp for all
files that are currently in use by the current branch. So the only
thing this does is to prevent us deleting cached files that might
still be needed by a different branch. There will be few of them,
so if we prematurely delete a handful that's not a big deal. If we
switch to checking mtime, this except won't even exist.

> +
> +    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}/{filename}.")
> +        os.chmod(filename, stat.S_IWRITE)

   file.chmod(stat.S_IWRITE)

> +        os.remove(filename)

   file.unlink()

> diff --git a/tests/Makefile.include b/tests/Makefile.include
> index e47ef4d45c9..d4dfbf3716d 100644
> --- a/tests/Makefile.include
> +++ b/tests/Makefile.include
> @@ -111,6 +111,7 @@ $(FUNCTIONAL_TARGETS): check-venv
>  .PHONY: check-functional
>  check-functional: check-venv
>  	@$(NINJA) precache-functional
> +	@$(PYTHON) $(SRC_PATH)/scripts/clean_functional_cache.py
>  	@QEMU_TEST_NO_DOWNLOAD=1 $(MAKE) SPEED=thorough check-func check-func-quick
>  
>  .PHONY: check-func check-func-quick
> -- 
> 2.51.0
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



  reply	other threads:[~2025-10-10  9:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-10  9:32 [PATCH 0/2] Clean up the functional download cache after some months Thomas Huth
2025-10-10  9:32 ` [PATCH 1/2] tests/functional: Set current time stamp of assets when using them Thomas Huth
2025-10-10  9:39   ` Daniel P. Berrangé
2025-10-10  9:46     ` Thomas Huth
2025-10-10  9:53       ` Daniel P. Berrangé
2025-10-10  9:32 ` [PATCH 2/2] tests: Evict stale files in the functional download cache after a while Thomas Huth
2025-10-10  9:50   ` Daniel P. Berrangé [this message]
2025-10-13 11:47     ` Thomas Huth
2025-10-13 11:50       ` Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aOjW8aJKxNtUf3Py@redhat.com \
    --to=berrange@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=jsnow@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).