-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a pip check
command.
#3750
Merged
Merged
Add a pip check
command.
#3750
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import logging | ||
|
||
from pip.basecommand import Command | ||
from pip.operations.check import ( | ||
check_requirements, get_installed_distributions) | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CheckCommand(Command): | ||
"""Verify installed packages have compatible dependencies.""" | ||
name = 'check' | ||
usage = """ | ||
%prog [options]""" | ||
summary = 'Verify installed packages have compatible dependencies.' | ||
|
||
def run(self, options, args): | ||
installed = get_installed_distributions(skip=()) | ||
missing_reqs_dict, incompatible_reqs_dict = check_requirements() | ||
|
||
for dist in installed: | ||
key = '%s==%s' % (dist.project_name, dist.version) | ||
|
||
for requirement in missing_reqs_dict.get(key, []): | ||
logger.info( | ||
"%s %s requires %s, which is not installed.", | ||
dist.project_name, dist.version, requirement.project_name) | ||
|
||
for requirement, actual in incompatible_reqs_dict.get(key, []): | ||
logger.info( | ||
"%s %s has requirement %s, but you have %s %s.", | ||
dist.project_name, dist.version, requirement, | ||
actual.project_name, actual.version) | ||
|
||
if missing_reqs_dict or incompatible_reqs_dict: | ||
return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from pip.utils import get_installed_distributions | ||
|
||
|
||
def check_requirements(): | ||
installed = get_installed_distributions(skip=()) | ||
missing_reqs_dict = {} | ||
incompatible_reqs_dict = {} | ||
|
||
for dist in installed: | ||
key = '%s==%s' % (dist.project_name, dist.version) | ||
|
||
missing_reqs = list(get_missing_reqs(dist, installed)) | ||
if missing_reqs: | ||
missing_reqs_dict[key] = missing_reqs | ||
|
||
incompatible_reqs = list(get_incompatible_reqs(dist, installed)) | ||
if incompatible_reqs: | ||
incompatible_reqs_dict[key] = incompatible_reqs | ||
|
||
return (missing_reqs_dict, incompatible_reqs_dict) | ||
|
||
|
||
def get_missing_reqs(dist, installed_dists): | ||
"""Return all of the requirements of `dist` that aren't present in | ||
`installed_dists`. | ||
|
||
""" | ||
installed_names = set(d.project_name.lower() for d in installed_dists) | ||
missing_requirements = set() | ||
|
||
for requirement in dist.requires(): | ||
if requirement.project_name.lower() not in installed_names: | ||
missing_requirements.add(requirement) | ||
yield requirement | ||
|
||
|
||
def get_incompatible_reqs(dist, installed_dists): | ||
"""Return all of the requirements of `dist` that are present in | ||
`installed_dists`, but have incompatible versions. | ||
|
||
""" | ||
installed_dists_by_name = {} | ||
for installed_dist in installed_dists: | ||
installed_dists_by_name[installed_dist.project_name] = installed_dist | ||
|
||
incompatible_requirements = set() | ||
for requirement in dist.requires(): | ||
present_dist = installed_dists_by_name.get(requirement.project_name) | ||
|
||
if present_dist and present_dist not in requirement: | ||
incompatible_requirements.add((requirement, present_dist)) | ||
yield (requirement, present_dist) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
def test_check_clean(script): | ||
"""On a clean environment, check shouldn't return anything. | ||
|
||
""" | ||
result = script.pip('check') | ||
assert result.stdout == "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a general UI principle, it's probably better if it explicitly says something like "No problems found 👍" instead of just returning with an empty stdout. |
||
|
||
|
||
def test_check_missing_dependency(script): | ||
# this will also install ipython, a dependency | ||
script.pip('install', 'ipdb==0.7') | ||
|
||
# deliberately remove the dependency | ||
script.pip('uninstall', 'ipython', '--yes') | ||
|
||
result = script.pip('check', expect_error=True) | ||
|
||
assert result.stdout == ("ipdb 0.7 requires ipython, " | ||
"which is not installed.\n") | ||
assert result.returncode == 1 | ||
|
||
|
||
def test_check_missing_dependency_normalize_case(script): | ||
# Install some things | ||
script.pip('install', 'devpi-web==2.2.2') | ||
script.pip('install', 'pyramid==1.5.2') | ||
|
||
# deliberately remove some dependencies | ||
script.pip('uninstall', 'pygments', '--yes') | ||
script.pip('uninstall', 'zope.deprecation', '--yes') | ||
|
||
result = script.pip('check', expect_error=True) | ||
|
||
assert ('devpi-web 2.2.2 requires pygments, ' | ||
'which is not installed.') in result.stdout | ||
assert ('pyramid 1.5.2 requires zope.deprecation, ' | ||
'which is not installed.') in result.stdout | ||
assert result.returncode == 1 | ||
|
||
|
||
def test_check_broken_dependency(script): | ||
# this will also install a compatible version of jinja2 | ||
script.pip('install', 'flask==0.10.1') | ||
|
||
# deliberately change dependency to a version that is too old | ||
script.pip('install', 'jinja2==2.3') | ||
|
||
result = script.pip('check', expect_error=True) | ||
|
||
assert result.stdout == ("Flask 0.10.1 has requirement Jinja2>=2.4, " | ||
"but you have Jinja2 2.3.\n") | ||
assert result.returncode == 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incompatible_requirements
is never used?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently yes, this could be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4065