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):