summaryrefslogtreecommitdiff
path: root/wwwbin/clean-dups.py
blob: ff9d65f6bd26a78a87c7abf72686a66bc714abcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
# arekm, 2008
# remove 

from __future__ import print_function

import os
import re
import time
import rpm
import sys

re_rpm = re.compile(r'.*\.rpm$')
re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$')
dir = '/home/pld/admins/th/ftp/test/SRPMS/RPMS'

ignore = re.compile('^(kernel-.*)$')
#|\
#crash-.*|\
#dahdi-linux-.*|\
#e1000e-.*|\
#igb-.*|\
#ipset-.*|\
#iscsitarget-.*|\
#ixgbe-.*|\
#kernel-net-wl-.*|\
#lin_tape-.*|\
#linux-fusion-.*|\
#linuxrdac-.*|\
#lirc-.*|\
#lttng-modules-.*|\
#madwifi-ng-.*|\
#nvidiabl-.*|\
#open-vm-tools-.*|\
#openvswitch-.*|\
#r8168-.*|\
#spl-.*|\
#tpm_emulator-.*|\
#VirtualBox-.*|\
#vpb-driver-.*|\
#xorg-driver-video-fglrx-.*|\
#xorg-driver-video-fglrx-legacy-.*|\
#xorg-driver-video-nvidia-.*|\
#xorg-driver-video-nvidia-legacy3-.*|\
#xorg-driver-video-nvidia-legacy-304xx-.*|\
#xtables-addons-.*)$')

ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD))

def compare(f1, f2):
	try:
		fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY)
	except Exception as e:
		print(e)
		# ignore non-files
		return 0
	try:
		fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY)
	except Exception as e:
		print(e)
		# ignore non-files
		return 0
	try:
		h1 = ts.hdrFromFdno(fd1)
	except Exception as e:
		print("hdrFromFdno for %s failed: %s" % (f1, e))
		os.close(fd1)
		os.close(fd2)
		return 0

	try:
		h2 = ts.hdrFromFdno(fd2)
	except Exception as e:
		print("hdrFromFdno for %s failed: %s" % (f2, e))
		os.close(fd1)
		os.close(fd2)
		return 0

	os.close(fd1)
	os.close(fd2)

	l1 = rpm.versionCompare(h1, h2)
	l2 = rpm.versionCompare(h2, h1)

	if l1 > 0 and l2 > 0:
		return 0

	return -l1


def find_old(files):
	return sorted(files, compare)

files = {}
dupes = {}

for file in os.listdir(dir):
	if not re_rpm.match(file):
		continue

	if ignore.match(file):
		continue

	m = re_nvr.match(file)
	if not m:
		print("problem with: %s" % file)
		sys.exit(1)

	if len(sys.argv) == 0:
		p = os.path.join(dir, file)
		mtime = os.stat(p).st_mtime
		if mtime > time.time() - 3*86400:
			continue

	name = m.group(1)

	if files.has_key(name):
		if dupes.has_key(name):
			dupes[name].append(file)
		else:
			dupes[name] = [ files[name] ]
			dupes[name].append(file)
	else:
		files[name] = file

for i in iter(dupes.keys()):
	for old in find_old(dupes[i])[1:]:
		print("removing: %s" % old)
		os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old)