Auto install core dependencies on boot

This commit is contained in:
Paulus Schoutsen 2015-07-06 23:59:21 -07:00
parent 4be9519e76
commit 90739c9df9

View File

@ -4,15 +4,9 @@ from __future__ import print_function
import sys import sys
import os import os
import argparse import argparse
import importlib import subprocess
DEPENDENCIES = ['requests>=2.0', 'pyyaml>=3.11', 'pytz>=2015.2']
# Home Assistant dependencies, mapped module -> package name
DEPENDENCIES = {
'requests': 'requests',
'yaml': 'pyyaml',
'pytz': 'pytz',
}
def validate_python(): def validate_python():
@ -24,21 +18,29 @@ def validate_python():
sys.exit() sys.exit()
# Copy of homeassistant.util.package because we can't import yet
def install_package(package):
"""Install a package on PyPi. Accepts pip compatible package strings.
Return boolean if install successfull."""
args = ['python3', '-m', 'pip', 'install', '--disable-pip-version-check',
'--quiet', package]
if sys.base_prefix == sys.prefix:
args.append('--user')
return not subprocess.call(args)
def validate_dependencies(): def validate_dependencies():
""" Validate all dependencies that HA uses. """ """ Validate all dependencies that HA uses. """
import_fail = False import_fail = False
for module, name in DEPENDENCIES.items(): for requirement in DEPENDENCIES:
try: if not install_package(requirement):
importlib.import_module(module)
except ImportError:
import_fail = True import_fail = True
print( print('Fatal Error: Unable to install dependency', requirement)
'Fatal Error: Unable to find dependency {}'.format(name))
if import_fail: if import_fail:
print(("Install dependencies by running: " print(("Install dependencies by running: "
"pip3 install -r requirements.txt")) "python3 -m pip install -r requirements.txt"))
sys.exit() sys.exit()