mirror of https://github.com/pypa/pip
Merge 871ee502bf
into 2a0acb595c
This commit is contained in:
commit
5a59bfca5f
|
@ -0,0 +1 @@
|
||||||
|
Add ``--ignore-packages`` flag to ``pip check`` to ignore specific packages.
|
|
@ -1,6 +1,8 @@
|
||||||
|
import functools
|
||||||
import logging
|
import logging
|
||||||
|
import operator
|
||||||
from optparse import Values
|
from optparse import Values
|
||||||
from typing import List
|
from typing import Callable, List, Optional
|
||||||
|
|
||||||
from pip._internal.cli.base_command import Command
|
from pip._internal.cli.base_command import Command
|
||||||
from pip._internal.cli.status_codes import ERROR, SUCCESS
|
from pip._internal.cli.status_codes import ERROR, SUCCESS
|
||||||
|
@ -20,10 +22,36 @@ class CheckCommand(Command):
|
||||||
usage = """
|
usage = """
|
||||||
%prog [options]"""
|
%prog [options]"""
|
||||||
|
|
||||||
|
def add_options(self) -> None:
|
||||||
|
self.cmd_opts.add_option(
|
||||||
|
"--ignore-packages",
|
||||||
|
dest="ignore_packages",
|
||||||
|
action="append",
|
||||||
|
default=[],
|
||||||
|
help="Ignore packages.",
|
||||||
|
)
|
||||||
|
self.cmd_opts.add_option(
|
||||||
|
"--recursive-ignore",
|
||||||
|
dest="recursive_ignore",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Ignore sub-dependencies.",
|
||||||
|
)
|
||||||
|
self.parser.insert_option_group(0, self.cmd_opts)
|
||||||
|
|
||||||
def run(self, options: Values, args: List[str]) -> int:
|
def run(self, options: Values, args: List[str]) -> int:
|
||||||
package_set, parsing_probs = create_package_set_from_installed()
|
package_set, parsing_probs = create_package_set_from_installed()
|
||||||
warn_legacy_versions_and_specifiers(package_set)
|
warn_legacy_versions_and_specifiers(package_set)
|
||||||
missing, conflicting = check_package_set(package_set)
|
should_ignore: Optional[Callable[[str], bool]] = (
|
||||||
|
functools.partial(operator.contains, options.ignore_packages)
|
||||||
|
if options.ignore_packages
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
missing, conflicting = check_package_set(
|
||||||
|
package_set,
|
||||||
|
should_ignore,
|
||||||
|
should_ignore if options.recursive_ignore else None,
|
||||||
|
)
|
||||||
|
|
||||||
for project_name in missing:
|
for project_name in missing:
|
||||||
version = package_set[project_name].version
|
version = package_set[project_name].version
|
||||||
|
|
|
@ -52,12 +52,17 @@ def create_package_set_from_installed() -> Tuple[PackageSet, bool]:
|
||||||
|
|
||||||
|
|
||||||
def check_package_set(
|
def check_package_set(
|
||||||
package_set: PackageSet, should_ignore: Optional[Callable[[str], bool]] = None
|
package_set: PackageSet,
|
||||||
|
should_ignore: Optional[Callable[[str], bool]] = None,
|
||||||
|
should_ignore_dependencies: Optional[Callable[[str], bool]] = None,
|
||||||
) -> CheckResult:
|
) -> CheckResult:
|
||||||
"""Check if a package set is consistent
|
"""Check if a package set is consistent
|
||||||
|
|
||||||
If should_ignore is passed, it should be a callable that takes a
|
If should_ignore/should_ignore_dependencies is passed, it should
|
||||||
package name and returns a boolean.
|
be a callable that takes a package name and returns a boolean.
|
||||||
|
|
||||||
|
should_ignore_dependencies should be used to filter out dependencies
|
||||||
|
of packages specified in package_set.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
warn_legacy_versions_and_specifiers(package_set)
|
warn_legacy_versions_and_specifiers(package_set)
|
||||||
|
@ -76,6 +81,10 @@ def check_package_set(
|
||||||
for req in package_detail.dependencies:
|
for req in package_detail.dependencies:
|
||||||
name = canonicalize_name(req.name)
|
name = canonicalize_name(req.name)
|
||||||
|
|
||||||
|
if should_ignore_dependencies and should_ignore_dependencies(name):
|
||||||
|
logger.debug("%s was ignored because --recursive-ignore is set", name)
|
||||||
|
continue
|
||||||
|
|
||||||
# Check if it's missing
|
# Check if it's missing
|
||||||
if name not in package_set:
|
if name not in package_set:
|
||||||
missed = True
|
missed = True
|
||||||
|
|
|
@ -309,3 +309,44 @@ def test_check_include_work_dir_pkg(script: PipTestEnvironment) -> None:
|
||||||
expected_lines = ("simple 1.0 requires missing, which is not installed.",)
|
expected_lines = ("simple 1.0 requires missing, which is not installed.",)
|
||||||
assert matches_expected_lines(result.stdout, expected_lines)
|
assert matches_expected_lines(result.stdout, expected_lines)
|
||||||
assert result.returncode == 1
|
assert result.returncode == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_ignore_packages(script: PipTestEnvironment) -> None:
|
||||||
|
package_a_path = create_test_package_with_setup(
|
||||||
|
script,
|
||||||
|
name="package_A",
|
||||||
|
version="1.0",
|
||||||
|
install_requires=["missing>=1.0"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# Without dependency
|
||||||
|
result = script.pip("install", "--no-index", package_a_path, "--no-deps")
|
||||||
|
assert "Successfully installed package-A-1.0" in result.stdout, str(result)
|
||||||
|
|
||||||
|
result = script.pip("check", "--ignore-packages=package-a")
|
||||||
|
expected_lines = ("No broken requirements found.",)
|
||||||
|
assert matches_expected_lines(result.stdout, expected_lines)
|
||||||
|
assert result.returncode == 0
|
||||||
|
|
||||||
|
result = script.pip("check", "--ignore-packages=missing", expect_error=True)
|
||||||
|
expected_lines = ("package-a 1.0 requires missing, which is not installed.",)
|
||||||
|
assert matches_expected_lines(result.stdout, expected_lines)
|
||||||
|
assert result.returncode == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_ignore_packages_recursive(script: PipTestEnvironment) -> None:
|
||||||
|
package_a_path = create_test_package_with_setup(
|
||||||
|
script,
|
||||||
|
name="package_A",
|
||||||
|
version="1.0",
|
||||||
|
install_requires=["missing>=1.0"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# Without dependency
|
||||||
|
result = script.pip("install", "--no-index", package_a_path, "--no-deps")
|
||||||
|
assert "Successfully installed package-A-1.0" in result.stdout, str(result)
|
||||||
|
|
||||||
|
result = script.pip("check", "--ignore-packages=missing", "--recursive-ignore")
|
||||||
|
expected_lines = ("No broken requirements found.",)
|
||||||
|
assert matches_expected_lines(result.stdout, expected_lines)
|
||||||
|
assert result.returncode == 0
|
||||||
|
|
Loading…
Reference in New Issue