Skip to content

Commit

Permalink
Fix pip --no-binary/--only-binary options
Browse files Browse the repository at this point in the history
  • Loading branch information
q0w committed Jan 6, 2023
1 parent b9b4d02 commit 771c102
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2814.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle properly pip ``--no-binary`` / ``--only-binary`` options in requirements.txt format files.
4 changes: 2 additions & 2 deletions src/tox/tox_env/python/pip/req/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def _global_options(parser: ArgumentParser) -> None:
parser.add_argument("-r", "--requirement", action=AddUniqueAction, dest="requirements")
parser.add_argument("-e", "--editable", action=AddUniqueAction, dest="editables")
parser.add_argument("-f", "--find-links", action=AddUniqueAction)
parser.add_argument("--no-binary", choices=[":all:", ":none:"]) # TODO: colon separated package names
parser.add_argument("--only-binary", choices=[":all:", ":none:"]) # TODO: colon separated package names
parser.add_argument("--no-binary")
parser.add_argument("--only-binary")
parser.add_argument("--prefer-binary", action="store_true", default=False)
parser.add_argument("--require-hashes", action="store_true", default=False)
parser.add_argument("--pre", action="store_true", default=False)
Expand Down
13 changes: 10 additions & 3 deletions src/tox/tox_env/python/pip/req/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from packaging.requirements import InvalidRequirement, Requirement

from .args import build_parser
from .util import VCS, get_url_scheme, is_url, url_to_path
from .util import VCS, get_url_scheme, handle_binary_option, is_url, url_to_path

# Matches environment variable-style values in '${MY_VARIABLE_1}' with the variable name consisting of only uppercase
# letters, digits or the '_' (underscore). This follows the POSIX standard defined in IEEE Std 1003.1, 2013 Edition.
Expand Down Expand Up @@ -341,10 +341,17 @@ def _merge_option_line(self, base_opt: Namespace, opt: Namespace, filename: str)
base_opt.trusted_hosts = []
if host not in base_opt.trusted_hosts:
base_opt.trusted_hosts.append(host)

no_binary = base_opt.no_binary if hasattr(base_opt, "no_binary") else set()
only_binary = base_opt.only_binary if hasattr(base_opt, "only_binary") else set()
if opt.no_binary:
base_opt.no_binary = opt.no_binary
handle_binary_option(opt.no_binary, no_binary, only_binary)
if opt.only_binary:
base_opt.only_binary = opt.only_binary
handle_binary_option(opt.only_binary, only_binary, no_binary)
if no_binary:
base_opt.no_binary = no_binary
if only_binary:
base_opt.only_binary = only_binary

@staticmethod
def _break_args_options(line: str) -> tuple[str, str]:
Expand Down
20 changes: 20 additions & 0 deletions src/tox/tox_env/python/pip/req/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from urllib.parse import urlsplit
from urllib.request import url2pathname

from packaging.utils import canonicalize_name

VCS = ["ftp", "ssh", "git", "hg", "bzr", "sftp", "svn"]
VALID_SCHEMAS = ["http", "https", "file"] + VCS

Expand All @@ -26,3 +28,21 @@ def url_to_path(url: str) -> str:
raise ValueError(f"non-local file URIs are not supported on this platform: {url!r}")
path = url2pathname(netloc + path)
return path


def handle_binary_option(value: str, target: set[str], other: set[str]) -> None:
new = value.split(",")
while ":all:" in new:
other.clear()
target.clear()
target.add(":all:")
del new[: new.index(":all:") + 1]
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)

0 comments on commit 771c102

Please sign in to comment.