Cleanup APTLY repo

After joining my new team, I've seen a huge number of packages in development's  APT repo. We use APTLY to manage our repo, and unfortunately it doesn't provide a way to expire or keep a number of package release. So, I wrote this python script to clean up the repo.

By default, it keeps 20 versions but it can be overridden. It manage APTLY package query : so it could be run against a single package or a set of packages.

Simple package query example

# clean-repo.py --repo buster-dev --package-query vault-server --keep 2 --dry-run
Run in dry mode, without actually deleting the packages.
Remove "vault-server" from buster-dev and keep the last 2 packages.

This package(s) would be kept:
vault-server_1:0.11.4~20190424~buster.build0_amd64
vault-server_1:0.11.4~20190425~buster.build0_amd64
# clean-repo.py --repo buster-dev --package-query vault-server --keep 1 --dry-run
Run in dry mode, without actually deleting the packages.
Remove "vault-server" from buster-dev and keep the last 1 packages.

This package(s) would be kept:
vault-server_1:0.11.4~20190425~buster.build0_amd64

This package(s) would be deleted:
vault-server_1:0.11.4~20190424~buster.build0_amd64

Multiple package query example

# clean-repo.py --repo buster-dev --package-query 'Name (% vault-*)' --keep 1 --dry-run
Run in dry mode, without actually deleting the packages.
Remove "Name (% vault-*)" from buster-dev and keep the last 1 packages.

This package(s) would be kept:
vault-common_1:0.11.4~20190425~buster.build0_amd64
vault-server_1:0.11.4~20190425~buster.build0_amd64

This package(s) would be deleted:
vault-common_1:0.11.4~20190424~buster.build0_amd64
vault-server_1:0.11.4~20190424~buster.build0_amd64

Full script

Use your favorite scheduler (rundeck, cron...) to run this script.