2016-07-03 18:38:14 +00:00
|
|
|
"""Home Assistant command line scripts."""
|
2016-08-10 06:54:34 +00:00
|
|
|
import argparse
|
2018-06-16 14:48:41 +00:00
|
|
|
import asyncio
|
2016-07-03 18:38:14 +00:00
|
|
|
import importlib
|
2017-07-15 14:25:02 +00:00
|
|
|
import logging
|
2016-07-03 18:38:14 +00:00
|
|
|
import os
|
2016-08-10 20:32:07 +00:00
|
|
|
import sys
|
2019-09-20 15:23:34 +00:00
|
|
|
from typing import List, Optional, Sequence, Text
|
2016-07-03 18:38:14 +00:00
|
|
|
|
2020-07-06 22:58:53 +00:00
|
|
|
from homeassistant import runner
|
2018-06-16 14:48:41 +00:00
|
|
|
from homeassistant.bootstrap import async_mount_local_lib_path
|
2016-08-10 06:54:34 +00:00
|
|
|
from homeassistant.config import get_default_config_dir
|
2019-05-26 18:58:42 +00:00
|
|
|
from homeassistant.requirements import pip_kwargs
|
2019-12-09 15:42:10 +00:00
|
|
|
from homeassistant.util.package import install_package, is_installed, is_virtual_env
|
2016-08-10 06:54:34 +00:00
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
# mypy: allow-untyped-defs, no-warn-return-any
|
2019-07-24 20:18:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-08-10 06:54:34 +00:00
|
|
|
def run(args: List) -> int:
|
2016-07-03 18:38:14 +00:00
|
|
|
"""Run a script."""
|
2016-08-01 03:58:39 +00:00
|
|
|
scripts = []
|
|
|
|
path = os.path.dirname(__file__)
|
|
|
|
for fil in os.listdir(path):
|
2019-07-31 19:25:30 +00:00
|
|
|
if fil == "__pycache__":
|
2016-08-01 03:58:39 +00:00
|
|
|
continue
|
2019-09-24 20:53:03 +00:00
|
|
|
|
|
|
|
if os.path.isdir(os.path.join(path, fil)):
|
2016-08-01 03:58:39 +00:00
|
|
|
scripts.append(fil)
|
2019-07-31 19:25:30 +00:00
|
|
|
elif fil != "__init__.py" and fil.endswith(".py"):
|
2016-08-01 03:58:39 +00:00
|
|
|
scripts.append(fil[:-3])
|
2016-07-03 18:38:14 +00:00
|
|
|
|
|
|
|
if not args:
|
2019-07-31 19:25:30 +00:00
|
|
|
print("Please specify a script to run.")
|
|
|
|
print("Available scripts:", ", ".join(scripts))
|
2016-07-03 18:38:14 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
if args[0] not in scripts:
|
2019-07-31 19:25:30 +00:00
|
|
|
print("Invalid script specified.")
|
|
|
|
print("Available scripts:", ", ".join(scripts))
|
2016-07-03 18:38:14 +00:00
|
|
|
return 1
|
|
|
|
|
2020-01-03 13:47:06 +00:00
|
|
|
script = importlib.import_module(f"homeassistant.scripts.{args[0]}")
|
2016-08-10 06:54:34 +00:00
|
|
|
|
|
|
|
config_dir = extract_config_dir()
|
2018-06-16 14:48:41 +00:00
|
|
|
|
2019-02-26 18:20:54 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
2018-06-16 14:48:41 +00:00
|
|
|
if not is_virtual_env():
|
2019-02-26 18:20:54 +00:00
|
|
|
loop.run_until_complete(async_mount_local_lib_path(config_dir))
|
2018-06-16 14:48:41 +00:00
|
|
|
|
2019-02-26 18:20:54 +00:00
|
|
|
_pip_kwargs = pip_kwargs(config_dir)
|
2016-08-10 20:32:07 +00:00
|
|
|
|
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
|
2018-01-30 11:30:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for req in getattr(script, "REQUIREMENTS", []):
|
2019-05-26 18:58:42 +00:00
|
|
|
if is_installed(req):
|
2019-02-26 18:20:54 +00:00
|
|
|
continue
|
|
|
|
|
2019-03-08 00:48:14 +00:00
|
|
|
if not install_package(req, **_pip_kwargs):
|
2019-07-31 19:25:30 +00:00
|
|
|
print("Aborting script, could not install dependency", req)
|
2016-08-10 06:54:34 +00:00
|
|
|
return 1
|
|
|
|
|
2020-07-06 22:58:53 +00:00
|
|
|
asyncio.set_event_loop_policy(runner.HassEventLoopPolicy(False))
|
|
|
|
|
2019-07-24 20:18:40 +00:00
|
|
|
return script.run(args[1:]) # type: ignore
|
2016-08-10 06:54:34 +00:00
|
|
|
|
|
|
|
|
2019-09-20 15:23:34 +00:00
|
|
|
def extract_config_dir(args: Optional[Sequence[Text]] = None) -> str:
|
2016-08-10 06:54:34 +00:00
|
|
|
"""Extract the config dir from the arguments or get the default."""
|
2016-08-18 01:57:52 +00:00
|
|
|
parser = argparse.ArgumentParser(add_help=False)
|
2019-07-31 19:25:30 +00:00
|
|
|
parser.add_argument("-c", "--config", default=None)
|
2019-09-20 15:23:34 +00:00
|
|
|
parsed_args = parser.parse_known_args(args)[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
2019-09-20 15:23:34 +00:00
|
|
|
os.path.join(os.getcwd(), parsed_args.config)
|
|
|
|
if parsed_args.config
|
2019-07-31 19:25:30 +00:00
|
|
|
else get_default_config_dir()
|
|
|
|
)
|