Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_internal/models/format_control.py: 62%

52 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 06:33 +0000

1from typing import FrozenSet, Optional, Set 

2 

3from pip._vendor.packaging.utils import canonicalize_name 

4 

5from pip._internal.exceptions import CommandError 

6 

7 

8class FormatControl: 

9 """Helper for managing formats from which a package can be installed.""" 

10 

11 __slots__ = ["no_binary", "only_binary"] 

12 

13 def __init__( 

14 self, 

15 no_binary: Optional[Set[str]] = None, 

16 only_binary: Optional[Set[str]] = None, 

17 ) -> None: 

18 if no_binary is None: 

19 no_binary = set() 

20 if only_binary is None: 

21 only_binary = set() 

22 

23 self.no_binary = no_binary 

24 self.only_binary = only_binary 

25 

26 def __eq__(self, other: object) -> bool: 

27 if not isinstance(other, self.__class__): 

28 return NotImplemented 

29 

30 if self.__slots__ != other.__slots__: 

31 return False 

32 

33 return all(getattr(self, k) == getattr(other, k) for k in self.__slots__) 

34 

35 def __repr__(self) -> str: 

36 return f"{self.__class__.__name__}({self.no_binary}, {self.only_binary})" 

37 

38 @staticmethod 

39 def handle_mutual_excludes(value: str, target: Set[str], other: Set[str]) -> None: 

40 if value.startswith("-"): 

41 raise CommandError( 

42 "--no-binary / --only-binary option requires 1 argument." 

43 ) 

44 new = value.split(",") 

45 while ":all:" in new: 

46 other.clear() 

47 target.clear() 

48 target.add(":all:") 

49 del new[: new.index(":all:") + 1] 

50 # Without a none, we want to discard everything as :all: covers it 

51 if ":none:" not in new: 

52 return 

53 for name in new: 

54 if name == ":none:": 

55 target.clear() 

56 continue 

57 name = canonicalize_name(name) 

58 other.discard(name) 

59 target.add(name) 

60 

61 def get_allowed_formats(self, canonical_name: str) -> FrozenSet[str]: 

62 result = {"binary", "source"} 

63 if canonical_name in self.only_binary: 

64 result.discard("source") 

65 elif canonical_name in self.no_binary: 

66 result.discard("binary") 

67 elif ":all:" in self.only_binary: 

68 result.discard("source") 

69 elif ":all:" in self.no_binary: 

70 result.discard("binary") 

71 return frozenset(result) 

72 

73 def disallow_binaries(self) -> None: 

74 self.handle_mutual_excludes( 

75 ":all:", 

76 self.no_binary, 

77 self.only_binary, 

78 )