uv_cache_prune.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Exact-to-lock pruning for uv caches.
  2. The lock is the contract for anything downstream consumes wholesale: a
  3. shipped bundle payload and a rolling CI cache snapshot both must not carry
  4. wheels the project's uv.lock cannot resolve. Rolling caches accumulate
  5. entries for superseded pins (prefix restores across dependency changes);
  6. pruning to the lock keeps every consumer exact without discarding wheels
  7. the current lock still needs (unlike ``uv cache prune --ci``).
  8. """
  9. from __future__ import annotations
  10. import os
  11. import re
  12. import shutil
  13. from pathlib import Path
  14. def _normalized(name: str) -> str:
  15. """PEP 503 name: a ``ruamel.yaml-*.dist-info`` bucket is the lock's ``ruamel-yaml``."""
  16. return re.sub(r"[-_.]+", "-", name).lower()
  17. def lock_package_names(source_repo: Path) -> set[str]:
  18. """Dist names the lock can resolve; the exactness contract for a cache."""
  19. import tomllib
  20. data = tomllib.loads((source_repo / "uv.lock").read_text(encoding="utf-8"))
  21. return {_normalized(entry["name"]) for entry in data["package"]}
  22. def _drop_dangling_links(cache: Path) -> int:
  23. """Delete wheel and sdist links whose ``archive-v0/`` bucket is gone.
  24. URL, git and named-index entries sit a hash directory deeper than ``pypi``
  25. and are not pruned by name, but their links point into buckets the prune
  26. removed. A link with no target is never a cache hit, and a payload that
  27. ships one fails ``publish_prepared``'s link check. ``os.walk`` lists a
  28. dangling link among the files, so the walk never follows one.
  29. """
  30. dropped = 0
  31. for family_dir in (*cache.glob("wheels-v*"), *cache.glob("sdists-v*")):
  32. for directory, _dirs, files in os.walk(family_dir):
  33. for name in files:
  34. entry = Path(directory, name)
  35. if entry.is_symlink() and not entry.exists():
  36. entry.unlink()
  37. dropped += 1
  38. return dropped
  39. def prune_uv_cache_to_lock(cache: Path, source_repo: Path) -> int:
  40. """Delete cache entries for dists the lock cannot resolve.
  41. Unidentifiable buckets (no dist-info) survive — pruning fails open for
  42. unknown layouts, never for identifiable stale pins. Links left without a
  43. target go too. Returns the pruned entry count for the build log.
  44. """
  45. keep = lock_package_names(source_repo)
  46. dist_info = re.compile(r"([A-Za-z0-9_.]+?)-\d[^-]*\.dist-info")
  47. def dist_name(bucket: Path) -> str | None:
  48. for marker_file in bucket.glob("*.dist-info"):
  49. match = dist_info.match(marker_file.name)
  50. if match:
  51. return _normalized(match.group(1))
  52. return None
  53. pruned = 0
  54. archive = cache / "archive-v0"
  55. if archive.is_dir():
  56. for bucket in archive.iterdir():
  57. if not bucket.is_dir():
  58. continue
  59. name = dist_name(bucket)
  60. if name is not None and name not in keep:
  61. shutil.rmtree(bucket, ignore_errors=True)
  62. pruned += 1
  63. for family_dir in (*cache.glob("wheels-v*/pypi"), *cache.glob("sdists-v*/pypi")):
  64. if not family_dir.is_dir():
  65. continue
  66. for entry in family_dir.iterdir():
  67. if entry.is_dir() and _normalized(entry.name) not in keep:
  68. shutil.rmtree(entry, ignore_errors=True)
  69. pruned += 1
  70. return pruned + _drop_dangling_links(cache)