Skip to content

Commit

Permalink
refactor: main.py->retention.py and add __main__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterStolz committed Mar 29, 2024
1 parent c709ca8 commit 58360c3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
7 changes: 7 additions & 0 deletions containercrop/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from containercrop.retention import main, RetentionArgs
import logging
if __name__ == "__main__":
import asyncio

logging.basicConfig(level=logging.DEBUG)
asyncio.run(main(retention_args=RetentionArgs.from_env()))
14 changes: 11 additions & 3 deletions containercrop/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def check_is_user(self):
return self.is_user

@ensure_user_checked
async def get_all_versions(self, url: str) -> list[Image]:
async def _get_all_versions(self, url: str) -> list[Image]:
"Get all versions of an image"
# TODO get all pages and not just one
async with self.session.get(url) as resp:
Expand All @@ -92,15 +92,15 @@ async def get_all_versions(self, url: str) -> list[Image]:
async def get_versions_for_user(self, image_name: str) -> list[Image]:
"Get all versions of an image for a repo"
assert self.is_user
return await self.get_all_versions(
return await self._get_all_versions(
f"{self.api_url}/user/packages/container/{image_name}/versions"
)

@ensure_user_checked
async def get_versions_for_org(self, image_name: str) -> list[Image]:
"Get all versions of an image for an org"
assert not self.is_user
return await self.get_all_versions(
return await self._get_all_versions(
f"{self.api_url}/orgs/{self.owner}/packages/container/{image_name}/versions"
)

Expand All @@ -110,3 +110,11 @@ async def get_versions(self, image_name: str) -> list[Image]:
if self.is_user:
return await self.get_versions_for_user(image_name)
return await self.get_versions_for_org(image_name)

async def delete_image(self, image: Image) -> bool:
"Delete an image"
return False

async def delete_images(self, images: list[Image]) -> list[Image]:
"Delete all images"
return []
26 changes: 12 additions & 14 deletions containercrop/main.py → containercrop/retention.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from dateparser import parse
from pydantic import BaseModel, Field, field_validator, model_validator

from .github_api import GithubAPI, Image
from containercrop.github_api import GithubAPI, Image


def get_args_from_env() -> dict[str, str | None]:
Expand Down Expand Up @@ -87,24 +87,26 @@ def matches_retention_policy(image: Image, args: RetentionArgs) -> bool:
if args.skip_tags and any(
any(fnmatch(tag, skip_tag) for skip_tag in args.skip_tags) for tag in image.tags
):
logging.debug(f"Image {image.name}({image.url}) does match skip tags")
logging.debug("Image %s(%s) does match skip tags", image.name, image.url)
return False
if args.untagged_only and image.tags:
logging.debug(
f"Image {image.name}({image.url}) is tagged and untagged_only is set"
"Image %s(%s) is tagged and untagged_only is set", image.name, image.url
)
return False
if args.cut_off and image.is_before_cut_off_date(args.cut_off):
logging.debug(f"Image {image.name}({image.url}) is before cut-off date")
logging.debug("Image %s(%s) is before cut-off date", image.name, image.url)
return True
if args.filter_tags and any(
any(fnmatch(tag, filter_tag) for filter_tag in args.filter_tags)
for tag in image.tags
):
logging.debug(f"Image {image.name}({image.url}) does match filter tags")
logging.debug("Image %s(%s) does match filter tags", image.name, image.url)
return True
logging.debug(
f"Image {image.name}({image.url}) does not match any policy, therefore we keep it"
"Image %s(%s) does not match any policy, therefore we keep it",
image.name,
image.url,
)
return False

Expand All @@ -119,11 +121,7 @@ def apply_retention_policy(args: RetentionArgs, images: list[Image]) -> list[Ima

async def main(retention_args: RetentionArgs):
api = GithubAPI(owner=retention_args.repo_owner, token=retention_args.token)
images = await api.get_versions(retention_args.image_names)


if __name__ == "__main__":
import asyncio

logging.basicConfig(level=logging.DEBUG)
asyncio.run(main(retention_args=RetentionArgs.from_env()))
images = await api.get_versions(retention_args.image_name)
to_delete = apply_retention_policy(retention_args, images)
logging.info("Images to delete: %s", to_delete)
await api.delete_images(to_delete)
2 changes: 1 addition & 1 deletion containercrop/test_github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from . import github_api
from containercrop import github_api


@pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest

from .github_api import Image
from .main import RetentionArgs, apply_retention_policy
from containercrop.github_api import Image
from containercrop.retention import RetentionArgs, apply_retention_policy


@pytest.fixture
Expand Down

0 comments on commit 58360c3

Please sign in to comment.