Skip to content

Commit

Permalink
refactor format control and rename test functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sinscary committed Aug 28, 2018
1 parent e10a613 commit b818b83
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/pip/_internal/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _get_candidates(self, link, package_name):
return []

canonical_name = canonicalize_name(package_name)
formats = self.format_control.fmt_ctl_formats(
formats = self.format_control.get_allowed_formats(
canonical_name
)
if not self.allowed_formats.intersection(formats):
Expand Down
18 changes: 15 additions & 3 deletions src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def getname(n):
names = ["build_options", "global_options", "install_options"]
if any(map(getname, names)):
control = options.format_control
control.fmt_ctl_no_binary()
control.disallow_binaries()
warnings.warn(
'Disabling all use of wheels due to the use of --build-options '
'/ --global-options / --install-options.', stacklevel=2,
Expand Down Expand Up @@ -397,11 +397,23 @@ def editable():
) # type: Any


def handle_cli_no_binary(option, opt_str, value, parser):
existing = getattr(parser.values, option.dest)
FormatControl.handle_mutual_excludes(
value, existing.no_binary, existing.only_binary,
)

def handle_cli_only_binary(option, opt_str, value, parser):
existing = getattr(parser.values, option.dest)
FormatControl.handle_mutual_excludes(
value, existing.only_binary, existing.no_binary,
)

def no_binary():
format_control = FormatControl(set(), set())
return Option(
"--no-binary", dest="format_control", action="callback",
callback=format_control._handle_no_binary, type="str",
callback=handle_cli_no_binary, type="str",
default=format_control,
help="Do not use binary packages. Can be supplied multiple times, and "
"each time adds to the existing value. Accepts either :all: to "
Expand All @@ -416,7 +428,7 @@ def only_binary():
format_control = FormatControl(set(), set())
return Option(
"--only-binary", dest="format_control", action="callback",
callback=format_control._handle_only_binary, type="str",
callback=handle_cli_only_binary, type="str",
default=format_control,
help="Do not use source packages. Can be supplied multiple times, and "
"each time adds to the existing value. Accepts either :all: to "
Expand Down
24 changes: 0 additions & 24 deletions src/pip/_internal/commands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,7 @@ def run(self, options, args):
else:
python_versions = None

<<<<<<< HEAD
cmdoptions.check_dist_restriction(options)
=======
dist_restriction_set = any([
options.python_version,
options.platform,
options.abi,
options.implementation,
])
fmt_ctl = FormatControl(set(), {':all:'})
no_sdist_dependencies = (
(
options.format_control.no_binary != fmt_ctl.no_binary or
options.format_control.only_binary != fmt_ctl.only_binary
) and not options.ignore_dependencies
)
if dist_restriction_set and no_sdist_dependencies:
raise CommandError(
"When restricting platform and interpreter constraints using "
"--python-version, --platform, --abi, or --implementation, "
"either --no-deps must be set, or --only-binary=:all: must be "
"set and --no-binary must not be set (or must be set to "
":none:)."
)
>>>>>>> Refactoring: Move FormatControl to separate class

options.src_dir = os.path.abspath(options.src_dir)
options.download_dir = normalize_path(options.download_dir)
Expand Down
29 changes: 14 additions & 15 deletions src/pip/_internal/format_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@


class FormatControl(object):
"""This class has two fields, no_binary and only_binary.
"""A helper class for controlling formats from which packages are installed.
If a field is falsy, it isn't set. If it is {':all:'}, it should match all
packages except those listed in the other field. Only one field can be set
to {':all:'} at a time. The rest of the time exact package name matches
are listed, with any given package only showing up in one field at a time.
"""
def __init__(self, no_binary, only_binary):
def __init__(self, no_binary=set(), only_binary=set()):
self.no_binary = no_binary
self.only_binary = only_binary

def _handle_no_binary(self, option, opt_str, value, parser):
existing = getattr(parser.values, option.dest)
self.fmt_ctl_handle_mutual_exclude(
value, existing.no_binary, existing.only_binary,
)
def __eq__(self, other):
return self.__dict__ == other.__dict__

def _handle_only_binary(self, option, opt_str, value, parser):
existing = getattr(parser.values, option.dest)
self.fmt_ctl_handle_mutual_exclude(
value, existing.only_binary, existing.no_binary,
def __repr__(self):
return "{}({}, {})".format(
self.__class__.__name__,
self.no_binary,
self.only_binary
)

def fmt_ctl_handle_mutual_exclude(self, value, target, other):
@staticmethod
def handle_mutual_excludes(value, target, other):
new = value.split(',')
while ':all:' in new:
other.clear()
Expand All @@ -42,7 +41,7 @@ def fmt_ctl_handle_mutual_exclude(self, value, target, other):
other.discard(name)
target.add(name)

def fmt_ctl_formats(self, canonical_name):
def get_allowed_formats(self, canonical_name):
result = {"binary", "source"}
if canonical_name in self.only_binary:
result.discard('source')
Expand All @@ -54,8 +53,8 @@ def fmt_ctl_formats(self, canonical_name):
result.discard('binary')
return frozenset(result)

def fmt_ctl_no_binary(self):
self.fmt_ctl_handle_mutual_exclude(
def disallow_binaries(self):
self.handle_mutual_excludes(
':all:', self.no_binary, self.only_binary,
)

Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def find_all_candidates(self, project_name):
logger.debug('* %s', location)

canonical_name = canonicalize_name(project_name)
formats = self.format_control.fmt_ctl_formats(canonical_name)
formats = self.format_control.get_allowed_formats(canonical_name)
search = Search(project_name, canonical_name, formats)
find_links_versions = self._package_versions(
# We trust every directly linked archive in find_links
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ def build(self, requirements, session, autobuilding=False):
if index.egg_info_matches(base, None, link) is None:
# E.g. local directory. Build wheel just for this run.
ephem_cache = True
if "binary" not in format_control.fmt_ctl_formats(
if "binary" not in format_control.get_allowed_formats(
canonicalize_name(req.name)):
logger.info(
"Skipping bdist_wheel for %s, due to binaries "
Expand Down
13 changes: 0 additions & 13 deletions tests/unit/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,16 +584,3 @@ def test_find_all_candidates_find_links_and_index(data):
versions = finder.find_all_candidates('simple')
# first the find-links versions then the page versions
assert [str(v.version) for v in versions] == ['3.0', '2.0', '1.0', '1.0']


def test_fmt_ctl_matches():
fmt = FormatControl(set(), set())
assert fmt.fmt_ctl_formats("fred") == frozenset(["source", "binary"])
fmt = FormatControl({"fred"}, set())
assert fmt.fmt_ctl_formats("fred") == frozenset(["source"])
fmt = FormatControl({"fred"}, {":all:"})
assert fmt.fmt_ctl_formats("fred") == frozenset(["source"])
fmt = FormatControl(set(), {"fred"})
assert fmt.fmt_ctl_formats("fred") == frozenset(["binary"])
fmt = FormatControl({":all:"}, {"fred"})
assert fmt.fmt_ctl_formats("fred") == frozenset(["binary"])
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ def test_comma_separated_values():
expected = FormatControl({'1', '2', '3'}, set())
assert cmd.options.format_control.only_binary == expected.only_binary
assert cmd.options.format_control.no_binary == expected.no_binary

def test_fmt_ctl_matches():
fmt = FormatControl(set(), set())
assert fmt.get_allowed_formats("fred") == frozenset(["source", "binary"])
fmt = FormatControl({"fred"}, set())
assert fmt.get_allowed_formats("fred") == frozenset(["source"])
fmt = FormatControl({"fred"}, {":all:"})
assert fmt.get_allowed_formats("fred") == frozenset(["source"])
fmt = FormatControl(set(), {"fred"})
assert fmt.get_allowed_formats("fred") == frozenset(["binary"])
fmt = FormatControl({":all:"}, {"fred"})
assert fmt.get_allowed_formats("fred") == frozenset(["binary"])

0 comments on commit b818b83

Please sign in to comment.