]> TLD Linux GIT Repositories - tld-ftp-admin.git/blob - wwwbin/clean-dups.py
- raw from PLD
[tld-ftp-admin.git] / wwwbin / clean-dups.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/test/SRPMS/RPMS'
14
15 ignore = re.compile('^(kernel-.*)$')
16 #|\
17 #crash-.*|\
18 #dahdi-linux-.*|\
19 #e1000e-.*|\
20 #igb-.*|\
21 #ipset-.*|\
22 #iscsitarget-.*|\
23 #ixgbe-.*|\
24 #kernel-net-wl-.*|\
25 #lin_tape-.*|\
26 #linux-fusion-.*|\
27 #linuxrdac-.*|\
28 #lirc-.*|\
29 #lttng-modules-.*|\
30 #madwifi-ng-.*|\
31 #nvidiabl-.*|\
32 #open-vm-tools-.*|\
33 #openvswitch-.*|\
34 #r8168-.*|\
35 #spl-.*|\
36 #tpm_emulator-.*|\
37 #VirtualBox-.*|\
38 #vpb-driver-.*|\
39 #xorg-driver-video-fglrx-.*|\
40 #xorg-driver-video-fglrx-legacy-.*|\
41 #xorg-driver-video-nvidia-.*|\
42 #xorg-driver-video-nvidia-legacy3-.*|\
43 #xorg-driver-video-nvidia-legacy-304xx-.*|\
44 #xtables-addons-.*)$')
45
46 ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD))
47
48 def compare(f1, f2):
49         try:
50                 fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY)
51         except Exception, e:
52                 print e
53                 # ignore non-files
54                 return 0
55         try:
56                 fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY)
57         except Exception, e:
58                 print e
59                 # ignore non-files
60                 return 0
61         try:
62                 h1 = ts.hdrFromFdno(fd1)
63         except Exception, e:
64                 print "hdrFromFdno for %s failed: %s" % (f1, e)
65                 os.close(fd1)
66                 os.close(fd2)
67                 return 0
68
69         try:
70                 h2 = ts.hdrFromFdno(fd2)
71         except Exception, e:
72                 print "hdrFromFdno for %s failed: %s" % (f2, e)
73                 os.close(fd1)
74                 os.close(fd2)
75                 return 0
76
77         os.close(fd1)
78         os.close(fd2)
79
80         l1 = rpm.versionCompare(h1, h2)
81         l2 = rpm.versionCompare(h2, h1)
82
83         if l1 > 0 and l2 > 0:
84                 return 0
85
86         return -l1
87
88
89 def find_old(files):
90         return sorted(files, compare)
91
92 files = {}
93 dupes = {}
94
95 for file in os.listdir(dir):
96         if not re_rpm.match(file):
97                 continue
98
99         if ignore.match(file):
100                 continue
101
102         m = re_nvr.match(file)
103         if not m:
104                 print "problem with: %s" % file
105                 sys.exit(1)
106
107         if len(sys.argv) == 0:
108                 p = os.path.join(dir, file)
109                 mtime = os.stat(p).st_mtime
110                 if mtime > time.time() - 3*86400:
111                         continue
112
113         name = m.group(1)
114
115         if files.has_key(name):
116                 if dupes.has_key(name):
117                         dupes[name].append(file)
118                 else:
119                         dupes[name] = [ files[name] ]
120                         dupes[name].append(file)
121         else:
122                 files[name] = file
123
124 for i in dupes.iterkeys():
125         for old in find_old(dupes[i])[1:]:
126                 print "removing: %s" % old
127                 os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old)