#!/usr/bin/python # arekm, 2008 # remove import os import re import time import rpm import sys re_info = re.compile(r'.*\.info$') re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.info$') dir = '/home/pld/admins/th/ftp/test/SRPMS/.metadata' def compare(f1, f2): m1 = re_nvr.match(f1) n1 = m1.group(1) v1 = m1.group(2) r1 = m1.group(3) m2 = re_nvr.match(f2) n2 = m2.group(1) v2 = m2.group(2) r2 = m2.group(3) l1 = rpm.labelCompare((n1, v1, r1), (n2, v2, r2)) l2 = rpm.labelCompare((n2, v2, r2), (n1, v1, r1)) if l1 > 0 and l2 > 0: return 0 return -l1 def find_old(files): return sorted(files, compare) files = {} dupes = {} for file in os.listdir(dir): if not re_info.match(file): continue m = re_nvr.match(file) if not m: print "problem with: %s" % file sys.exit(1) if len(sys.argv) == 0: p = os.path.join(dir, file) mtime = os.stat(p).st_mtime if mtime > time.time() - 3*86400: continue name = m.group(1) if files.has_key(name): if dupes.has_key(name): dupes[name].append(file) else: dupes[name] = [ files[name] ] dupes[name].append(file) else: files[name] = file for i in dupes.iterkeys(): for old in find_old(dupes[i])[1:]: os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old)