Skip to content

Commit

Permalink
Merge pull request #9 from neutrinoceros/fix_upload_time_parsing
Browse files Browse the repository at this point in the history
BUG: fix parsing upload time
  • Loading branch information
ConorMacBride authored Feb 20, 2024
2 parents e1cd582 + e7d28b9 commit 612ea80
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions remove_old_wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@
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
UPLOAD_TIME_FMT_V1 = r"%Y-%m-%d %H:%M:%S.%f%z" # e.g., 2018-10-19 19:03:58.717000+00:00
UPLOAD_TIME_FMT_V2 = r"%Y-%m-%d %H:%M:%S%z" # e.g., 2018-10-19 19:03:58+00:00
MIN_DATETIME = datetime.min.replace(tzinfo=timezone(timedelta(hours=0)))

def _parse_upload_time(upload_time: str) -> datetime:
for fmt in (UPLOAD_TIME_FMT_V1, UPLOAD_TIME_FMT_V2):
try:
return datetime.strptime(upload_time, fmt)
except ValueError:
continue
raise ValueError(
f"Failed to parse upload time {upload_time!r} "
"(didn't match any known format)"
)

@click.command()
@click.option("--user")
Expand All @@ -27,10 +38,7 @@ def remove(user, package, keep, token, dry):
for file_info in pkg["files"]:
if file_info["type"] == "pypi":
version = file_info["version"]
upload_time = datetime.strptime(
file_info["upload_time"],
UPLOAD_TIME_FMT,
)
upload_time = _parse_upload_time(file_info["upload_time"])
# Keep date of version's most recent upload
pypi_versions[version] = max(
pypi_versions.get(version, MIN_DATETIME),
Expand Down

0 comments on commit 612ea80

Please sign in to comment.