From 0ca13f1123d4687fed4b10fece108f7e6a9108cd Mon Sep 17 00:00:00 2001 From: Conor MacBride Date: Tue, 23 Jan 2024 20:54:27 +0000 Subject: [PATCH 1/3] Sort versions based on upload time --- remove_old_wheels.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/remove_old_wheels.py b/remove_old_wheels.py index 9af85ff..e4b5feb 100644 --- a/remove_old_wheels.py +++ b/remove_old_wheels.py @@ -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") @@ -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"]: From ca6d20a197085193f71e83d90f7ae11fde11b1db Mon Sep 17 00:00:00 2001 From: Conor MacBride Date: Tue, 23 Jan 2024 20:56:06 +0000 Subject: [PATCH 2/3] Upgrade conda-incubator/setup-miniconda from v2 to v3 --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 6a25db6..d198422 100644 --- a/action.yml +++ b/action.yml @@ -28,7 +28,7 @@ runs: using: "composite" steps: - - uses: conda-incubator/setup-miniconda@v2 + - uses: conda-incubator/setup-miniconda@v3 - name: Upload to Anaconda.org run: | From e3a6221a67e50d869925421e3de0545ace5f5248 Mon Sep 17 00:00:00 2001 From: Conor MacBride Date: Tue, 23 Jan 2024 21:10:45 +0000 Subject: [PATCH 3/3] Quote CLI arguments for better error handling if action input has empty strings --- action.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index d198422..348169d 100644 --- a/action.yml +++ b/action.yml @@ -33,8 +33,8 @@ runs: - 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} @@ -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}