2018-10-20 12:41:00 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2018-11-03 14:06:14 +01:00
|
|
|
import io
|
|
|
|
import logging
|
|
|
|
import re
|
2019-01-19 17:54:41 +01:00
|
|
|
import subprocess
|
2018-11-03 14:06:14 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
|
2018-10-16 23:16:06 +02:00
|
|
|
class Registry(dict):
|
|
|
|
def register(self, name):
|
|
|
|
def decorator(fun):
|
|
|
|
self[name] = fun
|
|
|
|
return fun
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceRegistry(dict):
|
|
|
|
def register(self, name, validator):
|
|
|
|
def decorator(fun):
|
|
|
|
self[name] = (validator, fun)
|
|
|
|
return fun
|
|
|
|
|
|
|
|
return decorator
|
2018-10-20 12:41:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
def safe_print(message=""):
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome.core import CORE
|
2018-12-18 19:31:43 +01:00
|
|
|
|
|
|
|
if CORE.dashboard:
|
|
|
|
try:
|
|
|
|
message = message.replace('\033', '\\033')
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
pass
|
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
try:
|
|
|
|
print(message)
|
|
|
|
return
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2019-01-19 16:48:16 +01:00
|
|
|
print(message.encode('utf-8', 'backslashreplace'))
|
2018-10-20 12:41:00 +02:00
|
|
|
except UnicodeEncodeError:
|
2019-01-19 16:48:16 +01:00
|
|
|
try:
|
|
|
|
print(message.encode('ascii', 'backslashreplace'))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
print("Cannot print line because of invalid locale!")
|
2018-11-03 14:06:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def shlex_quote(s):
|
|
|
|
if not s:
|
|
|
|
return u"''"
|
|
|
|
if re.search(r'[^\w@%+=:,./-]', s) is None:
|
|
|
|
return s
|
|
|
|
|
|
|
|
return u"'" + s.replace(u"'", u"'\"'\"'") + u"'"
|
|
|
|
|
|
|
|
|
2018-12-18 19:31:43 +01:00
|
|
|
class RedirectText(object):
|
|
|
|
def __init__(self, out):
|
|
|
|
self._out = out
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
return getattr(self._out, item)
|
|
|
|
|
|
|
|
def write(self, s):
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome.core import CORE
|
2018-12-18 19:31:43 +01:00
|
|
|
|
|
|
|
if CORE.dashboard:
|
|
|
|
try:
|
|
|
|
s = s.replace('\033', '\\033')
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self._out.write(s)
|
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
def isatty(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-11-03 14:06:14 +01:00
|
|
|
def run_external_command(func, *cmd, **kwargs):
|
|
|
|
def mock_exit(return_code):
|
|
|
|
raise SystemExit(return_code)
|
|
|
|
|
|
|
|
orig_argv = sys.argv
|
|
|
|
orig_exit = sys.exit # mock sys.exit
|
|
|
|
full_cmd = u' '.join(shlex_quote(x) for x in cmd)
|
|
|
|
_LOGGER.info(u"Running: %s", full_cmd)
|
|
|
|
|
2019-01-02 12:15:13 +01:00
|
|
|
orig_stdout = sys.stdout
|
2018-12-18 19:31:43 +01:00
|
|
|
sys.stdout = RedirectText(sys.stdout)
|
2019-01-02 12:15:13 +01:00
|
|
|
orig_stderr = sys.stderr
|
2018-12-18 19:31:43 +01:00
|
|
|
sys.stderr = RedirectText(sys.stderr)
|
|
|
|
|
2018-11-03 14:06:14 +01:00
|
|
|
capture_stdout = kwargs.get('capture_stdout', False)
|
|
|
|
if capture_stdout:
|
2019-01-02 12:15:13 +01:00
|
|
|
cap_stdout = sys.stdout = io.BytesIO()
|
2018-11-03 14:06:14 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
sys.argv = list(cmd)
|
|
|
|
sys.exit = mock_exit
|
|
|
|
return func() or 0
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return 1
|
|
|
|
except SystemExit as err:
|
|
|
|
return err.args[0]
|
|
|
|
except Exception as err: # pylint: disable=broad-except
|
|
|
|
_LOGGER.error(u"Running command failed: %s", err)
|
|
|
|
_LOGGER.error(u"Please try running %s locally.", full_cmd)
|
|
|
|
finally:
|
|
|
|
sys.argv = orig_argv
|
|
|
|
sys.exit = orig_exit
|
|
|
|
|
2019-01-02 12:15:13 +01:00
|
|
|
sys.stdout = orig_stdout
|
|
|
|
sys.stderr = orig_stderr
|
2018-12-18 19:31:43 +01:00
|
|
|
|
2018-11-03 14:06:14 +01:00
|
|
|
if capture_stdout:
|
|
|
|
# pylint: disable=lost-exception
|
2019-01-02 12:15:13 +01:00
|
|
|
return cap_stdout.getvalue()
|
2019-01-19 17:54:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def run_external_process(*cmd, **kwargs):
|
|
|
|
full_cmd = u' '.join(shlex_quote(x) for x in cmd)
|
|
|
|
_LOGGER.info(u"Running: %s", full_cmd)
|
|
|
|
|
|
|
|
capture_stdout = kwargs.get('capture_stdout', False)
|
|
|
|
if capture_stdout:
|
|
|
|
sub_stdout = io.BytesIO()
|
|
|
|
else:
|
|
|
|
sub_stdout = RedirectText(sys.stdout)
|
|
|
|
|
|
|
|
sub_stderr = RedirectText(sys.stderr)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return subprocess.call(cmd,
|
|
|
|
stdout=sub_stdout,
|
|
|
|
stderr=sub_stderr)
|
|
|
|
except Exception as err: # pylint: disable=broad-except
|
|
|
|
_LOGGER.error(u"Running command failed: %s", err)
|
|
|
|
_LOGGER.error(u"Please try running %s locally.", full_cmd)
|
|
|
|
finally:
|
|
|
|
if capture_stdout:
|
|
|
|
# pylint: disable=lost-exception
|
|
|
|
return sub_stdout.getvalue()
|