From 72a80f559a0c1ef905fd4db9e164ebedbd3b1a2d Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 13:06:45 +0200 Subject: [PATCH] Use relative include, add check --- esphome/components/adc/adc_sensor.cpp | 2 +- esphome/components/ads1115/sensor.py | 3 +- .../binary_sensor/binary_sensor.cpp | 2 +- esphome/components/binary_sensor/filter.cpp | 4 +- esphome/components/dht/dht.cpp | 2 +- .../components/esp32_touch/binary_sensor.py | 3 +- .../gpio/binary_sensor/gpio_binary_sensor.cpp | 2 +- .../components/gpio/switch/gpio_switch.cpp | 2 +- esphome/components/logger/logger.cpp | 2 +- esphome/components/my9231/output.py | 2 +- esphome/components/ota/ota_component.cpp | 2 +- esphome/components/sensor/filter.cpp | 4 +- esphome/components/sensor/sensor.cpp | 2 +- esphome/components/switch/switch.cpp | 2 +- esphome/components/uptime/uptime_sensor.cpp | 2 +- esphome/components/wifi/wifi_component.cpp | 2 +- .../components/wifi/wifi_component_esp32.cpp | 2 +- .../wifi/wifi_component_esp8266.cpp | 2 +- script/ci-custom.py | 55 +++++++++++++++---- 19 files changed, 65 insertions(+), 32 deletions(-) diff --git a/esphome/components/adc/adc_sensor.cpp b/esphome/components/adc/adc_sensor.cpp index d0cea53d49..2c448d0392 100644 --- a/esphome/components/adc/adc_sensor.cpp +++ b/esphome/components/adc/adc_sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/adc/adc_sensor.h" +#include "adc_sensor.h" #include "esphome/core/log.h" #ifdef USE_ADC_SENSOR_VCC diff --git a/esphome/components/ads1115/sensor.py b/esphome/components/ads1115/sensor.py index 2fe9b6fa86..204ccb99d7 100644 --- a/esphome/components/ads1115/sensor.py +++ b/esphome/components/ads1115/sensor.py @@ -1,10 +1,9 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor, voltage_sampler -from esphome.components.ads1115 import ADS1115Component from esphome.const import CONF_GAIN, CONF_MULTIPLEXER, ICON_FLASH, UNIT_VOLT, CONF_ID from esphome.py_compat import string_types -from . import ads1115_ns +from . import ads1115_ns, ADS1115Component DEPENDENCIES = ['ads1115'] diff --git a/esphome/components/binary_sensor/binary_sensor.cpp b/esphome/components/binary_sensor/binary_sensor.cpp index 8c982f938d..27c835d38c 100644 --- a/esphome/components/binary_sensor/binary_sensor.cpp +++ b/esphome/components/binary_sensor/binary_sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/binary_sensor/binary_sensor.h" +#include "binary_sensor.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/binary_sensor/filter.cpp b/esphome/components/binary_sensor/filter.cpp index 39b88f867c..b7ac2c4a79 100644 --- a/esphome/components/binary_sensor/filter.cpp +++ b/esphome/components/binary_sensor/filter.cpp @@ -1,5 +1,5 @@ -#include "esphome/components/binary_sensor/filter.h" -#include "esphome/components/binary_sensor/binary_sensor.h" +#include "filter.h" +#include "binary_sensor.h" namespace esphome { diff --git a/esphome/components/dht/dht.cpp b/esphome/components/dht/dht.cpp index 79732bb269..24a017e478 100644 --- a/esphome/components/dht/dht.cpp +++ b/esphome/components/dht/dht.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/dht/dht.h" +#include "dht.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" diff --git a/esphome/components/esp32_touch/binary_sensor.py b/esphome/components/esp32_touch/binary_sensor.py index 94748e53e8..a72ca5796f 100644 --- a/esphome/components/esp32_touch/binary_sensor.py +++ b/esphome/components/esp32_touch/binary_sensor.py @@ -1,10 +1,9 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import binary_sensor -from esphome.components.esp32_touch import ESP32TouchComponent from esphome.const import CONF_NAME, CONF_PIN, CONF_THRESHOLD, ESP_PLATFORM_ESP32, CONF_ID from esphome.pins import validate_gpio_pin -from . import esp32_touch_ns +from . import esp32_touch_ns, ESP32TouchComponent ESP_PLATFORMS = [ESP_PLATFORM_ESP32] DEPENDENCIES = ['esp32_touch'] diff --git a/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp b/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp index dff3609ce2..f95778af4c 100644 --- a/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp +++ b/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" +#include "gpio_binary_sensor.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/gpio/switch/gpio_switch.cpp b/esphome/components/gpio/switch/gpio_switch.cpp index 22139d6b9c..d22a74847e 100644 --- a/esphome/components/gpio/switch/gpio_switch.cpp +++ b/esphome/components/gpio/switch/gpio_switch.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/gpio/switch/gpio_switch.h" +#include "gpio_switch.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index cd71049f15..78f09989e4 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/logger/logger.h" +#include "logger.h" #ifdef ARDUINO_ARCH_ESP32 #include diff --git a/esphome/components/my9231/output.py b/esphome/components/my9231/output.py index 9acc8bdcd6..c69649fd5e 100644 --- a/esphome/components/my9231/output.py +++ b/esphome/components/my9231/output.py @@ -1,8 +1,8 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import output -from esphome.components.my9231 import MY9231OutputComponent from esphome.const import CONF_CHANNEL, CONF_ID +from . import MY9231OutputComponent DEPENDENCIES = ['my9231'] diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp index ee2d67c85e..d37a7a0c6a 100644 --- a/esphome/components/ota/ota_component.cpp +++ b/esphome/components/ota/ota_component.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/ota/ota_component.h" +#include "ota_component.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index 4923a1c09b..306607dfda 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -1,5 +1,5 @@ -#include "esphome/components/sensor/filter.h" -#include "esphome/components/sensor/sensor.h" +#include "filter.h" +#include "sensor.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index ca6f4c23bb..e12e55e320 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/sensor/sensor.h" +#include "sensor.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/switch/switch.cpp b/esphome/components/switch/switch.cpp index d2279e6ad6..a6a25dd9dc 100644 --- a/esphome/components/switch/switch.cpp +++ b/esphome/components/switch/switch.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/switch/switch.h" +#include "switch.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/uptime/uptime_sensor.cpp b/esphome/components/uptime/uptime_sensor.cpp index a66ca0636f..f047724768 100644 --- a/esphome/components/uptime/uptime_sensor.cpp +++ b/esphome/components/uptime/uptime_sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/uptime/uptime_sensor.h" +#include "uptime_sensor.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 35735e5f08..882d45f793 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/wifi/wifi_component.h" +#include "wifi_component.h" #ifdef ARDUINO_ARCH_ESP32 #include diff --git a/esphome/components/wifi/wifi_component_esp32.cpp b/esphome/components/wifi/wifi_component_esp32.cpp index f50acee614..6b118cf0ae 100644 --- a/esphome/components/wifi/wifi_component_esp32.cpp +++ b/esphome/components/wifi/wifi_component_esp32.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/wifi/wifi_component.h" +#include "wifi_component.h" #ifdef ARDUINO_ARCH_ESP32 diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index a2c9ac2551..11981b801b 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/wifi/wifi_component.h" +#include "wifi_component.h" #ifdef ARDUINO_ARCH_ESP8266 diff --git a/script/ci-custom.py b/script/ci-custom.py index 6aff5717cf..27b45dbe27 100755 --- a/script/ci-custom.py +++ b/script/ci-custom.py @@ -86,9 +86,12 @@ def lint_content_find_check(find, **kwargs): decor = lint_content_check(**kwargs) def decorator(func): - def new_func(content): - for line, col in find_all(content, find): - err = func() + def new_func(fname, content): + find_ = find + if callable(find): + find_ = find(fname, content) + for line, col in find_all(content, find_): + err = func(fname) return "{err} See line {line}:{col}.".format(err=err, line=line+1, col=col+1) return decor(new_func) return decorator @@ -123,31 +126,63 @@ def lint_executable_bit(fname): 'esphome/dashboard/static/ace.js', 'esphome/dashboard/static/ext-searchbox.js', 'script/.neopixelbus.patch', ]) -def lint_tabs(): +def lint_tabs(fname): return "File contains tab character. Please convert tabs to spaces." @lint_content_find_check('\r') -def lint_newline(): +def lint_newline(fname): return "File contains windows newline. Please set your editor to unix newline mode." @lint_content_check() -def lint_end_newline(content): +def lint_end_newline(fname, content): if content and not content.endswith('\n'): return "File does not end with a newline, please add an empty line at the end of the file." return None +def relative_cpp_search_text(fname, content): + parts = fname.split('/') + integration = parts[2] + return '#include "esphome/components/{}'.format(integration) + + +@lint_content_find_check(relative_cpp_search_text, include=['esphome/components/*.cpp']) +def lint_relative_cpp_import(fname): + return ("Component contains absolute import - Components must always use " + "relative imports.\n" + "Change:\n" + ' #include "esphome/components/abc/abc.h"\n' + 'to:\n' + ' #include "abc.h"\n\n') + + +def relative_py_search_text(fname, content): + parts = fname.split('/') + integration = parts[2] + return 'esphome.components.{}'.format(integration) + + +@lint_content_find_check(relative_py_search_text, include=['esphome/components/*.py']) +def lint_relative_py_import(fname): + return ("Component contains absolute import - Components must always use " + "relative imports within the integration.\n" + "Change:\n" + ' from esphome.components.abc import abc_ns"\n' + 'to:\n' + ' from . import abc_ns\n\n') + + @lint_content_find_check('"esphome.h"', include=cpp_include, exclude=['tests/custom.h']) -def lint_esphome_h(): +def lint_esphome_h(fname): return ("File contains reference to 'esphome.h' - This file is " "auto-generated and should only be used for *custom* " "components. Please replace with references to the direct files.") @lint_content_check(include=['*.h']) -def lint_pragma_once(content): +def lint_pragma_once(fname, content): if '#pragma once' not in content: return ("Header file contains no 'pragma once' header guard. Please add a " "'#pragma once' line at the top of the file.") @@ -171,7 +206,7 @@ def lint_pragma_once(content): 'esphome/core/log.h', 'tests/custom.h', ]) -def lint_log_in_header(): +def lint_log_in_header(fname): return ('Found reference to ESP_LOG in header file. Using ESP_LOG* in header files ' 'is currently not possible - please move the definition to a source file (.cpp)') @@ -202,7 +237,7 @@ for fname in files: except UnicodeDecodeError: add_errors(fname, "File is not readable as UTF-8. Please set your editor to UTF-8 mode.") continue - run_checks(LINT_CONTENT_CHECKS, fname, content) + run_checks(LINT_CONTENT_CHECKS, fname, fname, content) for f, errs in sorted(errors.items()): print("\033[0;32m************* File \033[1;32m{}\033[0m".format(f))