Skip to content

Commit 76c3561

Browse files
Wilfredmsabramo
authored andcommitted
Add a pip check command.
This command ensures that all packages installed have all the requirements they need, and that requirements have compatible versions. This is useful because pip can install incompatible dependencies[1], or a user may have manually (un)installed a package. [1] #775
1 parent 7fbde56 commit 76c3561

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

pip/commands/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pip.commands.freeze import FreezeCommand
88
from pip.commands.help import HelpCommand
99
from pip.commands.list import ListCommand
10+
from pip.commands.check import CheckCommand
1011
from pip.commands.search import SearchCommand
1112
from pip.commands.show import ShowCommand
1213
from pip.commands.install import InstallCommand
@@ -27,6 +28,7 @@
2728
UnzipCommand.name: UnzipCommand,
2829
ZipCommand.name: ZipCommand,
2930
ListCommand.name: ListCommand,
31+
CheckCommand.name: CheckCommand,
3032
WheelCommand.name: WheelCommand,
3133
}
3234

@@ -37,6 +39,7 @@
3739
FreezeCommand,
3840
ListCommand,
3941
ShowCommand,
42+
CheckCommand,
4043
SearchCommand,
4144
WheelCommand,
4245
ZipCommand,

pip/commands/check.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import logging
2+
3+
from pip.basecommand import Command
4+
from pip.operations.check import (
5+
check_requirements, get_installed_distributions)
6+
7+
8+
logger = logging.getLogger(__name__)
9+
10+
11+
class CheckCommand(Command):
12+
"""Verify installed packages have compatible dependencies."""
13+
name = 'check'
14+
usage = """
15+
%prog [options]"""
16+
summary = 'Verify installed packages have compatible dependencies.'
17+
18+
def run(self, options, args):
19+
installed = get_installed_distributions(skip=())
20+
missing_reqs_dict, incompatible_reqs_dict = check_requirements()
21+
22+
for dist in installed:
23+
key = '%s==%s' % (dist.project_name, dist.version)
24+
25+
for requirement in missing_reqs_dict.get(key, []):
26+
logger.info(
27+
"%s %s requires %s, which is not installed.",
28+
dist.project_name, dist.version, requirement.project_name)
29+
30+
for requirement, actual in incompatible_reqs_dict.get(key, []):
31+
logger.info(
32+
"%s %s has requirement %s, but you have %s %s.",
33+
dist.project_name, dist.version, requirement,
34+
actual.project_name, actual.version)
35+
36+
if missing_reqs_dict or incompatible_reqs_dict:
37+
return 1

pip/operations/check.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from pip.utils import get_installed_distributions
2+
3+
4+
def check_requirements():
5+
installed = get_installed_distributions(skip=())
6+
missing_reqs_dict = {}
7+
incompatible_reqs_dict = {}
8+
9+
for dist in installed:
10+
key = '%s==%s' % (dist.project_name, dist.version)
11+
12+
missing_reqs = list(get_missing_reqs(dist, installed))
13+
if missing_reqs:
14+
missing_reqs_dict[key] = missing_reqs
15+
16+
incompatible_reqs = list(get_incompatible_reqs(dist, installed))
17+
if incompatible_reqs:
18+
incompatible_reqs_dict[key] = incompatible_reqs
19+
20+
return (missing_reqs_dict, incompatible_reqs_dict)
21+
22+
23+
def get_missing_reqs(dist, installed_dists):
24+
"""Return all of the requirements of `dist` that aren't present in
25+
`installed_dists`.
26+
27+
"""
28+
installed_names = set(d.project_name.lower() for d in installed_dists)
29+
missing_requirements = set()
30+
31+
for requirement in dist.requires():
32+
if requirement.project_name.lower() not in installed_names:
33+
missing_requirements.add(requirement)
34+
yield requirement
35+
36+
37+
def get_incompatible_reqs(dist, installed_dists):
38+
"""Return all of the requirements of `dist` that are present in
39+
`installed_dists`, but have incompatible versions.
40+
41+
"""
42+
installed_dists_by_name = {}
43+
for installed_dist in installed_dists:
44+
installed_dists_by_name[installed_dist.project_name] = installed_dist
45+
46+
incompatible_requirements = set()
47+
for requirement in dist.requires():
48+
present_dist = installed_dists_by_name.get(requirement.project_name)
49+
50+
if present_dist and present_dist not in requirement:
51+
incompatible_requirements.add((requirement, present_dist))
52+
yield (requirement, present_dist)

tests/functional/test_check.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
def test_check_clean(script):
2+
"""On a clean environment, check shouldn't return anything.
3+
4+
"""
5+
result = script.pip('check')
6+
assert result.stdout == ""
7+
8+
9+
def test_check_missing_dependency(script):
10+
# this will also install ipython, a dependency
11+
script.pip('install', 'ipdb==0.7')
12+
13+
# deliberately remove the dependency
14+
script.pip('uninstall', 'ipython', '--yes')
15+
16+
result = script.pip('check', expect_error=True)
17+
18+
assert result.stdout == ("ipdb 0.7 requires ipython, "
19+
"which is not installed.\n")
20+
assert result.returncode == 1
21+
22+
23+
def test_check_missing_dependency_normalize_case(script):
24+
# Install some things
25+
script.pip('install', 'devpi-web==2.2.2')
26+
script.pip('install', 'pyramid==1.5.2')
27+
28+
# deliberately remove some dependencies
29+
script.pip('uninstall', 'pygments', '--yes')
30+
script.pip('uninstall', 'zope.deprecation', '--yes')
31+
32+
result = script.pip('check', expect_error=True)
33+
34+
assert ('devpi-web 2.2.2 requires pygments, '
35+
'which is not installed.') in result.stdout
36+
assert ('pyramid 1.5.2 requires zope.deprecation, '
37+
'which is not installed.') in result.stdout
38+
assert result.returncode == 1
39+
40+
41+
def test_check_broken_dependency(script):
42+
# this will also install a compatible version of jinja2
43+
script.pip('install', 'flask==0.10.1')
44+
45+
# deliberately change dependency to a version that is too old
46+
script.pip('install', 'jinja2==2.3')
47+
48+
result = script.pip('check', expect_error=True)
49+
50+
assert result.stdout == ("Flask 0.10.1 has requirement Jinja2>=2.4, "
51+
"but you have Jinja2 2.3.\n")
52+
assert result.returncode == 1

0 commit comments

Comments
 (0)