mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 09:17:53 +00:00
Merge pull request #389 from balloob/pip-fixes
Fix pip not detecting package installed
This commit is contained in:
commit
3947ed3c2b
@ -1,6 +1,6 @@
|
||||
"""Helpers to install PyPi packages."""
|
||||
import os
|
||||
import logging
|
||||
import os
|
||||
import pkg_resources
|
||||
import subprocess
|
||||
import sys
|
||||
@ -15,25 +15,24 @@ def install_package(package, upgrade=True, target=None):
|
||||
"""Install a package on PyPi. Accepts pip compatible package strings.
|
||||
Return boolean if install successfull."""
|
||||
# Not using 'import pip; pip.main([])' because it breaks the logger
|
||||
args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
|
||||
|
||||
if upgrade:
|
||||
args.append('--upgrade')
|
||||
if target:
|
||||
args += ['--target', os.path.abspath(target)]
|
||||
|
||||
with INSTALL_LOCK:
|
||||
if check_package_exists(package, target):
|
||||
return True
|
||||
|
||||
_LOGGER.info('Attempting install of %s', package)
|
||||
args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
|
||||
if upgrade:
|
||||
args.append('--upgrade')
|
||||
if target:
|
||||
args += ['--target', os.path.abspath(target)]
|
||||
|
||||
try:
|
||||
return 0 == subprocess.call(args)
|
||||
except subprocess.SubprocessError:
|
||||
return False
|
||||
|
||||
|
||||
def check_package_exists(package, target=None):
|
||||
def check_package_exists(package, target):
|
||||
"""Check if a package exists.
|
||||
Returns True when the requirement is met.
|
||||
Returns False when the package is not installed or doesn't meet req."""
|
||||
@ -43,16 +42,5 @@ def check_package_exists(package, target=None):
|
||||
# This is a zip file
|
||||
req = pkg_resources.Requirement.parse(urlparse(package).fragment)
|
||||
|
||||
if target:
|
||||
work_set = pkg_resources.WorkingSet([target])
|
||||
search_fun = work_set.find
|
||||
|
||||
else:
|
||||
search_fun = pkg_resources.get_distribution
|
||||
|
||||
try:
|
||||
result = search_fun(req)
|
||||
except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict):
|
||||
return False
|
||||
|
||||
return bool(result)
|
||||
return any(dist in req for dist in
|
||||
pkg_resources.find_distributions(target))
|
||||
|
Loading…
x
Reference in New Issue
Block a user