Stupid Fedora tricks: prune livecd-creator cache directory

Here’s a really dumb python script that prunes the livecd-creator cache directory – or, any directory with a big lump of RPM files in it, really. It finds (roughly, but we’re not going for the World Accuracy Prize here) multiple versions of the same package and removes all but the newest one (it relies on ls to order the files by date after ordering them by name, which it does here, but YMMV). Run it from the directory you want to prune. No guarantees given at all, may eat your babies.

#!/bin/python

from subprocess import check_output
import re
import os

sep = re.compile('-\d')

output = check_output('ls *x86_64.rpm', shell=True)
lastfile = ''
for rpmfile in output.split('\n'):
    if sep.split(rpmfile)[0] == sep.split(lastfile)[0]:
        os.remove(lastfile)
    lastfile = rpmfile

output = check_output('ls *noarch.rpm', shell=True)
lastfile = ''
for rpmfile in output.split('\n'):
    if sep.split(rpmfile)[0] == sep.split(lastfile)[0]:
        os.remove(lastfile)
    lastfile = rpmfile

output = check_output('ls *i686.rpm', shell=True)
lastfile = ''
for rpmfile in output.split('\n'):
    if sep.split(rpmfile)[0] == sep.split(lastfile)[0]:
        os.remove(lastfile)
    lastfile = rpmfile

2 Responses

  1. Pete Travis
    Pete Travis December 4, 2014 at 4:09 pm | | Reply

    You might enjoy https://apps.fedoraproject.org/packages/python-natsort for future sorting adventures. Also, parsing `ls` with python feels dirty – try on os.walk().

Leave a Reply to adamw Cancel reply

Your email address will not be published. Required fields are marked *