ha-core/homeassistant/requirements.py

118 lines
3.5 KiB
Python
Raw Normal View History

2018-01-30 12:30:47 +01:00
"""Module to handle installing requirements."""
import asyncio
import logging
import os
from pathlib import Path
2019-12-05 19:40:05 +01:00
from typing import Any, Dict, List, Optional, Set
2018-01-30 12:30:47 +01:00
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.loader import Integration, async_get_integration
2018-01-30 12:30:47 +01:00
import homeassistant.util.package as pkg_util
2019-07-31 21:25:30 +02:00
DATA_PIP_LOCK = "pip_lock"
DATA_PKG_CACHE = "pkg_cache"
CONSTRAINT_FILE = "package_constraints.txt"
PROGRESS_FILE = ".pip_progress"
2018-01-30 12:30:47 +01:00
_LOGGER = logging.getLogger(__name__)
class RequirementsNotFound(HomeAssistantError):
"""Raised when a component is not found."""
def __init__(self, domain: str, requirements: List) -> None:
"""Initialize a component not found error."""
super().__init__(f"Requirements for {domain} not found: {requirements}.")
self.domain = domain
self.requirements = requirements
async def async_get_integration_with_requirements(
2019-12-05 19:40:05 +01:00
hass: HomeAssistant, domain: str, done: Set[str] = None
) -> Integration:
"""Get an integration with installed requirements.
This can raise IntegrationNotFound if manifest or integration
is invalid, RequirementNotFound if there was some type of
failure to install requirements.
"""
2019-12-05 19:40:05 +01:00
if done is None:
done = {domain}
else:
done.add(domain)
integration = await async_get_integration(hass, domain)
2019-10-31 19:39:26 +01:00
if hass.config.skip_pip:
return integration
2019-10-31 19:39:26 +01:00
if integration.requirements:
await async_process_requirements(
hass, integration.domain, integration.requirements
)
2019-12-05 19:40:05 +01:00
deps_to_check = [
dep
for dep in integration.dependencies + (integration.after_dependencies or [])
if dep not in done
]
2019-11-02 01:21:50 +01:00
2019-12-05 19:40:05 +01:00
if deps_to_check:
2019-11-02 01:21:50 +01:00
await asyncio.gather(
2019-12-05 19:40:05 +01:00
*[
async_get_integration_with_requirements(hass, dep, done)
for dep in deps_to_check
]
2019-11-02 01:21:50 +01:00
)
return integration
2019-07-31 21:25:30 +02:00
async def async_process_requirements(
hass: HomeAssistant, name: str, requirements: List[str]
) -> None:
2018-01-30 12:30:47 +01:00
"""Install the requirements for a component or platform.
This method is a coroutine. It will raise RequirementsNotFound
if an requirement can't be satisfied.
2018-01-30 12:30:47 +01:00
"""
pip_lock = hass.data.get(DATA_PIP_LOCK)
if pip_lock is None:
pip_lock = hass.data[DATA_PIP_LOCK] = asyncio.Lock()
2018-01-30 12:30:47 +01:00
kwargs = pip_kwargs(hass.config.config_dir)
2018-01-30 12:30:47 +01:00
async with pip_lock:
2018-01-30 12:30:47 +01:00
for req in requirements:
if pkg_util.is_installed(req):
continue
2019-07-31 21:25:30 +02:00
ret = await hass.async_add_executor_job(_install, hass, req, kwargs)
2018-01-30 12:30:47 +01:00
if not ret:
raise RequirementsNotFound(name, [req])
2018-01-30 12:30:47 +01:00
def _install(hass: HomeAssistant, req: str, kwargs: Dict) -> bool:
"""Install requirement."""
progress_path = Path(hass.config.path(PROGRESS_FILE))
progress_path.touch()
try:
return pkg_util.install_package(req, **kwargs)
finally:
progress_path.unlink()
def pip_kwargs(config_dir: Optional[str]) -> Dict[str, Any]:
2018-01-30 12:30:47 +01:00
"""Return keyword arguments for PIP install."""
is_docker = pkg_util.is_docker_env()
2018-01-30 12:30:47 +01:00
kwargs = {
2019-07-31 21:25:30 +02:00
"constraints": os.path.join(os.path.dirname(__file__), CONSTRAINT_FILE),
"no_cache_dir": is_docker,
2018-01-30 12:30:47 +01:00
}
2019-07-31 21:25:30 +02:00
if "WHEELS_LINKS" in os.environ:
kwargs["find_links"] = os.environ["WHEELS_LINKS"]
if not (config_dir is None or pkg_util.is_virtual_env()) and not is_docker:
kwargs["target"] = os.path.join(config_dir, "deps")
2018-01-30 12:30:47 +01:00
return kwargs