3209008
# A plugin for yum which only leaves n 'kernel' packages installed instead
3209008
# of infinitely doing installonly
3209008
#
3209008
# Copyright 2005  Red Hat, Inc.
3209008
# Jeremy Katz <katzj@redhat.com>
3209008
#
3209008
# This program is free software; you can redistribute it and/or modify
3209008
# it under the terms of the GNU General Public License as published by
3209008
# the Free Software Foundation; either version 2 of the License, or
3209008
# (at your option) any later version.
3209008
#
3209008
# This program is distributed in the hope that it will be useful,
3209008
# but WITHOUT ANY WARRANTY; without even the implied warranty of
3209008
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3209008
# GNU General Public License for more details.
3209008
#
42feee9
# version 0.93
3209008
3209008
import os
3209008
from rpmUtils import miscutils
f539ae2
from yum.plugins import TYPE_CORE
def9aac
from yum.packages import YumInstalledPackage, comparePoEVR
3209008
def9aac
requires_api_version = '2.4'
f539ae2
plugin_type = TYPE_CORE
3209008
3209008
def get_running_kernel_version_release():
3209008
    """This takes the output of uname and figures out the (version, release)
3209008
    tuple for the running kernel."""
3209008
    ver = os.uname()[2]
37d3975
    for s in ("bigmem", "enterprise", "smp", "hugemem", "PAE", "rt",
42feee9
              "guest", "hypervisor", "xen0", "xenU", "xen", "debug"):
3209008
        if ver.endswith(s):
3209008
            ver = ver.replace(s, "")
2efbe72
    if ver.find("-") != -1:
2efbe72
        (v, r) = ver.split("-", 1)
74e4bb4
        return (v, r)
74e4bb4
    return (None, None)
3209008
3209008
def config_hook(conduit):
3209008
    global num_tokeep
3209008
    num_tokeep = conduit.confInt('main', 'tokeep', default=2)
3209008
    
3209008
def postresolve_hook(conduit):
3209008
    ts = conduit.getTsInfo()
3209008
    rpmdb = conduit.getRpmDB()
3209008
    conf = conduit.getConf()
3209008
    mems = ts.getMembers()
3209008
    toremove = []
3209008
    for instpkg in conf.installonlypkgs:
3209008
        for m in mems:
def9aac
            if (m.name == instpkg or instpkg in m.po.provides_names) \
01d2082
                   and m.ts_state in ('i', 'u'):
def9aac
                installed = rpmdb.searchNevra(name=m.name)
3209008
                if len(installed) >= num_tokeep - 1: # since we're adding one
3209008
                    numleft = len(installed) - num_tokeep + 1
3209008
                    (curv, curr) = get_running_kernel_version_release()
3209008
                    
def9aac
                    installed.sort(comparePoEVR)
def9aac
                    for po in installed:
def9aac
                        if (po.version, po.release) == (curv, curr): 
def9aac
                            # don't remove running
3209008
                            continue
3209008
                        if numleft == 0:
3209008
                            break
def9aac
                        toremove.append(po)
4509fbe
                        numleft -= 1
3209008
                        
3209008
    map(lambda x: ts.addErase(x), toremove)