You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

format_control.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from pip._vendor.packaging.utils import canonicalize_name
  2. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  3. if MYPY_CHECK_RUNNING:
  4. from typing import Optional, Set, FrozenSet # noqa: F401
  5. class FormatControl(object):
  6. """Helper for managing formats from which a package can be installed.
  7. """
  8. def __init__(self, no_binary=None, only_binary=None):
  9. # type: (Optional[Set], Optional[Set]) -> None
  10. if no_binary is None:
  11. no_binary = set()
  12. if only_binary is None:
  13. only_binary = set()
  14. self.no_binary = no_binary
  15. self.only_binary = only_binary
  16. def __eq__(self, other):
  17. return self.__dict__ == other.__dict__
  18. def __ne__(self, other):
  19. return not self.__eq__(other)
  20. def __repr__(self):
  21. return "{}({}, {})".format(
  22. self.__class__.__name__,
  23. self.no_binary,
  24. self.only_binary
  25. )
  26. @staticmethod
  27. def handle_mutual_excludes(value, target, other):
  28. # type: (str, Optional[Set], Optional[Set]) -> None
  29. new = value.split(',')
  30. while ':all:' in new:
  31. other.clear()
  32. target.clear()
  33. target.add(':all:')
  34. del new[:new.index(':all:') + 1]
  35. # Without a none, we want to discard everything as :all: covers it
  36. if ':none:' not in new:
  37. return
  38. for name in new:
  39. if name == ':none:':
  40. target.clear()
  41. continue
  42. name = canonicalize_name(name)
  43. other.discard(name)
  44. target.add(name)
  45. def get_allowed_formats(self, canonical_name):
  46. # type: (str) -> FrozenSet
  47. result = {"binary", "source"}
  48. if canonical_name in self.only_binary:
  49. result.discard('source')
  50. elif canonical_name in self.no_binary:
  51. result.discard('binary')
  52. elif ':all:' in self.only_binary:
  53. result.discard('source')
  54. elif ':all:' in self.no_binary:
  55. result.discard('binary')
  56. return frozenset(result)
  57. def disallow_binaries(self):
  58. # type: () -> None
  59. self.handle_mutual_excludes(
  60. ':all:', self.no_binary, self.only_binary,
  61. )