[projects/pld-ftp-admin] clean-dups.py: compare versions as a move does, with FtpTree.evrs()
arekm
arekm at pld-linux.org
Sat Nov 21 10:43:22 CET 2026
commit 2aae6463d29bede21e6007813040a715b5d0f78b
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date: Sat Nov 21 01:38:47 2026 +0100
clean-dups.py: compare versions as a move does, with FtpTree.evrs()
The sort read both src.rpm headers again for every comparison, and kept
a ValueError fallback that rpm.versionCompare never raises. FtpTree.evrs()
reads each header once, in its process pool, so one place decides which
version is older.
wwwbin/clean-dups.py | 78 ++++++++++------------------------------------------
1 file changed, 15 insertions(+), 63 deletions(-)
---
diff --git a/wwwbin/clean-dups.py b/wwwbin/clean-dups.py
index bce3531..1e2e2a9 100755
--- a/wwwbin/clean-dups.py
+++ b/wwwbin/clean-dups.py
@@ -28,65 +28,18 @@ ftp_tree = "test"
ignore = re.compile(r'^(kernel-.*)$')
-ts = rpm.TransactionSet("", (rpm.RPMVSF_NOHDRCHK | rpm.RPMVSF_NEEDPAYLOAD | rpm.RPMVSF_MASK_NODIGESTS | rpm.RPMVSF_MASK_NOSIGNATURES))
-
-class CompareError(Exception):
- pass
-
-def compare(f1, f2):
- try:
- fd1 = os.open(os.path.join(dir, f1), os.O_RDONLY)
- except OSError as e:
- raise CompareError(e)
- try:
- fd2 = os.open(os.path.join(dir, f2), os.O_RDONLY)
- except OSError as e:
- os.close(fd1)
- raise CompareError(e)
- try:
- h1 = ts.hdrFromFdno(fd1)
- except (rpm.error, OSError) as e:
- os.close(fd1)
- os.close(fd2)
- raise CompareError(f"hdrFromFdno for {f1} failed: {e}")
-
- try:
- h2 = ts.hdrFromFdno(fd2)
- except (rpm.error, OSError) as e:
- os.close(fd1)
- os.close(fd2)
- raise CompareError(f"hdrFromFdno for {f2} failed: {e}")
-
- os.close(fd1)
- os.close(fd2)
-
- try:
- l1 = rpm.versionCompare(h1, h2)
- except ValueError:
- l1 = -1
- try:
- l2 = rpm.versionCompare(h2, h1)
- except ValueError:
- l2 = -1
-
- if l1 > 0 and l2 > 0:
- return 0
-
- return -l1
-
-
-def find_old(files):
- return sorted(files, key=functools.cmp_to_key(compare))[1:]
-
-def find_dupes(dupes):
- pkgs = []
- for old in dupes:
- # an order with an unreadable file in it is arbitrary and could drop the newest version
- try:
- pkgs.extend(find_old(old))
- except CompareError as e:
- print(f"{e}; keeping {' '.join(old)}")
- return pkgs
+def find_old(tree, groups):
+ """All but the newest version in each of groups of nvrs, as a move compares them"""
+ evr, unreadable = tree.evrs([nvr for nvrs in groups for nvr in nvrs])
+ old = []
+ for nvrs in groups:
+ # an order with an unreadable src.rpm in it is arbitrary and could drop the newest version
+ bad = [unreadable[nvr] for nvr in nvrs if nvr in unreadable]
+ if bad:
+ print(f"{'; '.join(bad)}; keeping {' '.join(nvrs)}")
+ continue
+ old += sorted(nvrs, key=functools.cmp_to_key(lambda a, b: rpm.labelCompare(evr[b], evr[a])))[1:]
+ return old
def young(path):
"""Newer than --min-age; a missing file is not, so reading its header later keeps all its versions"""
@@ -110,12 +63,11 @@ try:
for nvr in tree.pkgnames:
if ignore.match(nvr):
continue
- file = nvr + '.src.rpm'
- if args.min_age and young(os.path.join(dir, file)):
+ if args.min_age and young(f'{dir}/{nvr}.src.rpm'):
continue
- byname.setdefault('-'.join(nvr.split('-')[:-2]), []).append(file)
+ byname.setdefault('-'.join(nvr.split('-')[:-2]), []).append(nvr)
- pkgs = find_dupes(files for files in byname.values() if len(files) > 1)
+ pkgs = find_old(tree, [nvrs for nvrs in byname.values() if len(nvrs) > 1])
for pkg in pkgs:
print(f"removing: {pkg}")
tree.mark4removal(pkgs)
================================================================
---- gitweb:
http://git.pld-linux.org/gitweb.cgi/projects/pld-ftp-admin.git/commitdiff/a049a512859eaede288d71649a2320e16c959476
More information about the pld-cvs-commit
mailing list