From d799c03f0c2815f517bc45eaac04d96e30dd0817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=BCtz?= Date: Sat, 19 Jan 2019 17:54:41 +0100 Subject: [PATCH] call platformio and esptool using subprocess if $ESPHOME_USE_SUBPROCESS is set (#359) * call platformio and esptool using subprocess if $ESPHOME_USE_SUBPROCESS is set * Update setup.py --- esphomeyaml/__main__.py | 13 ++++++++----- esphomeyaml/platformio_api.py | 13 ++++++++----- esphomeyaml/util.py | 26 ++++++++++++++++++++++++++ setup.py | 12 ++++++++++-- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/esphomeyaml/__main__.py b/esphomeyaml/__main__.py index 26f42c14a..06dbd2761 100644 --- a/esphomeyaml/__main__.py +++ b/esphomeyaml/__main__.py @@ -19,7 +19,7 @@ from esphomeyaml.helpers import color, indent from esphomeyaml.py_compat import safe_input, text_type, IS_PY2 from esphomeyaml.storage_json import StorageJSON, esphomeyaml_storage_path, \ start_update_check_thread, storage_path -from esphomeyaml.util import run_external_command, safe_print +from esphomeyaml.util import run_external_command, run_external_process, safe_print _LOGGER = logging.getLogger(__name__) @@ -167,13 +167,16 @@ def compile_program(args, config): def upload_using_esptool(config, port): - import esptool - path = os.path.join(CORE.build_path, '.pioenvs', CORE.name, 'firmware.bin') cmd = ['esptool.py', '--before', 'default_reset', '--after', 'hard_reset', '--chip', 'esp8266', '--port', port, 'write_flash', '0x0', path] - # pylint: disable=protected-access - return run_external_command(esptool._main, *cmd) + + if os.environ.get('ESPHOME_USE_SUBPROCESS') is None: + import esptool + # pylint: disable=protected-access + return run_external_command(esptool._main, *cmd) + + return run_external_process(*cmd) def upload_program(config, args, host): diff --git a/esphomeyaml/platformio_api.py b/esphomeyaml/platformio_api.py index 3892b4a76..aae84e89b 100644 --- a/esphomeyaml/platformio_api.py +++ b/esphomeyaml/platformio_api.py @@ -7,18 +7,21 @@ import re import subprocess from esphomeyaml.core import CORE -from esphomeyaml.util import run_external_command +from esphomeyaml.util import run_external_command, run_external_process _LOGGER = logging.getLogger(__name__) def run_platformio_cli(*args, **kwargs): - import platformio.__main__ - os.environ["PLATFORMIO_FORCE_COLOR"] = "true" cmd = ['platformio'] + list(args) - return run_external_command(platformio.__main__.main, - *cmd, **kwargs) + + if os.environ.get('ESPHOME_USE_SUBPROCESS') is None: + import platformio.__main__ + return run_external_command(platformio.__main__.main, + *cmd, **kwargs) + + return run_external_process(*cmd, **kwargs) def run_platformio_cli_run(config, verbose, *args, **kwargs): diff --git a/esphomeyaml/util.py b/esphomeyaml/util.py index 64835ce49..5ff8a9f0d 100644 --- a/esphomeyaml/util.py +++ b/esphomeyaml/util.py @@ -3,6 +3,7 @@ from __future__ import print_function import io import logging import re +import subprocess import sys _LOGGER = logging.getLogger(__name__) @@ -121,3 +122,28 @@ def run_external_command(func, *cmd, **kwargs): if capture_stdout: # pylint: disable=lost-exception return cap_stdout.getvalue() + + +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() diff --git a/setup.py b/setup.py index 87fd6b778..c85a3f66d 100755 --- a/setup.py +++ b/setup.py @@ -1,6 +1,7 @@ #!/usr/bin/env python """esphomeyaml setup script.""" from setuptools import setup, find_packages +import os from esphomeyaml import const @@ -23,18 +24,25 @@ DOWNLOAD_URL = '{}/archive/{}.zip'.format(GITHUB_URL, const.__version__) REQUIRES = [ 'voluptuous>=0.11.1', - 'platformio>=3.5.3', 'pyyaml>=3.12', 'paho-mqtt>=1.3.1', 'colorlog>=3.1.2', 'tornado>=5.0.0', - 'esptool>=2.3.1', 'typing>=3.0.0;python_version<"3.5"', 'protobuf>=3.4', 'tzlocal>=1.4', 'pyserial>=3.4,<4', ] +# If you have problems importing platformio and esptool as modules you can set +# $ESPHOME_USE_SUBPROCESS to make ESPHome call their executables instead. +# This means they have to be in your $PATH. +if os.environ.get('ESPHOME_USE_SUBPROCESS') is None: + REQUIRES.extend([ + 'platformio>=3.5.3', + 'esptool>=2.3.1', + ]) + CLASSIFIERS = [ 'Environment :: Console', 'Intended Audience :: Developers',