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

Comments

Pete Travis wrote on 2014-12-05 00:09:
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().
adamw wrote on 2014-12-05 00:14:
Oh, it's incredibly dirty. It's a dirty hack. (Though actually what happened is I was writing it a slightly different way as a bash script, then decided it'd be easier in Python so I just converted it, then I changed the approach to one I probably could've done in bash anyway. ah, well.) I have a mental 'hobby project' threshold: until something crosses that, it is by default a 'dirty hack', which means I write it until it works and then I stop. If it gets complex/interesting enough to become a 'hobby project', it gets cleaned up. If it doesn't, it stays exactly as dirty as it was when it started working. =) natsort looks like just the right thing if I needed to do this properly, though, indeed - thanks!