release_control.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import annotations
  2. from dataclasses import dataclass, field
  3. from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
  4. from pip._internal.exceptions import CommandError
  5. # TODO: add slots=True when Python 3.9 is dropped
  6. @dataclass
  7. class ReleaseControl:
  8. """Helper for managing which release types can be installed."""
  9. all_releases: set[str] = field(default_factory=set)
  10. only_final: set[str] = field(default_factory=set)
  11. _order: list[tuple[str, str]] = field(
  12. init=False, default_factory=list, compare=False, repr=False
  13. )
  14. def handle_mutual_excludes(
  15. self, value: str, target: set[str], other: set[str], attr_name: str
  16. ) -> None:
  17. """Parse and apply release control option value.
  18. Processes comma-separated package names or special values `:all:` and `:none:`.
  19. When adding packages to target, they're removed from other to maintain mutual
  20. exclusivity between all_releases and only_final. All operations are tracked in
  21. order so that the original command-line argument sequence can be reconstructed
  22. when passing options to build subprocesses.
  23. """
  24. if value.startswith("-"):
  25. raise CommandError(
  26. "--all-releases / --only-final option requires 1 argument."
  27. )
  28. new = value.split(",")
  29. while ":all:" in new:
  30. other.clear()
  31. target.clear()
  32. target.add(":all:")
  33. # Track :all: in order
  34. self._order.append((attr_name, ":all:"))
  35. del new[: new.index(":all:") + 1]
  36. # Without a none, we want to discard everything as :all: covers it
  37. if ":none:" not in new:
  38. return
  39. for name in new:
  40. if name == ":none:":
  41. target.clear()
  42. # Track :none: in order
  43. self._order.append((attr_name, ":none:"))
  44. continue
  45. name = canonicalize_name(name)
  46. other.discard(name)
  47. target.add(name)
  48. # Track package-specific setting in order
  49. self._order.append((attr_name, name))
  50. def get_ordered_args(self) -> list[tuple[str, str]]:
  51. """
  52. Get ordered list of (flag_name, value) tuples for reconstructing CLI args.
  53. Returns:
  54. List of tuples where each tuple is (attribute_name, value).
  55. The attribute_name is either 'all_releases' or 'only_final'.
  56. Example:
  57. [("all_releases", ":all:"), ("only_final", "simple")]
  58. would be reconstructed as:
  59. ["--all-releases", ":all:", "--only-final", "simple"]
  60. """
  61. return self._order[:]
  62. def allows_prereleases(self, canonical_name: NormalizedName) -> bool | None:
  63. """
  64. Determine if pre-releases are allowed for a package.
  65. Returns:
  66. True: Pre-releases are allowed (package in all_releases)
  67. False: Only final releases allowed (package in only_final)
  68. None: No specific setting, use default behavior
  69. """
  70. if canonical_name in self.all_releases:
  71. return True
  72. elif canonical_name in self.only_final:
  73. return False
  74. elif ":all:" in self.all_releases:
  75. return True
  76. elif ":all:" in self.only_final:
  77. return False
  78. return None