2016-03-07 22:20:48 +00:00
|
|
|
"""Helpers to install PyPi packages."""
|
2017-07-14 02:26:21 +00:00
|
|
|
import asyncio
|
2015-09-05 08:50:35 +00:00
|
|
|
import logging
|
2015-09-17 06:12:38 +00:00
|
|
|
import os
|
2017-07-15 14:25:02 +00:00
|
|
|
from subprocess import PIPE, Popen
|
2015-07-16 01:37:24 +00:00
|
|
|
import sys
|
2016-07-28 03:33:49 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2015-09-05 08:50:35 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-06-08 13:53:12 +00:00
|
|
|
|
2015-09-05 08:50:35 +00:00
|
|
|
|
2018-07-23 08:24:39 +00:00
|
|
|
def is_virtual_env() -> bool:
|
2018-03-05 23:51:37 +00:00
|
|
|
"""Return if we run in a virtual environtment."""
|
|
|
|
# Check supports venv && virtualenv
|
|
|
|
return (getattr(sys, 'base_prefix', sys.prefix) != sys.prefix or
|
|
|
|
hasattr(sys, 'real_prefix'))
|
|
|
|
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def install_package(package: str, upgrade: bool = True,
|
|
|
|
target: Optional[str] = None,
|
|
|
|
constraints: Optional[str] = None) -> bool:
|
2016-03-07 22:20:48 +00:00
|
|
|
"""Install a package on PyPi. Accepts pip compatible package strings.
|
|
|
|
|
2016-01-26 23:08:06 +00:00
|
|
|
Return boolean if install successful.
|
|
|
|
"""
|
2015-07-07 07:00:21 +00:00
|
|
|
# Not using 'import pip; pip.main([])' because it breaks the logger
|
2018-08-28 10:52:18 +00:00
|
|
|
_LOGGER.info('Attempting install of %s', package)
|
|
|
|
env = os.environ.copy()
|
|
|
|
args = [sys.executable, '-m', 'pip', 'install', '--quiet', package]
|
|
|
|
if upgrade:
|
|
|
|
args.append('--upgrade')
|
|
|
|
if constraints is not None:
|
|
|
|
args += ['--constraint', constraints]
|
|
|
|
if target:
|
|
|
|
assert not is_virtual_env()
|
|
|
|
# This only works if not running in venv
|
|
|
|
args += ['--user']
|
|
|
|
env['PYTHONUSERBASE'] = os.path.abspath(target)
|
|
|
|
if sys.platform != 'win32':
|
|
|
|
# Workaround for incompatible prefix setting
|
|
|
|
# See http://stackoverflow.com/a/4495175
|
|
|
|
args += ['--prefix=']
|
|
|
|
process = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)
|
|
|
|
_, stderr = process.communicate()
|
|
|
|
if process.returncode != 0:
|
|
|
|
_LOGGER.error("Unable to install package %s: %s",
|
|
|
|
package, stderr.decode('utf-8').lstrip().strip())
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2017-07-14 02:26:21 +00:00
|
|
|
|
|
|
|
|
2018-06-16 14:48:41 +00:00
|
|
|
async def async_get_user_site(deps_dir: str) -> str:
|
2017-07-14 02:26:21 +00:00
|
|
|
"""Return user local library path.
|
|
|
|
|
|
|
|
This function is a coroutine.
|
|
|
|
"""
|
2018-06-16 14:48:41 +00:00
|
|
|
env = os.environ.copy()
|
|
|
|
env['PYTHONUSERBASE'] = os.path.abspath(deps_dir)
|
|
|
|
args = [sys.executable, '-m', 'site', '--user-site']
|
2018-02-25 11:38:46 +00:00
|
|
|
process = await asyncio.create_subprocess_exec(
|
2018-06-16 14:48:41 +00:00
|
|
|
*args, stdin=asyncio.subprocess.PIPE,
|
2017-07-14 02:26:21 +00:00
|
|
|
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL,
|
|
|
|
env=env)
|
2018-02-25 11:38:46 +00:00
|
|
|
stdout, _ = await process.communicate()
|
2017-07-14 02:26:21 +00:00
|
|
|
lib_dir = stdout.decode().strip()
|
|
|
|
return lib_dir
|