Tuesday, January 4, 2022

[SOLVED] Get the size of an upgrade with yum command

Issue

I would like to get the size of a linux upgrades with yum.

For apt I have done this:

`def get_upgradable() : #récupère la list des paquets
            command = "apt list --upgradable 2>/dev/null"
            process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, encoding='utf8')
            packages_name = []
            for out in process.stdout :
                if "/" in out :
                    packages_name.append(out[:out.index('/')])
            return packages_name

        def get_size(*args, return_somme=True) : #fait la somme du poids de chaque paquets
            command = "apt-cache --no-all-versions show {pkg} | grep \"^Size\" | cut -d' ' -f2"
            sizes = []
            somme = 0
            for pkg in args :
                size = int(
                    subprocess.Popen(command.format(pkg=pkg), shell=True, stdout=subprocess.PIPE, encoding='utf8'
                    ).stdout.read())
                sizes.append(size)
                somme += size
            if return_somme : return somme
            return sizes

        somme=get_size(*get_upgradable())`

And I try this but I don't know, how to get only the size :

yum check-update | awk '/\S+\s+[0-9]\S+\s+\S+/ {print $1 }' > updates

Solution

So with a friend we work on that and upgrade the solution. Reminder : I send a python sript on server to get the upgrade size.

def get_upgradable() :
        if os.path.isfile("/usr/bin/apt"):
            command = "apt list --upgradable 2>/dev/null | cut -d'/' -f1"
        elif os.path.isfile("/usr/bin/yum"):
            #command = "yum check-update 2>/dev/null | grep \".x86_64\" | cut -d' ' -f1"
            command="yum check-update | awk '/\S+\s+[0-9]\S+\s+\S+/ {print $1 }'"
        else :
            raise PkgManager("Not found package manager")
            return None
        process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, encoding='utf8')
        packages_name = []
        for out in process.stdout :
            packages_name.append(out[:-1])
        return packages_name

def get_yum_size(*args, return_somme=True) :
    command = "yum info {pkg} | egrep \"(Taille|Size)\""
    factor = {"k" : 1000,
              "M" : 1000000,
              "G" : 1000000000}
    somme = 0
    sizes = []
    for pkg in args :
        lines = subprocess.Popen(command.format(pkg=pkg),
                                 shell=True,
                                 stdout=subprocess.PIPE,
                                 encoding='utf8',
                                ).stdout.readlines()
        infos = lines[-1][:-1].split(' ')
        size = float(infos[-2])*factor[infos[-1]]
        sizes.append(size)
        somme += size
    if return_somme : return somme
    return sizes

You call it with :

def get_size(*args, **kwargs) :
    if os.path.isfile("/usr/bin/apt"):
        return get_apt_size(*args, **kwargs)
    elif os.path.isfile("/usr/bin/yum"):
        return get_yum_size(*args, **kwargs)
    else :
        raise PkgManager("Not found package manager")

And in main :

get_size(*get_upgradable())


Answered By - bastien