Skip to content

Commit 84000b3

Browse files
committed
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] pypa#775
1 parent fe3d37b commit 84000b3

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

pip/commands/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pip.commands.freeze import FreezeCommand
99
from pip.commands.help import HelpCommand
1010
from pip.commands.list import ListCommand
11+
from pip.commands.check import CheckCommand
1112
from pip.commands.search import SearchCommand
1213
from pip.commands.show import ShowCommand
1314
from pip.commands.install import InstallCommand
@@ -29,6 +30,7 @@
2930
UnzipCommand.name: UnzipCommand,
3031
ZipCommand.name: ZipCommand,
3132
ListCommand.name: ListCommand,
33+
CheckCommand.name: CheckCommand,
3234
WheelCommand.name: WheelCommand,
3335
}
3436

@@ -39,6 +41,7 @@
3941
FreezeCommand,
4042
ListCommand,
4143
ShowCommand,
44+
CheckCommand,
4245
SearchCommand,
4346
WheelCommand,
4447
ZipCommand,

pip/commands/check.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from pip.log import logger
2+
from pip.basecommand import Command
3+
from pip.util import get_installed_distributions
4+
5+
6+
class CheckCommand(Command):
7+
"""Verify installed packages have compatible dependencies."""
8+
name = 'check'
9+
usage = """
10+
%prog [options]"""
11+
summary = 'Verify installed packages have compatible dependencies.'
12+
13+
def setup_logging(self):
14+
logger.move_stdout_to_stderr()
15+
16+
def run(self, options, args):
17+
all_requirements_met = True
18+
19+
installed = get_installed_distributions()
20+
for dist in installed:
21+
22+
missing_requirements = self.get_missing_requirements(dist, installed)
23+
for requirement in missing_requirements:
24+
logger.notify("%s %s requires %s, which is not installed." %
25+
(dist.project_name, dist.version, requirement.project_name))
26+
27+
incompatible_requirements = self.get_incompatible_requirements(dist, installed)
28+
for requirement, actual in incompatible_requirements:
29+
logger.notify("%s %s has requirement %s, but you have %s %s." %
30+
(dist.project_name, dist.version, requirement,
31+
actual.project_name, actual.version))
32+
33+
if missing_requirements or incompatible_requirements:
34+
all_requirements_met = False
35+
36+
if not all_requirements_met:
37+
return 1
38+
39+
def get_missing_requirements(self, dist, installed_dists):
40+
"""Return all of the requirements of `dist` that aren't present in
41+
`installed_dists`.
42+
43+
"""
44+
installed_names = set(d.project_name for d in installed_dists)
45+
46+
missing_requirements = set()
47+
for requirement in dist.requires():
48+
if requirement.project_name not in installed_names:
49+
missing_requirements.add(requirement)
50+
yield requirement
51+
52+
def get_incompatible_requirements(self, dist, installed_dists):
53+
"""Return all of the requirements of `dist` that are present in
54+
`installed_dists`, but have incompatible versions.
55+
56+
"""
57+
installed_dists_by_name = {}
58+
for installed_dist in installed_dists:
59+
installed_dists_by_name[installed_dist.project_name] = installed_dist
60+
61+
incompatible_requirements = set()
62+
for requirement in dist.requires():
63+
present_dist = installed_dists_by_name.get(requirement.project_name)
64+
65+
if present_dist and present_dist not in requirement:
66+
incompatible_requirements.add((requirement, present_dist))
67+
yield (requirement, present_dist)

tests/functional/test_check.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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, expect_stderr=True)
17+
18+
assert result.stderr == "ipdb 0.7 requires ipython, which is not installed.\n"
19+
assert result.returncode == 1
20+
21+
22+
def test_check_broken_dependency(script):
23+
# this will also install a compatible version of jinja2
24+
script.pip('install', 'flask==0.10.1')
25+
26+
# deliberately change dependency to a version that is too old
27+
script.pip('install', 'jinja2==2.3')
28+
29+
result = script.pip('check', expect_error=True, expect_stderr=True)
30+
31+
assert result.stderr == "Flask 0.10.1 has requirement Jinja2>=2.4, but you have Jinja2 2.3.\n"
32+
assert result.returncode == 1

0 commit comments

Comments
 (0)