#!/usr/bin/python # Remove older versions of things from garchive directories. import os, stat, re, sys, getopt from garstowlib.utils import * arc_re = re.compile(r'^([A-Za-z0-9_+-]+)-([0-9\.]+[0-9a-z]*(?:-rc[0-9]+)?)\.(?:tar\.gz|tar\.bz2|zip)$') sig_re = re.compile(r'^(.*)\.(?:sig|asc|sign)$') def garchive_clean(dir, dryrun): files = {} for entry in os.listdir(dir): if stat.S_ISREG(os.stat(dir + "/" + entry).st_mode): files[entry] = True remove = {} # Find files where a newer version exists. found = {} for entry in files.keys(): m = arc_re.match(entry) if not m: continue (name, version) = m.group(1, 2) version = parse_version(version) l = found.setdefault(name, []) l.append((version, entry)) for l in found.values(): l.sort() for (version, entry) in l[:-1]: remove[entry] = l[-1][1] + " is newer" # Look for signature files where the file they sign no longer exists. for entry in files.keys(): m = sig_re.match(entry) if not m: continue sentry = m.group(1) if (sentry not in files) or (sentry in remove): remove[entry] = sentry + " does not exist" rentries = remove.keys() rentries.sort() for entry in rentries: if dryrun: action = "Would remove" else: action = "Removed" os.unlink(dir + "/" + entry) print action + " " + entry + " because " + remove[entry] if __name__ == "__main__": dryrun = False (opts, args) = getopt.getopt(sys.argv[1:], "n") for (o, a) in opts: if o == "-n": dryrun = True else: die("unknown argument: " + o) for dir in args: garchive_clean(dir, dryrun)