1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/src/pip/_internal/models/format_control.py

83 lines
2.6 KiB
Python
Raw Normal View History

# The following comment should be removed at some point in the future.
# mypy: strict-optional=False
# mypy: disallow-untyped-defs=False
from pip._vendor.packaging.utils import canonicalize_name
2018-10-05 16:23:22 +02:00
from pip._internal.exceptions import CommandError
2018-10-28 17:42:15 +01:00
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
2019-02-22 12:17:07 +01:00
from typing import Optional, Set, FrozenSet
2018-10-28 17:42:15 +01:00
class FormatControl(object):
2018-11-21 09:53:48 +01:00
"""Helper for managing formats from which a package can be installed.
"""
2018-11-21 09:53:48 +01:00
def __init__(self, no_binary=None, only_binary=None):
2019-11-12 19:08:48 +01:00
# type: (Optional[Set[str]], Optional[Set[str]]) -> None
2018-11-21 09:53:48 +01:00
if no_binary is None:
no_binary = set()
if only_binary is None:
only_binary = set()
self.no_binary = no_binary
self.only_binary = only_binary
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "{}({}, {})".format(
self.__class__.__name__,
self.no_binary,
self.only_binary
)
@staticmethod
def handle_mutual_excludes(value, target, other):
2019-11-12 19:08:48 +01:00
# type: (str, Optional[Set[str]], Optional[Set[str]]) -> None
if value.startswith('-'):
raise CommandError(
"--no-binary / --only-binary option requires 1 argument."
)
new = value.split(',')
while ':all:' in new:
other.clear()
target.clear()
target.add(':all:')
del new[:new.index(':all:') + 1]
# Without a none, we want to discard everything as :all: covers it
if ':none:' not in new:
return
for name in new:
if name == ':none:':
target.clear()
continue
name = canonicalize_name(name)
other.discard(name)
target.add(name)
def get_allowed_formats(self, canonical_name):
2019-11-12 19:08:48 +01:00
# type: (str) -> FrozenSet[str]
result = {"binary", "source"}
if canonical_name in self.only_binary:
result.discard('source')
elif canonical_name in self.no_binary:
result.discard('binary')
elif ':all:' in self.only_binary:
result.discard('source')
elif ':all:' in self.no_binary:
result.discard('binary')
return frozenset(result)
def disallow_binaries(self):
2018-11-21 09:53:48 +01:00
# type: () -> None
self.handle_mutual_excludes(
':all:', self.no_binary, self.only_binary,
)