]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - wwwbin/clean-dups.py
Fix exception handling syntax (python3 compat)
[projects/pld-ftp-admin.git] / wwwbin / clean-dups.py
CommitLineData
ed1ec331
ER
1#!/usr/bin/python
2# arekm, 2008
3# remove
4
94960bbc
JR
5from __future__ import print_function
6
ed1ec331
ER
7import os
8import re
9import time
10import rpm
11import sys
12
13re_rpm = re.compile(r'.*\.rpm$')
14re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$')
15dir = '/home/pld/admins/th/ftp/test/SRPMS/RPMS'
16
385a6048
ER
17ignore = 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
48ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD))
ed1ec331
ER
49
50def compare(f1, f2):
51 try:
52 fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY)
9c170c61 53 except Exception as e:
94960bbc 54 print(e)
ed1ec331
ER
55 # ignore non-files
56 return 0
57 try:
58 fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY)
9c170c61 59 except Exception as e:
94960bbc 60 print(e)
ed1ec331
ER
61 # ignore non-files
62 return 0
63 try:
64 h1 = ts.hdrFromFdno(fd1)
9c170c61 65 except Exception as e:
94960bbc 66 print("hdrFromFdno for %s failed: %s" % (f1, e))
ed1ec331
ER
67 os.close(fd1)
68 os.close(fd2)
69 return 0
70
71 try:
72 h2 = ts.hdrFromFdno(fd2)
9c170c61 73 except Exception as e:
94960bbc 74 print("hdrFromFdno for %s failed: %s" % (f2, e))
ed1ec331
ER
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
91def find_old(files):
92 return sorted(files, compare)
93
94files = {}
95dupes = {}
96
97for file in os.listdir(dir):
98 if not re_rpm.match(file):
99 continue
100
3eedc733
JR
101 if ignore.match(file):
102 continue
103
ed1ec331
ER
104 m = re_nvr.match(file)
105 if not m:
94960bbc 106 print("problem with: %s" % file)
ed1ec331
ER
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
94960bbc 126for i in iter(dupes.keys()):
ed1ec331 127 for old in find_old(dupes[i])[1:]:
94960bbc 128 print("removing: %s" % old)
ed1ec331 129 os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old)
This page took 0.591978 seconds and 4 git commands to generate.