Merge pull request #326 from rmkraus/fix_pip
Fix pip installation issues.
This commit is contained in:
commit
e61299a46f
1 changed files with 37 additions and 3 deletions
|
@ -1,19 +1,53 @@
|
||||||
"""Helpers to install PyPi packages."""
|
"""Helpers to install PyPi packages."""
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
import pkg_resources
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import threading
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
INSTALL_LOCK = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def install_package(package, upgrade=False, target=None):
|
def install_package(package, upgrade=True, target=None):
|
||||||
"""Install a package on PyPi. Accepts pip compatible package strings.
|
"""Install a package on PyPi. Accepts pip compatible package strings.
|
||||||
Return boolean if install successfull."""
|
Return boolean if install successfull."""
|
||||||
# Not using 'import pip; pip.main([])' because it breaks the logger
|
# Not using 'import pip; pip.main([])' because it breaks the logger
|
||||||
args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
|
args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
|
||||||
|
|
||||||
if upgrade:
|
if upgrade:
|
||||||
args.append('--upgrade')
|
args.append('--upgrade')
|
||||||
if target:
|
if target:
|
||||||
args += ['--target', os.path.abspath(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)
|
||||||
try:
|
try:
|
||||||
return 0 == subprocess.call(args)
|
return 0 == subprocess.call(args)
|
||||||
except subprocess.SubprocessError:
|
except subprocess.SubprocessError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def check_package_exists(package, target=None):
|
||||||
|
"""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."""
|
||||||
|
req = pkg_resources.Requirement.parse(package)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
Loading…
Add table
Reference in a new issue