mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	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:
		
				
					committed by
					
						 Otto Winter
						Otto Winter
					
				
			
			
				
	
			
			
			
						parent
						
							c86675f644
						
					
				
				
					commit
					d799c03f0c
				
			| @@ -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): | ||||
|   | ||||
| @@ -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): | ||||
|   | ||||
| @@ -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() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user