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