From dc4c1bc2256747755ed0fad4d943a5f647025955 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 8 Apr 2019 22:19:21 +0200 Subject: [PATCH] Use more lazy imports Speeds up esphome invocation a lot --- esphome/__main__.py | 10 +++++++++- esphome/helpers.py | 11 +++++++---- esphome/writer.py | 9 ++++++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/esphome/__main__.py b/esphome/__main__.py index ff97961811..cdd90aa0b1 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -7,7 +7,7 @@ import os import random import sys -from esphome import const, core_config, platformio_api, wizard, writer, yaml_util +from esphome import const, core_config, writer, yaml_util from esphome.config import get_component, iter_components, read_config, strip_default_ids from esphome.const import CONF_BAUD_RATE, CONF_BROKER, CONF_ESPHOME, CONF_LOGGER, \ CONF_USE_CUSTOM_CODE @@ -92,6 +92,8 @@ def get_port_type(port): def run_miniterm(config, port): import serial + from esphome import platformio_api + if CONF_LOGGER not in config: _LOGGER.info("Logger is not enabled. Not starting UART logs.") return @@ -155,6 +157,8 @@ def write_cpp(config): def compile_program(args, config): + from esphome import platformio_api + _LOGGER.info("Compiling app...") rc = platformio_api.run_compile(config, args.verbose) if rc != 0 and CORE.is_dev_esphome_core_version and not is_dev_esphome_version(): @@ -185,6 +189,8 @@ def upload_using_esptool(config, port): def upload_program(config, args, host): # if upload is to a serial port use platformio, otherwise assume ota if get_port_type(host) == 'SERIAL': + from esphome import platformio_api + if CORE.is_esp8266: return upload_using_esptool(config, host) return platformio_api.run_upload(config, args.verbose, host) @@ -268,6 +274,8 @@ def setup_log(debug=False): def command_wizard(args): + from esphome import wizard + return wizard.wizard(args.configuration) diff --git a/esphome/helpers.py b/esphome/helpers.py index 01b31111b9..8c07677998 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -1,13 +1,9 @@ from __future__ import print_function -import errno import logging import os -import socket -import subprocess from esphome.py_compat import char_to_byte, text_type -from esphome.zeroconf import Zeroconf _LOGGER = logging.getLogger(__name__) @@ -73,6 +69,8 @@ def color(the_color, message=''): def run_system_command(*args): + import subprocess + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() rc = p.returncode @@ -80,6 +78,8 @@ def run_system_command(*args): def mkdir_p(path): + import errno + try: os.makedirs(path) except OSError as exc: @@ -103,6 +103,8 @@ def is_ip_address(host): def _resolve_with_zeroconf(host): from esphome.core import EsphomeError + from esphome.zeroconf import Zeroconf + try: zc = Zeroconf() except Exception: @@ -122,6 +124,7 @@ def _resolve_with_zeroconf(host): def resolve_ip_address(host): from esphome.core import EsphomeError + import socket try: ip = socket.gethostbyname(host) diff --git a/esphome/writer.py b/esphome/writer.py index 9c2bc8fbd8..12bc2ed9be 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -1,11 +1,9 @@ from __future__ import print_function import codecs -import json import logging import os import re -import shutil from esphome.config import iter_components from esphome.const import ARDUINO_VERSION_ESP32_1_0_0, ARDUINO_VERSION_ESP8266_2_5_0, \ @@ -15,7 +13,6 @@ from esphome.const import ARDUINO_VERSION_ESP32_1_0_0, ARDUINO_VERSION_ESP8266_2 from esphome.core import CORE, EsphomeError from esphome.core_config import GITHUB_ARCHIVE_ZIP, LIBRARY_URI_REPO, VERSION_REGEX from esphome.helpers import mkdir_p, run_system_command -from esphome.symlink_ops import symlink, islink, readlink, unlink from esphome.pins import ESP8266_FLASH_SIZES, ESP8266_LD_SCRIPTS from esphome.py_compat import IS_PY3, string_types from esphome.storage_json import StorageJSON, storage_path @@ -217,6 +214,8 @@ def update_storage_json(): def symlink_esphome_core_version(esphome_core_version): + from esphome.symlink_ops import symlink, islink, readlink, unlink + lib_path = CORE.relative_build_path('lib') dst_path = CORE.relative_build_path('lib', 'esphome-core') if CORE.is_local_esphome_core_copy: @@ -250,6 +249,8 @@ def format_ini(data): def gather_lib_deps(): + import json + lib_deps = set() if CONF_REPOSITORY in CORE.esphome_core_version: repo = CORE.esphome_core_version[CONF_REPOSITORY] @@ -509,6 +510,8 @@ def write_cpp(code_s): def clean_build(): + import shutil + pioenvs = CORE.relative_pioenvs_path() if os.path.isdir(pioenvs): _LOGGER.info("Deleting %s", pioenvs)