]>
Commit | Line | Data |
---|---|---|
ed1ec331 ER |
1 | #!/usr/bin/python |
2 | # arekm, 2008 | |
3 | # remove | |
4 | ||
5 | import os | |
6 | import re | |
7 | import time | |
8 | import rpm | |
9 | import sys | |
10 | ||
11 | re_rpm = re.compile(r'.*\.rpm$') | |
12 | re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$') | |
13 | dir = '/home/pld/admins/th/ftp/test/SRPMS/RPMS' | |
14 | ||
3eedc733 JR |
15 | ignore = re.compile('^(kernel-.*|\ |
16 | dahdi-linux-.*|\ | |
17 | e1000e-.*|\ | |
18 | igb-.*|\ | |
19 | ipset-.*|\ | |
20 | ixgbe-.*|\ | |
21 | linuxrdac-.*|\ | |
22 | lirc-.*|\ | |
23 | madwifi-ng-.*|\ | |
24 | open-vm-tools-.*|\ | |
25 | r8168-.*|\ | |
26 | VirtualBox-.*|\ | |
27 | xorg-driver-video-fglrx-.*|\ | |
28 | xorg-driver-video-fglrx-legacy-12.x-.*|\ | |
29 | xorg-driver-video-nvidia-.*|\ | |
30 | xtables-addons-.*|\ | |
31 | iscsitarget-.*|\ | |
32 | openvswitch-.*|\ | |
33 | xorg-driver-video-nvidia-legacy3-.*)$') | |
34 | ||
7e39d1d1 | 35 | ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD or rpm.RPMVSF_NODSAHEADER)) |
ed1ec331 ER |
36 | |
37 | def compare(f1, f2): | |
38 | try: | |
39 | fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY) | |
40 | except Exception, e: | |
41 | print e | |
42 | # ignore non-files | |
43 | return 0 | |
44 | try: | |
45 | fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY) | |
46 | except Exception, e: | |
47 | print e | |
48 | # ignore non-files | |
49 | return 0 | |
50 | try: | |
51 | h1 = ts.hdrFromFdno(fd1) | |
52 | except Exception, e: | |
53 | print "hdrFromFdno for %s failed: %s" % (f1, e) | |
54 | os.close(fd1) | |
55 | os.close(fd2) | |
56 | return 0 | |
57 | ||
58 | try: | |
59 | h2 = ts.hdrFromFdno(fd2) | |
60 | except Exception, e: | |
61 | print "hdrFromFdno for %s failed: %s" % (f2, e) | |
62 | os.close(fd1) | |
63 | os.close(fd2) | |
64 | return 0 | |
65 | ||
66 | os.close(fd1) | |
67 | os.close(fd2) | |
68 | ||
69 | l1 = rpm.versionCompare(h1, h2) | |
70 | l2 = rpm.versionCompare(h2, h1) | |
71 | ||
72 | if l1 > 0 and l2 > 0: | |
73 | return 0 | |
74 | ||
75 | return -l1 | |
76 | ||
77 | ||
78 | def find_old(files): | |
79 | return sorted(files, compare) | |
80 | ||
81 | files = {} | |
82 | dupes = {} | |
83 | ||
84 | for file in os.listdir(dir): | |
85 | if not re_rpm.match(file): | |
86 | continue | |
87 | ||
3eedc733 JR |
88 | if ignore.match(file): |
89 | continue | |
90 | ||
ed1ec331 ER |
91 | m = re_nvr.match(file) |
92 | if not m: | |
93 | print "problem with: %s" % file | |
94 | sys.exit(1) | |
95 | ||
96 | if len(sys.argv) == 0: | |
97 | p = os.path.join(dir, file) | |
98 | mtime = os.stat(p).st_mtime | |
99 | if mtime > time.time() - 3*86400: | |
100 | continue | |
101 | ||
102 | name = m.group(1) | |
103 | ||
104 | if files.has_key(name): | |
105 | if dupes.has_key(name): | |
106 | dupes[name].append(file) | |
107 | else: | |
108 | dupes[name] = [ files[name] ] | |
109 | dupes[name].append(file) | |
110 | else: | |
111 | files[name] = file | |
112 | ||
113 | for i in dupes.iterkeys(): | |
114 | for old in find_old(dupes[i])[1:]: | |
115 | os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old) |