Skip to content

Commit

Permalink
Add LRU cleanup for generic repositories (#151)
Browse files Browse the repository at this point in the history
fix ##130
---------

Co-authored-by: allburov <[email protected]>
  • Loading branch information
AJIOB and allburov authored Jan 13, 2025
1 parent e1b74fa commit 1b0427e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -570,4 +577,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

23 changes: 18 additions & 5 deletions artifactory_cleanup/rules/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,25 @@ 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
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)
kept_artifacts = artifacts[:self.keep]
artifacts.keep(kept_artifacts)
return artifacts
9 changes: 8 additions & 1 deletion artifactory_cleanup/rules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"]

0 comments on commit 1b0427e

Please sign in to comment.