Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LRU cleanup for generic repositories #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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

26 changes: 21 additions & 5 deletions artifactory_cleanup/rules/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
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"]