]> TLD Linux GIT Repositories - tld-ftp-admin.git/blob - wwwbin/clean-dups-archive.py
- raw from PLD
[tld-ftp-admin.git] / wwwbin / clean-dups-archive.py
1 #!/usr/bin/python
2 # arekm, 2008
3 # remove 
4
5 import os
6 import re
7 import time
8 import rpm
9 import sys
10
11 re_rpm = re.compile(r'.*\.rpm$')
12 re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$')
13 dir = '/home/pld/admins/th/ftp/.archive/PLD/SRPMS/RPMS'
14
15 ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD))
16
17 def 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
44 def find_old(files):
45         return sorted(files, compare)
46
47 files = {}
48 dupes = {}
49
50 for 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
76 for 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)