Blame dnf/conf/read.py

Packit 6f3914
# read.py
Packit 6f3914
# Reading configuration from files.
Packit 6f3914
#
Packit 6f3914
# Copyright (C) 2014-2017 Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
# This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
# the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
# Public License for more details.  You should have received a copy of the
Packit 6f3914
# GNU General Public License along with this program; if not, write to the
Packit 6f3914
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
# source code or documentation are not subject to the GNU General Public
Packit 6f3914
# License and may only be used or replicated with the express permission of
Packit 6f3914
# Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
Packit 6f3914
from __future__ import absolute_import
Packit 6f3914
from __future__ import unicode_literals
Packit 6f3914
from dnf.i18n import _, ucd
Packit 6f3914
import dnf.conf
Packit 6f3914
import libdnf.conf
Packit 6f3914
import dnf.exceptions
Packit 6f3914
import dnf.repo
Packit 6f3914
import glob
Packit 6f3914
import logging
Packit 6f3914
Packit 6f3914
logger = logging.getLogger('dnf')
Packit 6f3914
Packit 6f3914
Packit 6f3914
class RepoReader(object):
Packit 6f3914
    def __init__(self, conf, opts):
Packit 6f3914
        self.conf = conf
Packit 6f3914
        self.opts = opts
Packit 6f3914
Packit 6f3914
    def __iter__(self):
Packit 6f3914
        # get the repos from the main yum.conf file
Packit 6f3914
        for r in self._get_repos(self.conf.config_file_path):
Packit 6f3914
            yield r
Packit 6f3914
Packit 6f3914
        # read .repo files from directories specified by conf.reposdir
Packit 6f3914
        for repofn in (repofn for reposdir in self.conf.reposdir
Packit e1b857
                       for repofn in sorted(glob.glob('{}/*.repo'.format(reposdir)))):
Packit 6f3914
            try:
Packit 6f3914
                for r in self._get_repos(repofn):
Packit 6f3914
                    yield r
Packit 6f3914
            except dnf.exceptions.ConfigError:
Packit 6f3914
                logger.warning(_("Warning: failed loading '%s', skipping."),
Packit 6f3914
                               repofn)
Packit 6f3914
Packit 6f3914
    def _build_repo(self, parser, id_, repofn):
Packit 6f3914
        """Build a repository using the parsed data."""
Packit 6f3914
Packit e1b857
        substituted_id = libdnf.conf.ConfigParser.substitute(id_, self.conf.substitutions)
Packit e1b857
Packit e1b857
        # Check the repo.id against the valid chars
Packit e1b857
        invalid = dnf.repo.repo_id_invalid(substituted_id)
Packit e1b857
        if invalid is not None:
Packit e1b857
            if substituted_id != id_:
Packit e1b857
                msg = _("Bad id for repo: {} ({}), byte = {} {}").format(substituted_id, id_,
Packit e1b857
                                                                         substituted_id[invalid],
Packit e1b857
                                                                         invalid)
Packit e1b857
            else:
Packit e1b857
                msg = _("Bad id for repo: {}, byte = {} {}").format(id_, id_[invalid], invalid)
Packit e1b857
            raise dnf.exceptions.ConfigError(msg)
Packit e1b857
Packit e1b857
        repo = dnf.repo.Repo(substituted_id, self.conf)
Packit 6f3914
        try:
Packit 6f3914
            repo._populate(parser, id_, repofn, dnf.conf.PRIO_REPOCONFIG)
Packit 6f3914
        except ValueError as e:
Packit e1b857
            if substituted_id != id_:
Packit e1b857
                msg = _("Repository '{}' ({}): Error parsing config: {}").format(substituted_id,
Packit e1b857
                                                                                 id_, e)
Packit e1b857
            else:
Packit e1b857
                msg = _("Repository '{}': Error parsing config: {}").format(id_, e)
Packit 6f3914
            raise dnf.exceptions.ConfigError(msg)
Packit 6f3914
Packit 6f3914
        # Ensure that the repo name is set
Packit 6f3914
        if repo._get_priority('name') == dnf.conf.PRIO_DEFAULT:
Packit e1b857
            if substituted_id != id_:
Packit e1b857
                msg = _("Repository '{}' ({}) is missing name in configuration, using id.").format(
Packit e1b857
                    substituted_id, id_)
Packit e1b857
            else:
Packit e1b857
                msg = _("Repository '{}' is missing name in configuration, using id.").format(id_)
Packit e1b857
            logger.warning(msg)
Packit 6f3914
        repo.name = ucd(repo.name)
Packit 6f3914
        repo._substitutions.update(self.conf.substitutions)
Packit 6f3914
        repo.cfg = parser
Packit 6f3914
Packit 6f3914
        return repo
Packit 6f3914
Packit 6f3914
    def _get_repos(self, repofn):
Packit 6f3914
        """Parse and yield all repositories from a config file."""
Packit 6f3914
Packit 6f3914
        substs = self.conf.substitutions
Packit 6f3914
        parser = libdnf.conf.ConfigParser()
Packit 6f3914
        parser.setSubstitutions(substs)
Packit 6f3914
        try:
Packit 6f3914
            parser.read(repofn)
Packit 6f3914
        except RuntimeError as e:
Packit e1b857
            raise dnf.exceptions.ConfigError(_('Parsing file "{}" failed: {}').format(repofn, e))
Packit 6f3914
        except IOError as e:
Packit 6f3914
            logger.warning(e)
Packit 6f3914
Packit 6f3914
        # Check sections in the .repo file that was just slurped up
Packit 6f3914
        for section in parser.getData():
Packit 6f3914
Packit 6f3914
            if section == 'main':
Packit 6f3914
                continue
Packit 6f3914
Packit 6f3914
            try:
Packit 6f3914
                thisrepo = self._build_repo(parser, ucd(section), repofn)
Packit 6f3914
            except (dnf.exceptions.RepoError, dnf.exceptions.ConfigError) as e:
Packit 6f3914
                logger.warning(e)
Packit 6f3914
                continue
Packit 6f3914
            else:
Packit 6f3914
                thisrepo.repofile = repofn
Packit 6f3914
Packit 6f3914
            thisrepo._configure_from_options(self.opts)
Packit 6f3914
Packit 6f3914
            yield thisrepo