1

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 = yb.conf.config_file_path
package_list = yb.doPackageLists(pkgnarrow='updates', patterns='', ignore_case=True)

When running the script it comes back with the following for CentOS 7:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.sov.uk.goscomb.net
* extras: mirror.sov.uk.goscomb.net
* updates: mirror.sov.uk.goscomb.net

And then on CentOS 6:

Loaded plugins: fastestmirror
Determining fastest mirrors

I don't want this verbose level of printing. I'm thinking this is something to do with the logging level, but am at cross path as to how to alter this.

Apenketh
  • 13
  • 4

2 Answers2

1

All you need are these two lines,

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

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

  import yum

  yb = 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]
....
....
iamauser
  • 11,119
  • 5
  • 34
  • 52
  • This is perfect, I used just the debug level one which silenced the output. Thanks. – Apenketh Apr 26 '17 at 06:19
  • 1
    iamauser - I already have but take note of the following: Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. – Apenketh Apr 26 '17 at 14:26
0

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
RobertB
  • 1,879
  • 10
  • 17