Sunday, February 27, 2022

[SOLVED] Check yum security updates using python

Issue

Using the following code I can list all packages that have an update available:

import yum

base = yum.YumBase()
package_list = base.doPackageLists(pkgnarrow='updates', patterns='', ignore_case=True)

if package_list.updates:
  for pkg in package_list.updates:
    print(pkg)

but what I actually want is to list security updates only (equivalent of yum check-update --security).

Unfortunately I cannot find proper documentation about the yum library. Can someone help me in the right direction?


Solution

See the following script.

Short answer, you should use UpdateMetadata to filter the results based on the metadata of each notice.

import yum
from yum.update_md import UpdateMetadata
y = yum.YumBase()
ygh = y.doPackageLists('updates')    
x = UpdateMetadata()

for i in ygh.updates:
    md = x.get_notice((i.name, i.ver, i.rel))
    if md:
        md = md.get_metadata()
        if sec_only:
            if md['type'] != 'security':
                continue

(the code above is just a portion of the linked code, with the relevant bits)



Answered By - D. Berebi
Answer Checked By - Willingham (WPSolving Volunteer)