repoclosure: Clean up cache for dnf5

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ář <lsedlar@redhat.com>
(cherry picked from commit a31f4233226d1df85d3f197ecc4b7fd47b827593)
This commit is contained in:
Lubomír Sedlář 2025-08-07 09:05:16 +02:00 committed by Stepan Oksanichenko
parent 363a28f561
commit 2fbc4e7bcb

View File

@ -101,22 +101,48 @@ def run_repoclosure(compose):
def _delete_repoclosure_cache_dirs(compose): def _delete_repoclosure_cache_dirs(compose):
from dnf.const import SYSTEM_CACHEDIR """Find any cached repodata and delete it. The case is not going to be
from dnf.util import am_i_root reused ever again, and would otherwise consume storage space.
from dnf.yum.misc import getCacheDir
if am_i_root(): DNF will use a different directory depending on whether it is running as
top_cache_dir = SYSTEM_CACHEDIR root or not. It is not easy to tell though if DNF 4 or 5 is being used, so
else: let's be sure and check both locations. All our cached entries are prefixed
top_cache_dir = getCacheDir() 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): try:
if name.startswith(compose.compose_id): # DNF 4
cache_path = os.path.join(top_cache_dir, name) from dnf.const import SYSTEM_CACHEDIR
if os.path.isdir(cache_path): from dnf.util import am_i_root
shutil.rmtree(cache_path) from dnf.yum.misc import getCacheDir
else:
os.remove(cache_path) 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): def _run_repoclosure_cmd(compose, repos, lookaside, arches, logfile):