Skip to content

Commit

Permalink
Implement replacing repositories
Browse files Browse the repository at this point in the history
There is no way to remove a repository from the RepoSack without dropping
the entire Base. Therefore, a repository can be only configured again,
but not replaced. Since it cannot be removed, the test for repository
removal was removed as well.
  • Loading branch information
pkratoch committed Nov 25, 2024
1 parent 49f9c11 commit 42e2d38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
41 changes: 25 additions & 16 deletions pyanaconda/modules/payloads/payload/dnf/dnf_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,33 +810,29 @@ def add_repository(self, data: RepoConfigurationData):
:param RepoConfigurationData data: a repo configuration
"""
repositories = libdnf5.repo.RepoQuery(self._base)
repositories.filter_id(data.name)

with self._lock:
# Create a new repository.
repo = self._create_repository(data)

# FIXME: How to handle existing repositories?
# Remove an existing repository.
#
# if repo.id in self._base.repos:
# self._base.repos.pop(repo.id)

# Add the new repository.
#self._base.repos.add(repo)
if repositories.empty():
# Create a new repository.
repo = self._create_repository(data)
else:
# Replace the existing repository with a new one.
repo = self._configure_repository(repositories.get(), data)

log.info("Added the '%s' repository: %s", repo.get_id(), repo)

def _create_repository(self, data: RepoConfigurationData):
"""Create a DNF repository.
def _configure_repository(self, repo: libdnf5.repo.Repo, data: RepoConfigurationData):
"""Configure a DNF repository.
:param libdnf5.repo.Repo repo:existing repository
:param RepoConfigurationData data: a repo configuration
return dnf.repo.Repo: a DNF repository
return libdnf5.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 = repo.get_config()

# Disable the repo if requested.
Expand Down Expand Up @@ -887,6 +883,19 @@ def _create_repository(self, data: RepoConfigurationData):

return repo

def _create_repository(self, data: RepoConfigurationData):
"""Create a DNF repository.
:param RepoConfigurationData data: a repo configuration
return libdnf5.repo.Repo: a DNF repository
"""
if self._repositories_loaded:
raise RuntimeError("Cannot create a new repository. Repositories were already loaded.")

repo = self._base.get_repo_sack().create_repo(data.name)

return self._configure_repository(repo, data)

def generate_repo_file(self, data: RepoConfigurationData):
"""Generate a content of the .repo file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,6 @@ def test_add_repository_packages(self):
assert config.includepkgs == ("p1", "p2")
assert config.excludepkgs == ("p3", "p4")

@pytest.mark.skip("Not implemented")
def test_add_repository_replace(self):
"""Test the add_repository method with a replacement."""
data = RepoConfigurationData()
Expand All @@ -878,25 +877,6 @@ def test_add_repository_replace(self):
config = self._get_configuration("r1")
assert config.baseurl == ("http://u2",)

@pytest.mark.skip("Not implemented")
def test_remove_repository(self):
"""Test the remove_repository method."""
assert self.dnf_manager.repositories == []

self._add_repository("r1")
self._add_repository("r2")

assert self.dnf_manager.repositories == ["r1", "r2"]

self.dnf_manager.remove_repository("r1")
assert self.dnf_manager.repositories == ["r2"]

self.dnf_manager.remove_repository("r3")
assert self.dnf_manager.repositories == ["r2"]

self.dnf_manager.remove_repository("r2")
assert self.dnf_manager.repositories == []

def test_generate_repo_file_baseurl(self):
"""Test the generate_repo_file method with baseurl."""
data = RepoConfigurationData()
Expand Down

0 comments on commit 42e2d38

Please sign in to comment.