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