2015-07-07 07:00:21 +00:00
|
|
|
"""Helpers to install PyPi packages."""
|
|
|
|
import subprocess
|
2015-07-16 01:37:24 +00:00
|
|
|
import sys
|
2015-07-07 07:00:21 +00:00
|
|
|
|
|
|
|
from . import environment as env
|
|
|
|
|
|
|
|
# If we are not in a virtual environment, install in user space
|
|
|
|
INSTALL_USER = not env.is_virtual()
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2015-07-16 01:37:24 +00:00
|
|
|
args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
|
2015-07-07 07:00:21 +00:00
|
|
|
if upgrade:
|
|
|
|
args.append('--upgrade')
|
|
|
|
if user:
|
|
|
|
args.append('--user')
|
2015-07-16 01:37:24 +00:00
|
|
|
try:
|
|
|
|
return 0 == subprocess.call(args)
|
|
|
|
except subprocess.SubprocessError:
|
|
|
|
return False
|