Skip to content

Commit

Permalink
Extract requirement_is_installed() from setuptools_supports_wheels()
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jan 10, 2016
1 parent ca099ba commit 97569e2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
18 changes: 4 additions & 14 deletions pip_accel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
makedirs,
match_option,
match_option_with_value,
requirement_is_installed,
same_directories,
uninstall,
)
Expand All @@ -70,7 +71,6 @@
from humanfriendly import concatenate, Timer, pluralize
from pip import index as pip_index_module
from pip import wheel as pip_wheel_module
from pip._vendor import pkg_resources
from pip.commands import install as pip_install_module
from pip.commands.install import InstallCommand
from pip.exceptions import DistributionNotFound
Expand Down Expand Up @@ -247,20 +247,10 @@ def setuptools_supports_wheels(self):
"""
Check whether setuptools should be upgraded to ``>= 0.8`` for wheel support.
:returns: :data:`True` when setuptools needs to be upgraded, :data:`False` otherwise.
:returns: :data:`True` when setuptools 0.8 or higher is already
installed, :data:`False` otherwise (it needs to be upgraded).
"""
# Don't use pkg_resources.Requirement.parse, to avoid the override
# in distribute, that converts `setuptools' to `distribute'.
setuptools_requirement = next(pkg_resources.parse_requirements('setuptools >= 0.8'))
try:
installed_setuptools = pkg_resources.get_distribution('setuptools')
if installed_setuptools in setuptools_requirement:
# setuptools >= 0.8 is already installed; nothing to do.
return True
except pkg_resources.DistributionNotFound:
pass
# We need to install setuptools >= 0.8.
return False
return requirement_is_installed('setuptools >= 0.8')

def get_requirements(self, arguments, max_retries=None, use_wheels=False):
"""
Expand Down
26 changes: 24 additions & 2 deletions pip_accel/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Utility functions for the pip accelerator.
#
# Author: Peter Odding <[email protected]>
# Last Change: December 28, 2015
# Last Change: January 10, 2016
# URL: https://github.com/paylogic/pip-accel

"""
Expand All @@ -26,7 +26,12 @@
# External dependencies.
from humanfriendly import parse_path
from pip.commands.uninstall import UninstallCommand
from pkg_resources import WorkingSet
from pip._vendor.pkg_resources import (
DistributionNotFound,
WorkingSet,
get_distribution,
parse_requirements,
)

# Initialize a logger for this module.
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -254,6 +259,23 @@ def __exit__(self, exc_type=None, exc_value=None, traceback=None):
replace_file(self.temporary_file, self.filename)


def requirement_is_installed(expr):
"""
Check whether a requirement is installed.
:param expr: A requirement specification similar to those used in pip
requirement files (a string).
:returns: :data:`True` if the requirement is available (installed),
:data:`False` otherwise.
"""
required_dist = next(parse_requirements(expr))
try:
installed_dist = get_distribution(required_dist.key)
return installed_dist in required_dist
except DistributionNotFound:
return False


def is_installed(package_name):
"""
Check whether a package is installed in the current environment.
Expand Down

0 comments on commit 97569e2

Please sign in to comment.