Skip to content

Commit

Permalink
Use YumBase instead of YumBaseCli to get pkg updates
Browse files Browse the repository at this point in the history
YumBaseCli was causing the logging of convert2rhel to be broken - after
instantiating YumBaseCli every log message was duplicate on the output.

The reason is that initialization of the YumBaseCli class instance includes
calling logging.basicConfig() which sets up handlers on the root logger.

It is possible to use the YumBase class, which does not affect
logging, to get the same data - list of packages having updates
available.

The YumBaseCli logging handlers interfered with the convert2rhel logging as
we use propage=True to be able to use caplog in our unit tests:
#179
  • Loading branch information
bocekm committed May 13, 2022
1 parent 68bf0ac commit 905dc2c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
22 changes: 5 additions & 17 deletions convert2rhel/pkghandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,18 +1024,9 @@ def get_total_packages_to_update():


def _get_packages_to_update_yum():
"""Query all the packages with yum that has an update pending on the
system."""
# Check first if this path is not in the `sys.path`
if "/usr/share/yum-cli" not in sys.path:
# Yum's installation path for the CLI code
# https://github.com/rpm-software-management/yum/blob/4ed25525ee4781907bd204018c27f44948ed83fe/bin/yum#L26
sys.path.insert(0, "/usr/share/yum-cli")

from cli import YumBaseCli # pylint: disable=E0401

base = YumBaseCli()
packages = base.returnPkgLists(["updates"], None)
"""Query all the packages with yum that has an update pending on the system."""
base = pkgmanager.YumBase()
packages = base.doPackageLists(pkgnarrow="updates")
all_packages = []
for package in packages.updates:
all_packages.append(package.name)
Expand All @@ -1044,12 +1035,9 @@ def _get_packages_to_update_yum():


def _get_packages_to_update_dnf():
"""Query all the packages with dnf that has an update pending on the
system."""
from dnf import Base # pylint: disable=E0401

"""Query all the packages with dnf that has an update pending on the system."""
packages = []
base = Base()
base = pkgmanager.Base()
# Fix the case when we are trying to query packages in Oracle Linux 8
# when the DNF API gets called, those variables are not populated by default.
if system_info.id == "oracle":
Expand Down
8 changes: 2 additions & 6 deletions convert2rhel/unit_tests/pkghandler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,8 +1502,7 @@ def test_get_total_packages_to_update(package_manager_type, packages, monkeypatc
)
@pytest.mark.parametrize(("packages"), ((["package-1", "package-2", "package-3"],)))
def test_get_packages_to_update_yum(packages, monkeypatch):
sys.path.insert(0, "/usr/share/yum-cli")
from cli import YumBaseCli # pylint: disable=E0401
from yum import YumBase # pylint: disable=E0401

PkgName = namedtuple("PkgNames", ["name"])
PkgUpdates = namedtuple("PkgUpdates", ["updates"])
Expand All @@ -1513,11 +1512,8 @@ def test_get_packages_to_update_yum(packages, monkeypatch):

pkg_lists_mock = mock.Mock(return_value=PkgUpdates(transaction_pkgs))

monkeypatch.setattr(YumBaseCli, "returnPkgLists", value=pkg_lists_mock)
monkeypatch.setattr(YumBase, "doPackageLists", value=pkg_lists_mock)

# Remvoe the /usr/share/yum-cli from the system path so the code
# can load it
sys.path.remove("/usr/share/yum-cli")
assert _get_packages_to_update_yum() == packages


Expand Down

0 comments on commit 905dc2c

Please sign in to comment.