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.2.7, created at 2023-06-07 06:48 +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 "{}({}, {})".format( 

37 self.__class__.__name__, self.no_binary, self.only_binary 

38 ) 

39 

40 @staticmethod 

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

42 if value.startswith("-"): 

43 raise CommandError( 

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

45 ) 

46 new = value.split(",") 

47 while ":all:" in new: 

48 other.clear() 

49 target.clear() 

50 target.add(":all:") 

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

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

53 if ":none:" not in new: 

54 return 

55 for name in new: 

56 if name == ":none:": 

57 target.clear() 

58 continue 

59 name = canonicalize_name(name) 

60 other.discard(name) 

61 target.add(name) 

62 

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

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

65 if canonical_name in self.only_binary: 

66 result.discard("source") 

67 elif canonical_name in self.no_binary: 

68 result.discard("binary") 

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

70 result.discard("source") 

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

72 result.discard("binary") 

73 return frozenset(result) 

74 

75 def disallow_binaries(self) -> None: 

76 self.handle_mutual_excludes( 

77 ":all:", 

78 self.no_binary, 

79 self.only_binary, 

80 )