Handle requirements for scripts (#2765)
This commit is contained in:
1 parent
a03691455b
commit
aadf6a7750
5 files changed
+56
-12
No files matched your search
@@ -1,9 +1,15 @@
|
||||
"""Home Assistant command line scripts."""
|
||||
import argparse
|
||||
import importlib
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
from homeassistant.config import get_default_config_dir
|
||||
from homeassistant.util.package import install_package
|
||||
from homeassistant.bootstrap import mount_local_lib_path
|
||||
|
||||
|
||||
def run(args: str) -> int:
|
||||
def run(args: List) -> int:
|
||||
"""Run a script."""
|
||||
scripts = []
|
||||
path = os.path.dirname(__file__)
|
||||
@@ -26,4 +32,21 @@ def run(args: str) -> int:
|
||||
return 1
|
||||
|
||||
script = importlib.import_module('homeassistant.scripts.' + args[0])
|
||||
|
||||
config_dir = extract_config_dir()
|
||||
deps_dir = mount_local_lib_path(config_dir)
|
||||
for req in getattr(script, 'REQUIREMENTS', []):
|
||||
if not install_package(req, target=deps_dir):
|
||||
print('Aborting scipt, could not install dependency', req)
|
||||
return 1
|
||||
|
||||
return script.run(args[1:]) # type: ignore
|
||||
|
||||
|
||||
def extract_config_dir(args=None) -> str:
|
||||
"""Extract the config dir from the arguments or get the default."""
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-c', '--config', default=None)
|
||||
args = parser.parse_known_args(args)[0]
|
||||
return (os.path.join(os.getcwd(), args.config) if args.config
|
||||
else get_default_config_dir())
|
||||
Reference in new issue
Block a user