@@ -810,33 +810,29 @@ def add_repository(self, data: RepoConfigurationData):
810
810
811
811
:param RepoConfigurationData data: a repo configuration
812
812
"""
813
+ repositories = libdnf5 .repo .RepoQuery (self ._base )
814
+ repositories .filter_id (data .name )
813
815
814
816
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 )
826
823
827
824
log .info ("Added the '%s' repository: %s" , repo .get_id (), repo )
828
825
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.
831
828
829
+ :param libdnf5.repo.Repo repo:existing repository
832
830
:param RepoConfigurationData data: a repo configuration
833
- return dnf .repo.Repo: a DNF repository
831
+ return libdnf5 .repo.Repo: a DNF repository
834
832
"""
835
833
if self ._repositories_loaded :
836
834
raise RuntimeError ("Cannot create a new repository. Repositories were already loaded." )
837
835
838
- repo_sack = self ._base .get_repo_sack ()
839
- repo = repo_sack .create_repo (data .name )
840
836
config = repo .get_config ()
841
837
842
838
# Disable the repo if requested.
@@ -887,6 +883,19 @@ def _create_repository(self, data: RepoConfigurationData):
887
883
888
884
return repo
889
885
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
+
890
899
def generate_repo_file (self , data : RepoConfigurationData ):
891
900
"""Generate a content of the .repo file.
892
901
0 commit comments