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,15 +101,41 @@ def run_repoclosure(compose):
def _delete_repoclosure_cache_dirs(compose): def _delete_repoclosure_cache_dirs(compose):
"""Find any cached repodata and delete it. The case is not going to be
reused ever again, and would otherwise consume storage space.
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 = []
try:
# DNF 4
from dnf.const import SYSTEM_CACHEDIR from dnf.const import SYSTEM_CACHEDIR
from dnf.util import am_i_root from dnf.util import am_i_root
from dnf.yum.misc import getCacheDir from dnf.yum.misc import getCacheDir
if am_i_root(): if am_i_root():
top_cache_dir = SYSTEM_CACHEDIR cache_dirs.append(SYSTEM_CACHEDIR)
else: else:
top_cache_dir = getCacheDir() 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): for name in os.listdir(top_cache_dir):
if name.startswith(compose.compose_id): if name.startswith(compose.compose_id):
cache_path = os.path.join(top_cache_dir, name) cache_path = os.path.join(top_cache_dir, name)