]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blob - wwwbin/clean-dups-archive.py
4f0c9c635c2501e1f9465abf35e1285d854706fe
[projects/pld-ftp-admin.git] / wwwbin / clean-dups-archive.py
1 #!/usr/bin/python
2 # arekm, 2008
3 # remove 
4
5 from __future__ import print_function
6
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
17 ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK or rpm.RPMVSF_NEEDPAYLOAD))
18
19 def compare(f1, f2):
20         try:
21                 fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY)
22         except Exception as e:
23                 print(e)
24                 # ignore non-files
25                 return 0
26         try:
27                 fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY)
28         except Exception as e:
29                 print(e)
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:
58                 print("problem with: %s" % file)
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
78 for i in iter(dupes.keys()):
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.063591 seconds and 2 git commands to generate.