]>
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 | ||
ed1ec331 ER |
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 | ||
385a6048 ER |
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)) | |
ed1ec331 ER |
49 | |
50 | def 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 | ||
6fb44dac JR |
82 | try: |
83 | l1 = rpm.versionCompare(h1, h2) | |
84 | except ValueError: | |
85 | l1 = -1 | |
86 | try: | |
87 | l2 = rpm.versionCompare(h2, h1) | |
88 | except ValueError: | |
89 | l2 = -1 | |
ed1ec331 ER |
90 | |
91 | if l1 > 0 and l2 > 0: | |
92 | return 0 | |
93 | ||
94 | return -l1 | |
95 | ||
96 | ||
97 | def find_old(files): | |
98 | return sorted(files, compare) | |
99 | ||
100 | files = {} | |
101 | dupes = {} | |
102 | ||
103 | for file in os.listdir(dir): | |
104 | if not re_rpm.match(file): | |
105 | continue | |
106 | ||
3eedc733 JR |
107 | if ignore.match(file): |
108 | continue | |
109 | ||
ed1ec331 ER |
110 | m = re_nvr.match(file) |
111 | if not m: | |
94960bbc | 112 | print("problem with: %s" % file) |
ed1ec331 ER |
113 | sys.exit(1) |
114 | ||
115 | if len(sys.argv) == 0: | |
116 | p = os.path.join(dir, file) | |
117 | mtime = os.stat(p).st_mtime | |
118 | if mtime > time.time() - 3*86400: | |
119 | continue | |
120 | ||
121 | name = m.group(1) | |
122 | ||
123 | if files.has_key(name): | |
124 | if dupes.has_key(name): | |
125 | dupes[name].append(file) | |
126 | else: | |
127 | dupes[name] = [ files[name] ] | |
128 | dupes[name].append(file) | |
129 | else: | |
130 | files[name] = file | |
131 | ||
94960bbc | 132 | for i in iter(dupes.keys()): |
ed1ec331 | 133 | for old in find_old(dupes[i])[1:]: |
94960bbc | 134 | print("removing: %s" % old) |
ed1ec331 | 135 | os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old) |