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
|
2019-05-26 18:58:42 +00:00
|
|
|
from urllib.parse import urlparse
|
2019-05-29 22:30:09 +00:00
|
|
|
from pathlib import Path
|
2019-05-26 18:58:42 +00:00
|
|
|
|
|
|
|
import pkg_resources
|
|
|
|
from importlib_metadata import version, PackageNotFoundError
|
|
|
|
|
2016-07-28 03:33:49 +00:00
|
|
|
|
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'))
|
|
|
|
|
|
|
|
|
2019-05-29 22:30:09 +00:00
|
|
|
def is_docker_env() -> bool:
|
|
|
|
"""Return True if we run in a docker env."""
|
|
|
|
return Path("/.dockerenv").exists()
|
|
|
|
|
|
|
|
|
2019-05-26 18:58:42 +00:00
|
|
|
def is_installed(package: str) -> bool:
|
|
|
|
"""Check if a package is installed and will be loaded when we import it.
|
|
|
|
|
|
|
|
Returns True when the requirement is met.
|
|
|
|
Returns False when the package is not installed or doesn't meet req.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
req = pkg_resources.Requirement.parse(package)
|
|
|
|
except ValueError:
|
|
|
|
# This is a zip file. We no longer use this in Home Assistant,
|
|
|
|
# leaving it in for custom components.
|
|
|
|
req = pkg_resources.Requirement.parse(urlparse(package).fragment)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return version(req.project_name) in req
|
|
|
|
except PackageNotFoundError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def install_package(package: str, upgrade: bool = True,
|
|
|
|
target: Optional[str] = None,
|
2019-05-29 22:30:09 +00:00
|
|
|
constraints: Optional[str] = None,
|
2019-06-01 08:04:12 +00:00
|
|
|
find_links: Optional[str] = None,
|
|
|
|
no_cache_dir: Optional[bool] = False) -> 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]
|
2019-06-01 08:04:12 +00:00
|
|
|
if no_cache_dir:
|
|
|
|
args.append('--no-cache-dir')
|
2018-08-28 10:52:18 +00:00
|
|
|
if upgrade:
|
|
|
|
args.append('--upgrade')
|
|
|
|
if constraints is not None:
|
|
|
|
args += ['--constraint', constraints]
|
2019-05-29 22:30:09 +00:00
|
|
|
if find_links is not None:
|
2019-06-21 15:47:56 +00:00
|
|
|
args += ['--find-links', find_links, '--prefer-binary']
|
2018-08-28 10:52:18 +00:00
|
|
|
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
|