richardphibel / rpms / dnf

Forked from rpms/dnf 2 years ago
Clone
Blob Blame History Raw
From 8bcd196fd95e70fd1f0be16d2c274e39a1cabe2e Mon Sep 17 00:00:00 2001
From: Jaroslav Rohel <jrohel@redhat.com>
Date: Thu, 21 Nov 2019 11:45:03 +0100
Subject: [PATCH] Do a substitution of variables in repo_id (RhBug:1748841)

Example of repo file:
[test-$basearch-$releasever]
Name=Test-$basearch-$releasever
baseurl=file:///mnt/
gpgcheck=0
enabled=1

https://bugzilla.redhat.com/show_bug.cgi?id=1748841
---
 dnf/conf/read.py | 40 +++++++++++++++++++++++++++-------------
 1 file changed, 27 insertions(+), 13 deletions(-)

diff --git a/dnf/conf/read.py b/dnf/conf/read.py
index a526a71..1efac22 100644
--- a/dnf/conf/read.py
+++ b/dnf/conf/read.py
@@ -43,7 +43,7 @@ class RepoReader(object):
 
         # read .repo files from directories specified by conf.reposdir
         for repofn in (repofn for reposdir in self.conf.reposdir
-                       for repofn in sorted(glob.glob('%s/*.repo' % reposdir))):
+                       for repofn in sorted(glob.glob('{}/*.repo'.format(reposdir)))):
             try:
                 for r in self._get_repos(repofn):
                     yield r
@@ -54,17 +54,38 @@ class RepoReader(object):
     def _build_repo(self, parser, id_, repofn):
         """Build a repository using the parsed data."""
 
-        repo = dnf.repo.Repo(id_, self.conf)
+        substituted_id = libdnf.conf.ConfigParser.substitute(id_, self.conf.substitutions)
+
+        # Check the repo.id against the valid chars
+        invalid = dnf.repo.repo_id_invalid(substituted_id)
+        if invalid is not None:
+            if substituted_id != id_:
+                msg = _("Bad id for repo: {} ({}), byte = {} {}").format(substituted_id, id_,
+                                                                         substituted_id[invalid],
+                                                                         invalid)
+            else:
+                msg = _("Bad id for repo: {}, byte = {} {}").format(id_, id_[invalid], invalid)
+            raise dnf.exceptions.ConfigError(msg)
+
+        repo = dnf.repo.Repo(substituted_id, self.conf)
         try:
             repo._populate(parser, id_, repofn, dnf.conf.PRIO_REPOCONFIG)
         except ValueError as e:
-            msg = _("Repository '%s': Error parsing config: %s") % (id_, e)
+            if substituted_id != id_:
+                msg = _("Repository '{}' ({}): Error parsing config: {}").format(substituted_id,
+                                                                                 id_, e)
+            else:
+                msg = _("Repository '{}': Error parsing config: {}").format(id_, e)
             raise dnf.exceptions.ConfigError(msg)
 
         # Ensure that the repo name is set
         if repo._get_priority('name') == dnf.conf.PRIO_DEFAULT:
-            msg = _("Repository '%s' is missing name in configuration, using id.")
-            logger.warning(msg, id_)
+            if substituted_id != id_:
+                msg = _("Repository '{}' ({}) is missing name in configuration, using id.").format(
+                    substituted_id, id_)
+            else:
+                msg = _("Repository '{}' is missing name in configuration, using id.").format(id_)
+            logger.warning(msg)
         repo.name = ucd(repo.name)
         repo._substitutions.update(self.conf.substitutions)
         repo.cfg = parser
@@ -80,23 +101,16 @@ class RepoReader(object):
         try:
             parser.read(repofn)
         except RuntimeError as e:
-            raise dnf.exceptions.ConfigError(_('Parsing file "%s" failed: %s') % (repofn, e))
+            raise dnf.exceptions.ConfigError(_('Parsing file "{}" failed: {}').format(repofn, e))
         except IOError as e:
             logger.warning(e)
 
         # Check sections in the .repo file that was just slurped up
         for section in parser.getData():
 
             if section == 'main':
                 continue
 
-            # Check the repo.id against the valid chars
-            invalid = dnf.repo.repo_id_invalid(section)
-            if invalid is not None:
-                logger.warning(_("Bad id for repo: %s, byte = %s %d"), section,
-                               section[invalid], invalid)
-                continue
-
             try:
                 thisrepo = self._build_repo(parser, ucd(section), repofn)
             except (dnf.exceptions.RepoError, dnf.exceptions.ConfigError) as e:
--
libgit2 0.28.2