]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blob - wwwbin/clean-dups.py
Make ftp tools python3 ready
[projects/pld-ftp-admin.git] / wwwbin / clean-dups.py
1 #!/usr/bin/python
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, 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, e:
60                 print(e)
61                 # ignore non-files
62                 return 0
63         try:
64                 h1 = ts.hdrFromFdno(fd1)
65         except Exception, 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, 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         l1 = rpm.versionCompare(h1, h2)
83         l2 = rpm.versionCompare(h2, h1)
84
85         if l1 > 0 and l2 > 0:
86                 return 0
87
88         return -l1
89
90
91 def find_old(files):
92         return sorted(files, compare)
93
94 files = {}
95 dupes = {}
96
97 for file in os.listdir(dir):
98         if not re_rpm.match(file):
99                 continue
100
101         if ignore.match(file):
102                 continue
103
104         m = re_nvr.match(file)
105         if not m:
106                 print("problem with: %s" % file)
107                 sys.exit(1)
108
109         if len(sys.argv) == 0:
110                 p = os.path.join(dir, file)
111                 mtime = os.stat(p).st_mtime
112                 if mtime > time.time() - 3*86400:
113                         continue
114
115         name = m.group(1)
116
117         if files.has_key(name):
118                 if dupes.has_key(name):
119                         dupes[name].append(file)
120                 else:
121                         dupes[name] = [ files[name] ]
122                         dupes[name].append(file)
123         else:
124                 files[name] = file
125
126 for i in iter(dupes.keys()):
127         for old in find_old(dupes[i])[1:]:
128                 print("removing: %s" % old)
129                 os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old)
This page took 0.035942 seconds and 3 git commands to generate.