Skip to content

Commit

Permalink
Merge pull request #6 from ConorMacBride/fix-version-sorting
Browse files Browse the repository at this point in the history
Remove old package versions based on upload time instead of version string
  • Loading branch information
ConorMacBride authored Jan 29, 2024
2 parents a86edb5 + e3a6221 commit e1cd582
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
12 changes: 6 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ runs:
using: "composite"
steps:

- uses: conda-incubator/setup-miniconda@v2
- uses: conda-incubator/setup-miniconda@v3

- name: Upload to Anaconda.org
run: |
conda install --yes anaconda-client
anaconda --token ${{ inputs.anaconda_token }} upload \
--user ${{ inputs.anaconda_user }} \
anaconda --token "${{ inputs.anaconda_token }}" upload \
--user "${{ inputs.anaconda_user }}" \
--skip-existing \
dist/*
shell: bash -l {0}
Expand All @@ -46,8 +46,8 @@ runs:
- name: Clean up old wheels on Anaconda.org
run: |
python ${{ github.action_path }}/remove_old_wheels.py \
--token ${{ inputs.anaconda_token }} \
--user ${{ inputs.anaconda_user }} \
--package ${{ inputs.anaconda_package }} \
--token "${{ inputs.anaconda_token }}" \
--user "${{ inputs.anaconda_user }}" \
--package "${{ inputs.anaconda_package }}" \
--keep ${{ inputs.keep_n_latest }}
shell: bash -l {0}
22 changes: 18 additions & 4 deletions remove_old_wheels.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Remove all but the latest N versions from wheels on Anaconda.org
from datetime import datetime, timezone, timedelta

import click
from binstar_client.utils import get_server_api

UPLOAD_TIME_FMT = r"%Y-%m-%d %H:%M:%S.%f%z" # e.g., 2018-10-19 19:03:58.717000+00:00
MIN_DATETIME = datetime.min.replace(tzinfo=timezone(timedelta(hours=0)))


@click.command()
@click.option("--user")
Expand All @@ -19,14 +23,24 @@ def remove(user, package, keep, token, dry):
pkg = api.package(user, package)

# Find versions for which wheels are available
pypi_versions = set()
pypi_versions = {}
for file_info in pkg["files"]:
if file_info["type"] == "pypi":
pypi_versions.add(file_info["version"])
pypi_versions = sorted(pypi_versions)
version = file_info["version"]
upload_time = datetime.strptime(
file_info["upload_time"],
UPLOAD_TIME_FMT,
)
# Keep date of version's most recent upload
pypi_versions[version] = max(
pypi_versions.get(version, MIN_DATETIME),
upload_time,
)
pypi_versions = [(upload_time, version) for version, upload_time in pypi_versions.items()]
pypi_versions = sorted(pypi_versions) # sort by upload time (then version)

# Determine versions to remove
versions_to_remove = pypi_versions[:-keep]
versions_to_remove = [version[1] for version in pypi_versions[:-keep]]

# Remove the files
for file_info in pkg["files"]:
Expand Down

0 comments on commit e1cd582

Please sign in to comment.