]>
Commit | Line | Data |
---|---|---|
ed1ec331 ER |
1 | #!/usr/bin/python |
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 | |
32 | h1 = ts.hdrFromFdno(fd1) | |
33 | h2 = ts.hdrFromFdno(fd2) | |
34 | os.close(fd1) | |
35 | os.close(fd2) | |
36 | ||
37 | l1 = rpm.versionCompare(h1, h2) | |
38 | l2 = rpm.versionCompare(h2, h1) | |
39 | ||
40 | if l1 > 0 and l2 > 0: | |
41 | return 0 | |
42 | ||
43 | return -l1 | |
44 | ||
45 | ||
46 | def find_old(files): | |
47 | return sorted(files, compare) | |
48 | ||
49 | files = {} | |
50 | dupes = {} | |
51 | ||
52 | for file in os.listdir(dir): | |
53 | if not re_rpm.match(file): | |
54 | continue | |
55 | ||
56 | m = re_nvr.match(file) | |
57 | if not m: | |
94960bbc | 58 | print("problem with: %s" % file) |
ed1ec331 ER |
59 | sys.exit(1) |
60 | ||
61 | if len(sys.argv) == 0: | |
62 | p = os.path.join(dir, file) | |
63 | mtime = os.stat(p).st_mtime | |
64 | if mtime > time.time() - 3*86400: | |
65 | continue | |
66 | ||
67 | name = m.group(1) | |
68 | ||
69 | if files.has_key(name): | |
70 | if dupes.has_key(name): | |
71 | dupes[name].append(file) | |
72 | else: | |
73 | dupes[name] = [ files[name] ] | |
74 | dupes[name].append(file) | |
75 | else: | |
76 | files[name] = file | |
77 | ||
94960bbc | 78 | for i in iter(dupes.keys()): |
ed1ec331 ER |
79 | for old in find_old(dupes[i])[1:]: |
80 | os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py .archive/PLD %s" % old) |