check.py 1.64 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3
import logging

from pip._internal.cli.base_command import Command
xuebingbing's avatar
xuebingbing committed
4
from pip._internal.cli.status_codes import ERROR, SUCCESS
xuebingbing's avatar
xuebingbing committed
5 6 7 8 9
from pip._internal.operations.check import (
    check_package_set,
    create_package_set_from_installed,
)
from pip._internal.utils.misc import write_output
xuebingbing's avatar
xuebingbing committed
10
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
xuebingbing's avatar
xuebingbing committed
11 12 13

logger = logging.getLogger(__name__)

xuebingbing's avatar
xuebingbing committed
14 15 16 17
if MYPY_CHECK_RUNNING:
    from typing import List, Any
    from optparse import Values

xuebingbing's avatar
xuebingbing committed
18 19 20 21 22 23 24 25

class CheckCommand(Command):
    """Verify installed packages have compatible dependencies."""

    usage = """
      %prog [options]"""

    def run(self, options, args):
xuebingbing's avatar
xuebingbing committed
26 27
        # type: (Values, List[Any]) -> int

xuebingbing's avatar
xuebingbing committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        package_set, parsing_probs = create_package_set_from_installed()
        missing, conflicting = check_package_set(package_set)

        for project_name in missing:
            version = package_set[project_name].version
            for dependency in missing[project_name]:
                write_output(
                    "%s %s requires %s, which is not installed.",
                    project_name, version, dependency[0],
                )

        for project_name in conflicting:
            version = package_set[project_name].version
            for dep_name, dep_version, req in conflicting[project_name]:
                write_output(
                    "%s %s has requirement %s, but you have %s %s.",
                    project_name, version, req, dep_name, dep_version,
                )

        if missing or conflicting or parsing_probs:
xuebingbing's avatar
xuebingbing committed
48
            return ERROR
xuebingbing's avatar
xuebingbing committed
49 50
        else:
            write_output("No broken requirements found.")
xuebingbing's avatar
xuebingbing committed
51
            return SUCCESS