From 060a4afd37f14f7b2579682dd5ff7f1c0f1b43fa Mon Sep 17 00:00:00 2001 From: AJIOB Date: Tue, 26 Nov 2024 15:14:34 +0300 Subject: [PATCH 1/3] Add LRU cleanup rule for generic repositories --- README.md | 8 +++++++- artifactory_cleanup/rules/delete.py | 26 +++++++++++++++++++++----- artifactory_cleanup/rules/utils.py | 9 ++++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 37d3f85..5d8ec53 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,13 @@ policies: regex_pattern: "\d" ``` +- `DeleteLeastRecentlyUsedFiles` - delete the least recently used files and keep at most requested number of files. Creation is interpreted as a first usage + +```yaml +- rule: DeleteLeastRecentlyUsedFiles + keep: 10 +``` + ## Keep - `KeepLatestNFiles` - Leaves the last (by creation time) files in the amount of N pieces. WITHOUT accounting @@ -562,4 +569,3 @@ In order to provide a new release of `artifactory-cleanup`, there are two steps 1. Bump the version in the [setup.py](setup.py) 2. Bump the version in the [__init__.py](./artifactory_cleanup/__init__.py) 3. Create a Git release tag (in format `1.0.1`) by creating a release on GitHub - diff --git a/artifactory_cleanup/rules/delete.py b/artifactory_cleanup/rules/delete.py index fa76994..325e5b0 100644 --- a/artifactory_cleanup/rules/delete.py +++ b/artifactory_cleanup/rules/delete.py @@ -113,12 +113,28 @@ class DeleteByRegexpName(Rule): def __init__(self, regex_pattern): self.regex_pattern = rf"{regex_pattern}" - def aql_add_filter(self, filters): - print("Here's filters that we get\n", filters) - return filters - def filter(self, artifacts: ArtifactsList) -> ArtifactsList: for artifact in artifacts[:]: if re.match(self.regex_pattern, artifact["name"]) is None: artifacts.remove(artifact) - return artifacts \ No newline at end of file + return artifacts + + +class DeleteLeastRecentlyUsedFiles(Rule): + """ + Delete the least recently used files, and keep at most ``keep`` files. + Creation is interpreted as a first usage. + """ + + def __init__(self, keep: int): + self.keep = keep + + def filter(self, artifacts: ArtifactsList) -> ArtifactsList: + # List will contain fresh files at the beginning + artifacts.sort(key=utils.sort_by_usage, reverse=True) + + keptArtifacts = artifacts[:self.keep] + + artifacts.keep(keptArtifacts) + + return artifacts diff --git a/artifactory_cleanup/rules/utils.py b/artifactory_cleanup/rules/utils.py index d87b1e7..373ff81 100644 --- a/artifactory_cleanup/rules/utils.py +++ b/artifactory_cleanup/rules/utils.py @@ -3,7 +3,7 @@ from treelib import Node, Tree -from artifactory_cleanup.rules.base import ArtifactsList +from artifactory_cleanup.rules.base import ArtifactDict, ArtifactsList def is_repository(data): @@ -188,3 +188,10 @@ def to_masks(masks: Union[str, List[str]]): return masks else: raise AttributeError("'masks' argument must by list of string OR string") + + +def sort_by_usage(artifact: ArtifactDict) -> str: + try: + return artifact["stats"]["downloaded"] + except: + return artifact["created"] From 3e39c2ff822207e781202f8359bd858355a6ba99 Mon Sep 17 00:00:00 2001 From: allburov Date: Mon, 13 Jan 2025 17:05:39 +0700 Subject: [PATCH 2/3] Update artifactory_cleanup/rules/delete.py --- artifactory_cleanup/rules/delete.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/artifactory_cleanup/rules/delete.py b/artifactory_cleanup/rules/delete.py index 325e5b0..2d413f3 100644 --- a/artifactory_cleanup/rules/delete.py +++ b/artifactory_cleanup/rules/delete.py @@ -132,9 +132,6 @@ def __init__(self, keep: int): def filter(self, artifacts: ArtifactsList) -> ArtifactsList: # List will contain fresh files at the beginning artifacts.sort(key=utils.sort_by_usage, reverse=True) - keptArtifacts = artifacts[:self.keep] - artifacts.keep(keptArtifacts) - return artifacts From bdcd7e167ad8bd86c826f43f6f88d3149334fed7 Mon Sep 17 00:00:00 2001 From: allburov Date: Mon, 13 Jan 2025 17:08:11 +0700 Subject: [PATCH 3/3] Update artifactory_cleanup/rules/delete.py --- artifactory_cleanup/rules/delete.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/artifactory_cleanup/rules/delete.py b/artifactory_cleanup/rules/delete.py index 2d413f3..990d391 100644 --- a/artifactory_cleanup/rules/delete.py +++ b/artifactory_cleanup/rules/delete.py @@ -132,6 +132,6 @@ def __init__(self, keep: int): def filter(self, artifacts: ArtifactsList) -> ArtifactsList: # List will contain fresh files at the beginning artifacts.sort(key=utils.sort_by_usage, reverse=True) - keptArtifacts = artifacts[:self.keep] - artifacts.keep(keptArtifacts) + kept_artifacts = artifacts[:self.keep] + artifacts.keep(kept_artifacts) return artifacts