]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - wwwbin/clean-dups-archive.py
Fix exception handling syntax (python3 compat)
[projects/pld-ftp-admin.git] / wwwbin / clean-dups-archive.py
CommitLineData
ed1ec331
ER
1#!/usr/bin/python
2# arekm, 2008
3# remove
4
94960bbc
JR
5from __future__ import print_function
6
ed1ec331
ER
7import os
8import re
9import time
10import rpm
11import sys
12
13re_rpm = re.compile(r'.*\.rpm$')
14re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$')
15dir = '/home/pld/admins/th/ftp/.archive/PLD/SRPMS/RPMS'
16
6cb7417a 17ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD))
ed1ec331
ER
18
19def 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
46def find_old(files):
47 return sorted(files, compare)
48
49files = {}
50dupes = {}
51
52for 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 78for 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)
This page took 0.859257 seconds and 4 git commands to generate.