Skip to content

Commit

Permalink
Remember if repositories were loaded
Browse files Browse the repository at this point in the history
In libdnf5, the `repos_updated_and_loaded` bool attribute, which serves
the same purpose, is private.

Check if the repositories are loaded before making queries.
  • Loading branch information
pkratoch committed Oct 29, 2024
1 parent 52e3428 commit 4c78c94
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions pyanaconda/modules/payloads/payload/dnf/dnf_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ def __init__(self):
self._download_location = None
self._md_hashes = {}
self._enabled_system_repositories = []
self._repositories_loaded = False
self._query_environments = None
self._query_groups = None

Expand Down Expand Up @@ -428,6 +429,7 @@ def reset_base(self):
self._download_location = None
self._md_hashes = {}
self._enabled_system_repositories = []
self._repositories_loaded = False
log.debug("The DNF base has been reset.")

def configure_base(self, data: PackagesConfigurationData):
Expand Down Expand Up @@ -481,6 +483,9 @@ def default_environment(self):

@property
def _environments(self):
if not self._repositories_loaded:
log.warning("There is no metadata about environments and groups!")
return []
if self._query_environments is None:
self._query_environments = libdnf5.comps.EnvironmentQuery(self._base)
return self._query_environments
Expand Down Expand Up @@ -570,6 +575,9 @@ def _get_environment_data(self, env) -> CompsEnvironmentData:

@property
def _groups(self):
if not self._repositories_loaded:
log.warning("There is no metadata about environments and groups!")
return []
if self._query_groups is None:
self._query_groups = libdnf5.comps.GroupQuery(self._base)
return self._query_groups
Expand Down Expand Up @@ -781,9 +789,9 @@ def is_package_available(self, package_spec):
:param package_spec: a package spec
:return: True if the package can be installed, otherwise False
"""
#if not self._base.sack:
# log.warning("There is no metadata about packages!")
# return False
if not self._repositories_loaded:
log.warning("There is no metadata about packages!")
return False

query = libdnf5.rpm.PackageQuery(self._base)
query.filter_name([package_spec])
Expand All @@ -797,9 +805,9 @@ def match_available_packages(self, pattern):
:param pattern: a pattern for package names
:return: a list of matched package names
"""
#if not self._base.sack:
# log.warning("There is no metadata about packages!")
# return []
if not self._repositories_loaded:
log.warning("There is no metadata about packages!")
return []

query = libdnf5.rpm.PackageQuery(self._base)
query.filter_name([pattern], libdnf5.common.QueryCmp_GLOB)
Expand Down Expand Up @@ -1064,6 +1072,9 @@ def _create_repository(self, data: RepoConfigurationData):
:param RepoConfigurationData data: a repo configuration
return dnf.repo.Repo: a DNF repository
"""
if self._repositories_loaded:
raise RuntimeError("Cannot create a new repository. Repositories were already loaded.")

repo_sack = self._base.get_repo_sack()
repo = repo_sack.create_repo(data.name)
config = simplify_config(repo.get_config())
Expand Down Expand Up @@ -1253,6 +1264,7 @@ def load_repositories(self):
except RuntimeError as e:
log.warning(str(e))
raise MetadataError(str(e)) from None
self._repositories_loaded = True
log.info("Loaded repositories.")

def load_repomd_hashes(self):
Expand Down

0 comments on commit 4c78c94

Please sign in to comment.