Increase robustness dependency installation

This commit is contained in:
Paulus Schoutsen 2015-07-15 18:37:24 -07:00
parent 34b6627f9a
commit c532a28a98
3 changed files with 16 additions and 6 deletions

View File

@ -7,6 +7,8 @@ import argparse
import subprocess
DEPENDENCIES = ['requests>=2.0', 'pyyaml>=3.11', 'pytz>=2015.2']
IS_VIRTUAL = (getattr(sys, 'base_prefix', sys.prefix) != sys.prefix or
hasattr(sys, 'real_prefix'))
def validate_python():
@ -22,10 +24,13 @@ def validate_python():
def install_package(package):
"""Install a package on PyPi. Accepts pip compatible package strings.
Return boolean if install successfull."""
args = ['python3', '-m', 'pip', 'install', '--quiet', package]
if sys.base_prefix == sys.prefix:
args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
if not IS_VIRTUAL:
args.append('--user')
return not subprocess.call(args)
try:
return 0 == subprocess.call(args)
except subprocess.SubprocessError:
return False
def validate_dependencies():

View File

@ -5,4 +5,5 @@ import sys
def is_virtual():
""" Return if we run in a virtual environtment. """
# Check supports venv && virtualenv
return sys.base_prefix != sys.prefix or hasattr(sys, 'real_prefix')
return (getattr(sys, 'base_prefix', sys.prefix) != sys.prefix or
hasattr(sys, 'real_prefix'))

View File

@ -1,5 +1,6 @@
"""Helpers to install PyPi packages."""
import subprocess
import sys
from . import environment as env
@ -11,9 +12,12 @@ def install_package(package, upgrade=False, user=INSTALL_USER):
"""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 = ['python3', '-m', 'pip', 'install', '--quiet', package]
args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
if upgrade:
args.append('--upgrade')
if user:
args.append('--user')
return not subprocess.call(args)
try:
return 0 == subprocess.call(args)
except subprocess.SubprocessError:
return False