]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blob - wwwbin/clean-dups-old.py
Comaptibility updates for rpm4/python3
[projects/pld-ftp-admin.git] / wwwbin / clean-dups-old.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_info = re.compile(r'.*\.info$')
14 re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.info$')
15 dir = '/home/pld/admins/th/ftp/test/SRPMS/.metadata'
16
17 def compare(f1, f2):
18         m1 = re_nvr.match(f1)
19         n1 = m1.group(1)
20         v1 = m1.group(2)
21         r1 = m1.group(3)
22
23         m2 = re_nvr.match(f2)
24         n2 = m2.group(1)
25         v2 = m2.group(2)
26         r2 = m2.group(3)
27
28         try:
29                 l1 = rpm.labelCompare((n1, v1, r1), (n2, v2, r2))
30         except ValueError:
31                 l1 = -1
32         try:
33                 l2 = rpm.labelCompare((n2, v2, r2), (n1, v1, r1))
34         except ValueError:
35                 l2 = -1
36
37         if l1 > 0 and l2 > 0:
38                 return 0
39
40         return -l1
41
42
43 def find_old(files):
44         return sorted(files, compare)
45
46 files = {}
47 dupes = {}
48
49 for file in os.listdir(dir):
50         if not re_info.match(file):
51                 continue
52
53         m = re_nvr.match(file)
54         if not m:
55                 print("problem with: %s" % file)
56                 sys.exit(1)
57
58         if len(sys.argv) == 0:
59                 p = os.path.join(dir, file)
60                 mtime = os.stat(p).st_mtime
61                 if mtime > time.time() - 3*86400:
62                         continue
63
64         name = m.group(1)
65
66         if files.has_key(name):
67                 if dupes.has_key(name):
68                         dupes[name].append(file)
69                 else:
70                         dupes[name] = [ files[name] ]
71                         dupes[name].append(file)
72         else:
73                 files[name] = file
74
75 for i in iter(dupes.keys()):
76         for old in find_old(dupes[i])[1:]:
77                 os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old)
This page took 0.055456 seconds and 3 git commands to generate.