]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - wwwbin/clean-dups-archive.py
fix arg passing
[projects/pld-ftp-admin.git] / wwwbin / clean-dups-archive.py
CommitLineData
ed1ec331
ER
1#!/usr/bin/python
2# arekm, 2008
3# remove
4
5import os
6import re
7import time
8import rpm
9import sys
10
11re_rpm = re.compile(r'.*\.rpm$')
12re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$')
13dir = '/home/pld/admins/th/ftp/.archive/PLD/SRPMS/RPMS'
14
6cb7417a 15ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD))
ed1ec331
ER
16
17def compare(f1, f2):
18 try:
19 fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY)
20 except Exception, e:
21 print e
22 # ignore non-files
23 return 0
24 try:
25 fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY)
26 except Exception, e:
27 print e
28 # ignore non-files
29 return 0
30 h1 = ts.hdrFromFdno(fd1)
31 h2 = ts.hdrFromFdno(fd2)
32 os.close(fd1)
33 os.close(fd2)
34
35 l1 = rpm.versionCompare(h1, h2)
36 l2 = rpm.versionCompare(h2, h1)
37
38 if l1 > 0 and l2 > 0:
39 return 0
40
41 return -l1
42
43
44def find_old(files):
45 return sorted(files, compare)
46
47files = {}
48dupes = {}
49
50for file in os.listdir(dir):
51 if not re_rpm.match(file):
52 continue
53
54 m = re_nvr.match(file)
55 if not m:
56 print "problem with: %s" % file
57 sys.exit(1)
58
59 if len(sys.argv) == 0:
60 p = os.path.join(dir, file)
61 mtime = os.stat(p).st_mtime
62 if mtime > time.time() - 3*86400:
63 continue
64
65 name = m.group(1)
66
67 if files.has_key(name):
68 if dupes.has_key(name):
69 dupes[name].append(file)
70 else:
71 dupes[name] = [ files[name] ]
72 dupes[name].append(file)
73 else:
74 files[name] = file
75
76for i in dupes.iterkeys():
77 for old in find_old(dupes[i])[1:]:
78 os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py .archive/PLD %s" % old)
This page took 0.615894 seconds and 4 git commands to generate.