0

Is there a way to prevent the yum API from creating output when it runs?

For instance, if I run this simple code (with yum-3.4.3-132.el7.centos.0.1):

import yum
yb = yum.YumBase()
yb.repos.populateSack(mdtype='metadata', cacheonly=0)

I get the following output:

Loaded plugins: fastestmirror

I don't want to blackhole all output, just the output from the yum API.

sosiouxme
  • 1,226
  • 16
  • 26

2 Answers2

1

Piggybacking on thescouser89's answer here.

I'm not sure if this is an exhaustive list of every Yum logger, but I think it's pretty close. You can disable all of them before you start calling into Yum and it will be completely silent.

import logging
from yum.logginglevels import __NO_LOGGING

yumLoggers = ['yum.filelogging.RPMInstallCallback','yum.verbose.Repos', 'yum.verbose.plugin', 
'yum.Depsolve', 'yum.verbose', 'yum.plugin', 'yum.Repos', 'yum', 'yum.verbose.YumBase', 
'yum.filelogging', 'yum.verbose.YumPlugins', 'yum.RepoStorage', 'yum.YumBase', 
'yum.filelogging.YumBase', 'yum.verbose.Depsolve']

for loggerName in yumLoggers:
    logger = logging.getLogger(loggerName)
    logger.setLevel(__NO_LOGGING)

You can also override some of the progress bars / event loggers from the various RPM transactions by making a class that inherits RPMBaseCallback and passing that into the various transaction functions (processTransaction, etc). However, if you're disabling all the loggers as I've done above, those functions will not be called.

Edit: Upon further inspection, I think the simplest answer is: https://stackoverflow.com/a/43625141/619255

However, this approach doesn't silence warnings about incomplete transactions, while disabling all of the loggers does.

Robert Kelly
  • 996
  • 1
  • 10
  • 19
-1

I looked into the RHEL7 yum code and I believe you can do that in your code like so:

import logging
logger = logging.getLogger("yum.verbose.YumPlugins")
# Only print critical logs from yum
logger.setLevel(logging.CRITICAL)

# The "Loaded plugins:" text is printed as a debug. So anything above
# logging.DEBUG should silence it
thescouser89
  • 111
  • 1
  • 2