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
This commit is contained in:
Robert Schütz 2019-01-19 17:54:41 +01:00 committed by Otto Winter
parent c86675f644
commit d799c03f0c
4 changed files with 52 additions and 12 deletions

View file

@ -19,7 +19,7 @@ from esphomeyaml.helpers import color, indent
from esphomeyaml.py_compat import safe_input, text_type, IS_PY2 from esphomeyaml.py_compat import safe_input, text_type, IS_PY2
from esphomeyaml.storage_json import StorageJSON, esphomeyaml_storage_path, \ from esphomeyaml.storage_json import StorageJSON, esphomeyaml_storage_path, \
start_update_check_thread, 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__) _LOGGER = logging.getLogger(__name__)
@ -167,14 +167,17 @@ def compile_program(args, config):
def upload_using_esptool(config, port): def upload_using_esptool(config, port):
import esptool
path = os.path.join(CORE.build_path, '.pioenvs', CORE.name, 'firmware.bin') path = os.path.join(CORE.build_path, '.pioenvs', CORE.name, 'firmware.bin')
cmd = ['esptool.py', '--before', 'default_reset', '--after', 'hard_reset', cmd = ['esptool.py', '--before', 'default_reset', '--after', 'hard_reset',
'--chip', 'esp8266', '--port', port, 'write_flash', '0x0', path] '--chip', 'esp8266', '--port', port, 'write_flash', '0x0', path]
if os.environ.get('ESPHOME_USE_SUBPROCESS') is None:
import esptool
# pylint: disable=protected-access # pylint: disable=protected-access
return run_external_command(esptool._main, *cmd) return run_external_command(esptool._main, *cmd)
return run_external_process(*cmd)
def upload_program(config, args, host): def upload_program(config, args, host):
# if upload is to a serial port use platformio, otherwise assume ota # if upload is to a serial port use platformio, otherwise assume ota

View file

@ -7,19 +7,22 @@ import re
import subprocess import subprocess
from esphomeyaml.core import CORE 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__) _LOGGER = logging.getLogger(__name__)
def run_platformio_cli(*args, **kwargs): def run_platformio_cli(*args, **kwargs):
import platformio.__main__
os.environ["PLATFORMIO_FORCE_COLOR"] = "true" os.environ["PLATFORMIO_FORCE_COLOR"] = "true"
cmd = ['platformio'] + list(args) cmd = ['platformio'] + list(args)
if os.environ.get('ESPHOME_USE_SUBPROCESS') is None:
import platformio.__main__
return run_external_command(platformio.__main__.main, return run_external_command(platformio.__main__.main,
*cmd, **kwargs) *cmd, **kwargs)
return run_external_process(*cmd, **kwargs)
def run_platformio_cli_run(config, verbose, *args, **kwargs): def run_platformio_cli_run(config, verbose, *args, **kwargs):
command = ['run', '-d', CORE.build_path] command = ['run', '-d', CORE.build_path]

View file

@ -3,6 +3,7 @@ from __future__ import print_function
import io import io
import logging import logging
import re import re
import subprocess
import sys import sys
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -121,3 +122,28 @@ def run_external_command(func, *cmd, **kwargs):
if capture_stdout: if capture_stdout:
# pylint: disable=lost-exception # pylint: disable=lost-exception
return cap_stdout.getvalue() 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()

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
"""esphomeyaml setup script.""" """esphomeyaml setup script."""
from setuptools import setup, find_packages from setuptools import setup, find_packages
import os
from esphomeyaml import const from esphomeyaml import const
@ -23,18 +24,25 @@ DOWNLOAD_URL = '{}/archive/{}.zip'.format(GITHUB_URL, const.__version__)
REQUIRES = [ REQUIRES = [
'voluptuous>=0.11.1', 'voluptuous>=0.11.1',
'platformio>=3.5.3',
'pyyaml>=3.12', 'pyyaml>=3.12',
'paho-mqtt>=1.3.1', 'paho-mqtt>=1.3.1',
'colorlog>=3.1.2', 'colorlog>=3.1.2',
'tornado>=5.0.0', 'tornado>=5.0.0',
'esptool>=2.3.1',
'typing>=3.0.0;python_version<"3.5"', 'typing>=3.0.0;python_version<"3.5"',
'protobuf>=3.4', 'protobuf>=3.4',
'tzlocal>=1.4', 'tzlocal>=1.4',
'pyserial>=3.4,<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 = [ CLASSIFIERS = [
'Environment :: Console', 'Environment :: Console',
'Intended Audience :: Developers', 'Intended Audience :: Developers',