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