]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - wwwbin/clean-dups.py
- ingore oracle sqlplus replacements
[projects/pld-ftp-admin.git] / wwwbin / clean-dups.py
CommitLineData
ed1ec331
ER
1#!/usr/bin/python
2# arekm, 2008
3# remove
4
5import os
6import re
7import time
8import rpm
9import sys
10
11re_rpm = re.compile(r'.*\.rpm$')
12re_nvr = re.compile('^(.*)-([^-]*)-([^-]*)\.rpm$')
13dir = '/home/pld/admins/th/ftp/test/SRPMS/RPMS'
14
15ts = rpm.TransactionSet("", (rpm._RPMVSF_NOSIGNATURES or rpm.RPMVSF_NOHDRCHK or rpm._RPMVSF_NODIGESTS or rpm.RPMVSF_NEEDPAYLOAD))
16
17def 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
58def find_old(files):
59 return sorted(files, compare)
60
61files = {}
62dupes = {}
63
64for 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
90for 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.08832 seconds and 4 git commands to generate.