Skip to content

Commit

Permalink
Get repofile directories from dnf config, do not search subdirectories
Browse files Browse the repository at this point in the history
yum/dnf does not look into subdirectories, so leapp should not do that
either to avoid reporting false positives.
  • Loading branch information
drehak authored and pirat89 committed Jul 29, 2020
1 parent 48cc1eb commit 0c4d44c
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions repos/system_upgrade/el7toel8/libraries/repofileutils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import json
import os

from leapp.libraries.common import mounting, utils
from leapp.libraries.stdlib import api
from leapp.models import RepositoryFile, RepositoryData, fields

try:
import dnf
except ImportError:
api.current_logger().warn('repofileutils.py: failed to import dnf')


def _parse_repository(repoid, repo_data):
def asbool(x):
Expand Down Expand Up @@ -35,12 +42,26 @@ def parse_repofile(repofile):
return RepositoryFile(file=repofile, data=data)


def get_repodirs():
"""
Return all directories yum scans for repository files, if they exist.
By default, the possible paths on RHEL 7 should be:
['/etc/yum.repos.d', '/etc/yum/repos.d', '/etc/distro.repos.d']
ATTENTION: Requires the dnf module to be present.
TODO: Get repodirs inside given context.
"""
with dnf.base.Base() as base:
base.conf.read(priority=dnf.conf.PRIO_MAINCONFIG)
return list({os.path.realpath(d) for d in base.conf.reposdir if os.path.isdir(d)})


def get_parsed_repofiles(context=mounting.NotIsolatedActions(base_dir='/')):
"""
Scan all repositories on the system.
Repositories are scanned under /etc/yum.repos.d/ of the given context.
By default the context is the host system.
Repositories are scanned under repository directories (as reported by dnf)
of the given context. By default the context is the host system.
ATTENTION: Do not forget to ensure the redhat.repo file is regenerated
by RHSM when used.
Expand All @@ -50,7 +71,7 @@ def get_parsed_repofiles(context=mounting.NotIsolatedActions(base_dir='/')):
:rtype: List(RepositoryFile)
"""
repofiles = []
cmd = ['find', '/etc/yum.repos.d/', '-type', 'f', '-name', '*.repo']
cmd = ['find', '-L'] + get_repodirs() + ['-maxdepth', '1', '-type', 'f', '-name', '*.repo']
repofiles_paths = context.call(cmd, split=True)['stdout']
for repofile_path in repofiles_paths:
repofile = parse_repofile(context.full_path(repofile_path))
Expand Down

0 comments on commit 0c4d44c

Please sign in to comment.