Skip to content Skip to sidebar Skip to footer

Silencing Yum Api Output

I am looking to 'silence' the debug level of the yum API in python. For example I am assigning two yum commands to variables like below; import yum yb = yum.YumBase() yum_conf = y

Solution 1:

All you need are these two lines,

yb.preconf.debuglevel = 0yb.preconf.errorlevel = 0

e.g., a python scipt, getpkg.py looks something like the following:

importyumyb= yum.YumBase()
  yb.preconf.debuglevel = 0
  yb.preconf.errorlevel = 0
  yb.install(name='emacs-nox')
  yb.resolveDeps()
  yb.processTransaction()

Result:

~]# python getpkg.py Installing:1:perl-parent-0.225-244.el7.noarch 0/8868 [1/32]
Installing:1:perl-parent-0.225-244.el7.noarch 144/8868 [1/32]
Installing:1:perl-parent-0.225-244.el7.noarch 2649/8868 [1/32]
Installing:1:perl-parent-0.225-244.el7.noarch 5686/8868 [1/32]
....
....

Solution 2:

Perfect scenario for a context manager. This is either being printed to stdout or stderr, the below technique works for either.

import io
import contextlib
import yum

f = io.StringIO()
with contextlib.redirect_stdout(f):  # or redirect_stderr... or both''' All the output in here is redirected to the filelike object, f '''

    yb = yum.YumBase()
    yum_conf = yb.conf.config_file_path
    package_list = yb.doPackageLists(pkgnarrow='updates', patterns='', 
        ignore_case=True)
s = f.getvalue()  # s now contains all that output you didn't want

Post a Comment for "Silencing Yum Api Output"