]>
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/.archive/PLD/SRPMS/RPMS' | |
16 | ||
6cb7417a | 17 | ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD)) |
ed1ec331 ER |
18 | |
19 | def compare(f1, f2): | |
20 | try: | |
21 | fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY) | |
9c170c61 | 22 | except Exception as e: |
94960bbc | 23 | print(e) |
ed1ec331 ER |
24 | # ignore non-files |
25 | return 0 | |
26 | try: | |
27 | fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY) | |
9c170c61 | 28 | except Exception as e: |
94960bbc | 29 | print(e) |
ed1ec331 ER |
30 | # ignore non-files |
31 | return 0 | |
6fb44dac JR |
32 | try: |
33 | h1 = ts.hdrFromFdno(fd1) | |
34 | except Exception as e: | |
35 | print("hdrFromFdno for %s failed: %s" % (f1, e)) | |
36 | os.close(fd1) | |
37 | os.close(fd2) | |
38 | return 0 | |
39 | try: | |
40 | h2 = ts.hdrFromFdno(fd2) | |
41 | except Exception as e: | |
42 | print("hdrFromFdno for %s failed: %s" % (f1, e)) | |
43 | os.close(fd1) | |
44 | os.close(fd2) | |
45 | return 0 | |
ed1ec331 ER |
46 | os.close(fd1) |
47 | os.close(fd2) | |
48 | ||
6fb44dac JR |
49 | try: |
50 | l1 = rpm.versionCompare(h1, h2) | |
51 | except ValueError: | |
52 | l1 = -1 | |
53 | try: | |
54 | l2 = rpm.versionCompare(h2, h1) | |
55 | except ValueError: | |
56 | l2 = -1 | |
ed1ec331 ER |
57 | |
58 | if l1 > 0 and l2 > 0: | |
59 | return 0 | |
60 | ||
61 | return -l1 | |
62 | ||
63 | ||
64 | def find_old(files): | |
65 | return sorted(files, compare) | |
66 | ||
67 | files = {} | |
68 | dupes = {} | |
69 | ||
70 | for file in os.listdir(dir): | |
71 | if not re_rpm.match(file): | |
72 | continue | |
73 | ||
74 | m = re_nvr.match(file) | |
75 | if not m: | |
94960bbc | 76 | print("problem with: %s" % file) |
ed1ec331 ER |
77 | sys.exit(1) |
78 | ||
79 | if len(sys.argv) == 0: | |
80 | p = os.path.join(dir, file) | |
81 | mtime = os.stat(p).st_mtime | |
82 | if mtime > time.time() - 3*86400: | |
83 | continue | |
84 | ||
85 | name = m.group(1) | |
86 | ||
87 | if files.has_key(name): | |
88 | if dupes.has_key(name): | |
89 | dupes[name].append(file) | |
90 | else: | |
91 | dupes[name] = [ files[name] ] | |
92 | dupes[name].append(file) | |
93 | else: | |
94 | files[name] = file | |
95 | ||
94960bbc | 96 | for i in iter(dupes.keys()): |
ed1ec331 ER |
97 | for old in find_old(dupes[i])[1:]: |
98 | os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py .archive/PLD %s" % old) |