]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blob - wwwbin/clean-dups-archive.py
Switch to Python 3 for rpm.org rpm
[projects/pld-ftp-admin.git] / wwwbin / clean-dups-archive.py
1 #!/usr/bin/python3
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         try:
33                 h1 = ts.hdrFromFdno(fd1)
34         except Exception as e:
35                 print("hdrFromFdno for %s failed: %s" % (f1, e))
36                 os.close(fd1)
37                 os.close(fd2)
38                 return 0
39         try:
40                 h2 = ts.hdrFromFdno(fd2)
41         except Exception as e:
42                 print("hdrFromFdno for %s failed: %s" % (f1, e))
43                 os.close(fd1)
44                 os.close(fd2)
45                 return 0
46         os.close(fd1)
47         os.close(fd2)
48
49         try:
50                 l1 = rpm.versionCompare(h1, h2)
51         except ValueError:
52                 l1 = -1
53         try:
54                 l2 = rpm.versionCompare(h2, h1)
55         except ValueError:
56                 l2 = -1
57
58         if l1 > 0 and l2 > 0:
59                 return 0
60
61         return -l1
62
63
64 def find_old(files):
65         return sorted(files, compare)
66
67 files = {}
68 dupes = {}
69
70 for file in os.listdir(dir):
71         if not re_rpm.match(file):
72                 continue
73
74         m = re_nvr.match(file)
75         if not m:
76                 print("problem with: %s" % file)
77                 sys.exit(1)
78
79         if len(sys.argv) == 0:
80                 p = os.path.join(dir, file)
81                 mtime = os.stat(p).st_mtime
82                 if mtime > time.time() - 3*86400:
83                         continue
84
85         name = m.group(1)
86
87         if files.has_key(name):
88                 if dupes.has_key(name):
89                         dupes[name].append(file)
90                 else:
91                         dupes[name] = [ files[name] ]
92                         dupes[name].append(file)
93         else:
94                 files[name] = file
95
96 for i in iter(dupes.keys()):
97         for old in find_old(dupes[i])[1:]:
98                 os.system("/home/pld/admins/th/pld-ftp-admin/scripts/remove.py .archive/PLD %s" % old)
This page took 0.03803 seconds and 3 git commands to generate.