Skip to content

Commit 42e2d38

Browse files
committed
Implement replacing repositories
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.
1 parent 49f9c11 commit 42e2d38

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

pyanaconda/modules/payloads/payload/dnf/dnf_manager.py

+25-16
Original file line numberDiff line numberDiff line change
@@ -810,33 +810,29 @@ def add_repository(self, data: RepoConfigurationData):
810810
811811
:param RepoConfigurationData data: a repo configuration
812812
"""
813+
repositories = libdnf5.repo.RepoQuery(self._base)
814+
repositories.filter_id(data.name)
813815

814816
with self._lock:
815-
# Create a new repository.
816-
repo = self._create_repository(data)
817-
818-
# FIXME: How to handle existing repositories?
819-
# Remove an existing repository.
820-
#
821-
# if repo.id in self._base.repos:
822-
# self._base.repos.pop(repo.id)
823-
824-
# Add the new repository.
825-
#self._base.repos.add(repo)
817+
if repositories.empty():
818+
# Create a new repository.
819+
repo = self._create_repository(data)
820+
else:
821+
# Replace the existing repository with a new one.
822+
repo = self._configure_repository(repositories.get(), data)
826823

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

829-
def _create_repository(self, data: RepoConfigurationData):
830-
"""Create a DNF repository.
826+
def _configure_repository(self, repo: libdnf5.repo.Repo, data: RepoConfigurationData):
827+
"""Configure a DNF repository.
831828
829+
:param libdnf5.repo.Repo repo:existing repository
832830
:param RepoConfigurationData data: a repo configuration
833-
return dnf.repo.Repo: a DNF repository
831+
return libdnf5.repo.Repo: a DNF repository
834832
"""
835833
if self._repositories_loaded:
836834
raise RuntimeError("Cannot create a new repository. Repositories were already loaded.")
837835

838-
repo_sack = self._base.get_repo_sack()
839-
repo = repo_sack.create_repo(data.name)
840836
config = repo.get_config()
841837

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

888884
return repo
889885

886+
def _create_repository(self, data: RepoConfigurationData):
887+
"""Create a DNF repository.
888+
889+
:param RepoConfigurationData data: a repo configuration
890+
return libdnf5.repo.Repo: a DNF repository
891+
"""
892+
if self._repositories_loaded:
893+
raise RuntimeError("Cannot create a new repository. Repositories were already loaded.")
894+
895+
repo = self._base.get_repo_sack().create_repo(data.name)
896+
897+
return self._configure_repository(repo, data)
898+
890899
def generate_repo_file(self, data: RepoConfigurationData):
891900
"""Generate a content of the .repo file.
892901

tests/unit_tests/pyanaconda_tests/modules/payloads/payload/test_module_payload_dnf5_manager.py

-20
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,6 @@ def test_add_repository_packages(self):
861861
assert config.includepkgs == ("p1", "p2")
862862
assert config.excludepkgs == ("p3", "p4")
863863

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

881-
@pytest.mark.skip("Not implemented")
882-
def test_remove_repository(self):
883-
"""Test the remove_repository method."""
884-
assert self.dnf_manager.repositories == []
885-
886-
self._add_repository("r1")
887-
self._add_repository("r2")
888-
889-
assert self.dnf_manager.repositories == ["r1", "r2"]
890-
891-
self.dnf_manager.remove_repository("r1")
892-
assert self.dnf_manager.repositories == ["r2"]
893-
894-
self.dnf_manager.remove_repository("r3")
895-
assert self.dnf_manager.repositories == ["r2"]
896-
897-
self.dnf_manager.remove_repository("r2")
898-
assert self.dnf_manager.repositories == []
899-
900880
def test_generate_repo_file_baseurl(self):
901881
"""Test the generate_repo_file method with baseurl."""
902882
data = RepoConfigurationData()

0 commit comments

Comments
 (0)