]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blob - wwwbin/clean-dups.py
- import from ~pldth/bin
[projects/pld-ftp-admin.git] / wwwbin / clean-dups.py
1 #!/usr/bin/python
2 # arekm, 2008
3 # remove 
4
5 import os
6 import re
7 import time
8 import rpm
9 import sys
10
11 re_rpm = re.compile(r'.*\.rpm$')
12 re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$')
13 dir = '/home/pld/admins/th/ftp/test/SRPMS/RPMS'
14
15 ts = rpm.TransactionSet("", (rpm._RPMVSF_NOSIGNATURES or rpm.RPMVSF_NOHDRCHK or rpm._RPMVSF_NODIGESTS or rpm.RPMVSF_NEEDPAYLOAD))
16
17 def compare(f1, f2):
18         try:
19                 fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY)
20         except Exception, e:
21                 print e
22                 # ignore non-files
23                 return 0
24         try:
25                 fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY)
26         except Exception, e:
27                 print e
28                 # ignore non-files
29                 return 0
30         try:
31                 h1 = ts.hdrFromFdno(fd1)
32         except Exception, e:
33                 print "hdrFromFdno for %s failed: %s" % (f1, e)
34                 os.close(fd1)
35                 os.close(fd2)
36                 return 0
37
38         try:
39                 h2 = ts.hdrFromFdno(fd2)
40         except Exception, e:
41                 print "hdrFromFdno for %s failed: %s" % (f2, e)
42                 os.close(fd1)
43                 os.close(fd2)
44                 return 0
45
46         os.close(fd1)
47         os.close(fd2)
48
49         l1 = rpm.versionCompare(h1, h2)
50         l2 = rpm.versionCompare(h2, h1)
51
52         if l1 > 0 and l2 > 0:
53                 return 0
54
55         return -l1
56
57
58 def find_old(files):
59         return sorted(files, compare)
60
61 files = {}
62 dupes = {}
63
64 for file in os.listdir(dir):
65         if not re_rpm.match(file):
66                 continue
67
68         m = re_nvr.match(file)
69         if not m:
70                 print "problem with: %s" % file
71                 sys.exit(1)
72
73         if len(sys.argv) == 0:
74                 p = os.path.join(dir, file)
75                 mtime = os.stat(p).st_mtime
76                 if mtime > time.time() - 3*86400:
77                         continue
78
79         name = m.group(1)
80
81         if files.has_key(name):
82                 if dupes.has_key(name):
83                         dupes[name].append(file)
84                 else:
85                         dupes[name] = [ files[name] ]
86                         dupes[name].append(file)
87         else:
88                 files[name] = file
89
90 for i in dupes.iterkeys():
91         for old in find_old(dupes[i])[1:]:
92                 os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py test %s" % old)
This page took 0.068086 seconds and 4 git commands to generate.