From c714df7e3cf3dfd2caa4e580001fad8880590138 Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Mon, 5 Feb 2024 10:37:50 +0100 Subject: [PATCH] cleanup, fix order logic, really delete files --- builder/cleanup/cleanup.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/builder/cleanup/cleanup.py b/builder/cleanup/cleanup.py index 2277b2f29..8fa75401c 100755 --- a/builder/cleanup/cleanup.py +++ b/builder/cleanup/cleanup.py @@ -21,6 +21,11 @@ def owrt_version(release): raise ValueError(f"Invalid OpenWrt release format: {release}") def ns_version(version): + # convert from 0.0.1-beta1-3-g4c5b89a to 0.0.1-beta1.3 + # to correctly sort build part + if version.count('-') > 1: + parts = version.split('-') + version = parts[0] + '-' + parts[1] + '.' + parts[2] try: return semver.VersionInfo.parse(version) except ValueError: @@ -46,17 +51,20 @@ def ns_version(version): # Sort by OpenWrt release owrt_sorted = sorted(unordered_versions, key=lambda x: owrt_version(x)) # Sort by NethSecurity release -sorted_dev = sorted(owrt_sorted, key=lambda v: ns_version(v)) +sorted_dev = sorted(owrt_sorted, key=lambda v: ns_version(v[13:]), reverse=True) to_delete = [] +keep = 0 for version in sorted_dev: sversion = ns_version(version[13:]) # keep at least max_versions of each sub release like 8-23.05.2-ns.0.0.1-beta1-3-g4c5b89a - if '-' in sversion.prerelease and len(to_delete) < max_versions: - to_delete.append(version) + if sversion.prerelease and '.' in sversion.prerelease: + keep += 1 + if keep > max_versions: + to_delete.append(version) for d in to_delete: print(f"Deleting {d} ...") objects_to_delete = s3_client.list_objects(Bucket=bucket_name, Prefix=f"{prefix}/{d}/") delete_keys = {'Objects' : []} delete_keys['Objects'] = [{'Key' : k} for k in [obj['Key'] for obj in objects_to_delete.get('Contents', [])] ] - #s3_client.delete_objects(Bucket=bucket_name, Delete=delete_keys) + s3_client.delete_objects(Bucket=bucket_name, Delete=delete_keys)