diff --git a/mirror.py b/mirror.py index 98ac917..cd20ef3 100644 --- a/mirror.py +++ b/mirror.py @@ -6,6 +6,7 @@ """ import re +import time import tomllib from pathlib import Path @@ -20,6 +21,10 @@ "pyproject.toml": r'("unref==)[0-9.]+', "README.md": r'(rev: v|rev = "v)[0-9.]+', } +# PyPI's JSON API can name a release before the simple index that `uv lock` reads, which is cached for up to 10 +# minutes. `uv lock` is retried for longer than that. +_LOCK_ATTEMPTS = 48 +_LOCK_DELAY_SECONDS = 15 def _released_versions() -> list[Version]: @@ -52,13 +57,29 @@ def _set_version(version: Version) -> None: file.write_text(text) +def _lock(version: Version) -> None: + for attempt in range(1, _LOCK_ATTEMPTS + 1): + try: + # Without the refresh, uv could reuse its own cache of the index from the attempt before. + shpyx.run(["uv", "lock", "--refresh-package", "unref"], exec_dir=_ROOT) + return + except shpyx.ShpyxVerificationError as error: + if attempt == _LOCK_ATTEMPTS: + raise + print( + f"Attempt {attempt} of {_LOCK_ATTEMPTS} to lock unref {version} failed:\n{error.result.stderr}", + flush=True, + ) + time.sleep(_LOCK_DELAY_SECONDS) + + def main() -> None: current = _current_version() for version in _released_versions(): if version <= current: continue _set_version(version) - shpyx.run(["uv", "lock"], exec_dir=_ROOT) + _lock(version) shpyx.run(["git", "add", *_FILES, "uv.lock"], exec_dir=_ROOT) shpyx.run(["git", "commit", "--message", f"Mirror unref {version}"], exec_dir=_ROOT) shpyx.run(["git", "tag", f"v{version}"], exec_dir=_ROOT)