From 2fbc4e7bcb2301bda1b1dedd697979460abcc2c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Thu, 7 Aug 2025 09:05:16 +0200 Subject: [PATCH] repoclosure: Clean up cache for dnf5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If `dnf repoclosure` actually calls dnf5 (which is not easy to tell), our cache clean up is ineffective as dnf5 uses different locations to dnf4. Since it's not easy to tell what `dnf` actually is, let's be safe and iterate over both possibilities. Signed-off-by: Lubomír Sedlář (cherry picked from commit a31f4233226d1df85d3f197ecc4b7fd47b827593) --- pungi/phases/repoclosure.py | 54 +++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/pungi/phases/repoclosure.py b/pungi/phases/repoclosure.py index 86d2d190..1d3fad00 100644 --- a/pungi/phases/repoclosure.py +++ b/pungi/phases/repoclosure.py @@ -101,22 +101,48 @@ def run_repoclosure(compose): def _delete_repoclosure_cache_dirs(compose): - from dnf.const import SYSTEM_CACHEDIR - from dnf.util import am_i_root - from dnf.yum.misc import getCacheDir + """Find any cached repodata and delete it. The case is not going to be + reused ever again, and would otherwise consume storage space. - if am_i_root(): - top_cache_dir = SYSTEM_CACHEDIR - else: - top_cache_dir = getCacheDir() + DNF will use a different directory depending on whether it is running as + root or not. It is not easy to tell though if DNF 4 or 5 is being used, so + let's be sure and check both locations. All our cached entries are prefixed + by compose ID, so there's very limited amount of risk that we would delete + something incorrect. + """ + cache_dirs = [] - for name in os.listdir(top_cache_dir): - if name.startswith(compose.compose_id): - cache_path = os.path.join(top_cache_dir, name) - if os.path.isdir(cache_path): - shutil.rmtree(cache_path) - else: - os.remove(cache_path) + try: + # DNF 4 + from dnf.const import SYSTEM_CACHEDIR + from dnf.util import am_i_root + from dnf.yum.misc import getCacheDir + + if am_i_root(): + cache_dirs.append(SYSTEM_CACHEDIR) + else: + cache_dirs.append(getCacheDir()) + except ImportError: + pass + + try: + # DNF 5 config works directly for root, no need for special case. + import libdnf5 + + base = libdnf5.base.Base() + config = base.get_config() + cache_dirs.append(config.cachedir) + except ImportError: + pass + + for top_cache_dir in cache_dirs: + for name in os.listdir(top_cache_dir): + if name.startswith(compose.compose_id): + cache_path = os.path.join(top_cache_dir, name) + if os.path.isdir(cache_path): + shutil.rmtree(cache_path) + else: + os.remove(cache_path) def _run_repoclosure_cmd(compose, repos, lookaside, arches, logfile):