Blame dnf/conf/read.py

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