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
|
2019-12-09 15:42:10 +00:00
|
|
|
from pathlib import Path
|
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
|
|
|
|
|
|
|
|
import pkg_resources
|
2016-07-28 03:33:49 +00:00
|
|
|
|
2020-06-17 13:21:14 +00:00
|
|
|
if sys.version_info[:2] >= (3, 8):
|
|
|
|
from importlib.metadata import ( # pylint: disable=no-name-in-module,import-error
|
|
|
|
PackageNotFoundError,
|
|
|
|
version,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
from importlib_metadata import ( # pylint: disable=import-error
|
|
|
|
PackageNotFoundError,
|
|
|
|
version,
|
|
|
|
)
|
|
|
|
|
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:
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Return if we run in a virtual environment."""
|
2018-03-05 23:51:37 +00:00
|
|
|
# Check supports venv && virtualenv
|
2019-07-31 19:25:30 +00:00
|
|
|
return getattr(sys, "base_prefix", sys.prefix) != sys.prefix or hasattr(
|
|
|
|
sys, "real_prefix"
|
|
|
|
)
|
2018-03-05 23:51:37 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def install_package(
|
|
|
|
package: str,
|
|
|
|
upgrade: bool = True,
|
|
|
|
target: Optional[str] = None,
|
|
|
|
constraints: Optional[str] = None,
|
|
|
|
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
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info("Attempting install of %s", package)
|
2018-08-28 10:52:18 +00:00
|
|
|
env = os.environ.copy()
|
2019-07-31 19:25:30 +00:00
|
|
|
args = [sys.executable, "-m", "pip", "install", "--quiet", package]
|
2019-06-01 08:04:12 +00:00
|
|
|
if no_cache_dir:
|
2019-07-31 19:25:30 +00:00
|
|
|
args.append("--no-cache-dir")
|
2018-08-28 10:52:18 +00:00
|
|
|
if upgrade:
|
2019-07-31 19:25:30 +00:00
|
|
|
args.append("--upgrade")
|
2018-08-28 10:52:18 +00:00
|
|
|
if constraints is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
args += ["--constraint", constraints]
|
2019-05-29 22:30:09 +00:00
|
|
|
if find_links is not None:
|
2019-07-31 19:25:30 +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
|
2019-07-31 19:25:30 +00:00
|
|
|
args += ["--user"]
|
|
|
|
env["PYTHONUSERBASE"] = os.path.abspath(target)
|
|
|
|
if sys.platform != "win32":
|
2018-08-28 10:52:18 +00:00
|
|
|
# Workaround for incompatible prefix setting
|
|
|
|
# See http://stackoverflow.com/a/4495175
|
2019-07-31 19:25:30 +00:00
|
|
|
args += ["--prefix="]
|
2018-08-28 10:52:18 +00:00
|
|
|
process = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env)
|
|
|
|
_, stderr = process.communicate()
|
|
|
|
if process.returncode != 0:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Unable to install package %s: %s",
|
|
|
|
package,
|
|
|
|
stderr.decode("utf-8").lstrip().strip(),
|
|
|
|
)
|
2018-08-28 10:52:18 +00:00
|
|
|
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()
|
2019-07-31 19:25:30 +00:00
|
|
|
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(
|
2019-07-31 19:25:30 +00:00
|
|
|
*args,
|
|
|
|
stdin=asyncio.subprocess.PIPE,
|
|
|
|
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
|