]>
Commit | Line | Data |
---|---|---|
a1e62e44 | 1 | #!/usr/bin/python3 |
ed1ec331 ER |
2 | # arekm, 2008 |
3 | # remove | |
4 | ||
94960bbc JR |
5 | from __future__ import print_function |
6 | ||
75bfea46 | 7 | import functools |
ed1ec331 ER |
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 | ||
385a6048 ER |
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 or rpm.RPMVSF_NEEDPAYLOAD)) | |
ed1ec331 ER |
50 | |
51 | def compare(f1, f2): | |
52 | try: | |
53 | fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY) | |
9c170c61 | 54 | except Exception as e: |
94960bbc | 55 | print(e) |
ed1ec331 ER |
56 | # ignore non-files |
57 | return 0 | |
58 | try: | |
59 | fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY) | |
9c170c61 | 60 | except Exception as e: |
94960bbc | 61 | print(e) |
ed1ec331 ER |
62 | # ignore non-files |
63 | return 0 | |
64 | try: | |
65 | h1 = ts.hdrFromFdno(fd1) | |
9c170c61 | 66 | except Exception as e: |
94960bbc | 67 | print("hdrFromFdno for %s failed: %s" % (f1, e)) |
ed1ec331 ER |
68 | os.close(fd1) |
69 | os.close(fd2) | |
70 | return 0 | |
71 | ||
72 | try: | |
73 | h2 = ts.hdrFromFdno(fd2) | |
9c170c61 | 74 | except Exception as e: |
94960bbc | 75 | print("hdrFromFdno for %s failed: %s" % (f2, e)) |
ed1ec331 ER |
76 | os.close(fd1) |
77 | os.close(fd2) | |
78 | return 0 | |
79 | ||
80 | os.close(fd1) | |
81 | os.close(fd2) | |
82 | ||
6fb44dac JR |
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 | |
ed1ec331 ER |
91 | |
92 | if l1 > 0 and l2 > 0: | |
93 | return 0 | |
94 | ||
95 | return -l1 | |
96 | ||
97 | ||
98 | def find_old(files): | |
7d5a034d | 99 | return sorted(files, key=cmp_to_key(compare)) |
ed1ec331 ER |
100 | |
101 | files = {} | |
102 | dupes = {} | |
103 | ||
104 | for file in os.listdir(dir): | |
105 | if not re_rpm.match(file): | |
106 | continue | |
107 | ||
3eedc733 JR |
108 | if ignore.match(file): |
109 | continue | |
110 | ||
ed1ec331 ER |
111 | m = re_nvr.match(file) |
112 | if not m: | |
94960bbc | 113 | print("problem with: %s" % file) |
ed1ec331 ER |
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 | ||
5605351f JR |
124 | if name in files: |
125 | if name in dupes: | |
ed1ec331 ER |
126 | dupes[name].append(file) |
127 | else: | |
128 | dupes[name] = [ files[name] ] | |
129 | dupes[name].append(file) | |
130 | else: | |
131 | files[name] = file | |
132 | ||
94960bbc | 133 | for i in iter(dupes.keys()): |
ed1ec331 | 134 | for old in find_old(dupes[i])[1:]: |
94960bbc | 135 | print("removing: %s" % old) |
ed1ec331 | 136 | os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old) |