format_control.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # The following comment should be removed at some point in the future.
  2. # mypy: strict-optional=False
  3. # mypy: disallow-untyped-defs=False
  4. from pip._vendor.packaging.utils import canonicalize_name
  5. from pip._internal.exceptions import CommandError
  6. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  7. if MYPY_CHECK_RUNNING:
  8. from typing import Optional, Set, FrozenSet
  9. class FormatControl(object):
  10. """Helper for managing formats from which a package can be installed.
  11. """
  12. def __init__(self, no_binary=None, only_binary=None):
  13. # type: (Optional[Set], Optional[Set]) -> None
  14. if no_binary is None:
  15. no_binary = set()
  16. if only_binary is None:
  17. only_binary = set()
  18. self.no_binary = no_binary
  19. self.only_binary = only_binary
  20. def __eq__(self, other):
  21. return self.__dict__ == other.__dict__
  22. def __ne__(self, other):
  23. return not self.__eq__(other)
  24. def __repr__(self):
  25. return "{}({}, {})".format(
  26. self.__class__.__name__,
  27. self.no_binary,
  28. self.only_binary
  29. )
  30. @staticmethod
  31. def handle_mutual_excludes(value, target, other):
  32. # type: (str, Optional[Set], Optional[Set]) -> None
  33. if value.startswith('-'):
  34. raise CommandError(
  35. "--no-binary / --only-binary 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. del new[:new.index(':all:') + 1]
  43. # Without a none, we want to discard everything as :all: covers it
  44. if ':none:' not in new:
  45. return
  46. for name in new:
  47. if name == ':none:':
  48. target.clear()
  49. continue
  50. name = canonicalize_name(name)
  51. other.discard(name)
  52. target.add(name)
  53. def get_allowed_formats(self, canonical_name):
  54. # type: (str) -> FrozenSet
  55. result = {"binary", "source"}
  56. if canonical_name in self.only_binary:
  57. result.discard('source')
  58. elif canonical_name in self.no_binary:
  59. result.discard('binary')
  60. elif ':all:' in self.only_binary:
  61. result.discard('source')
  62. elif ':all:' in self.no_binary:
  63. result.discard('binary')
  64. return frozenset(result)
  65. def disallow_binaries(self):
  66. # type: () -> None
  67. self.handle_mutual_excludes(
  68. ':all:', self.no_binary, self.only_binary,
  69. )