2019-04-17 12:06:00 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import codecs
|
|
|
|
import collections
|
2019-05-11 15:12:30 +02:00
|
|
|
import fnmatch
|
2019-04-17 12:06:00 +02:00
|
|
|
import os.path
|
2019-10-19 22:31:32 +02:00
|
|
|
import re
|
2019-05-11 15:12:30 +02:00
|
|
|
import subprocess
|
2019-04-17 12:06:00 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def find_all(a_str, sub):
|
|
|
|
for i, line in enumerate(a_str.splitlines()):
|
|
|
|
column = 0
|
|
|
|
while True:
|
|
|
|
column = line.find(sub, column)
|
|
|
|
if column == -1:
|
|
|
|
break
|
|
|
|
yield i, column
|
|
|
|
column += len(sub)
|
|
|
|
|
|
|
|
|
2019-05-11 15:12:30 +02:00
|
|
|
command = ['git', 'ls-files', '-s']
|
|
|
|
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
|
|
|
|
output, err = proc.communicate()
|
|
|
|
lines = [x.split() for x in output.decode('utf-8').splitlines()]
|
|
|
|
EXECUTABLE_BIT = {
|
|
|
|
s[3].strip(): int(s[0]) for s in lines
|
|
|
|
}
|
|
|
|
files = [s[3].strip() for s in lines]
|
2019-06-18 19:31:22 +02:00
|
|
|
files = list(filter(os.path.exists, files))
|
2019-04-17 12:06:00 +02:00
|
|
|
files.sort()
|
|
|
|
|
2019-06-09 17:03:51 +02:00
|
|
|
file_types = ('.h', '.c', '.cpp', '.tcc', '.yaml', '.yml', '.ini', '.txt', '.ico', '.svg',
|
2019-08-27 19:10:44 +02:00
|
|
|
'.py', '.html', '.js', '.md', '.sh', '.css', '.proto', '.conf', '.cfg',
|
2019-08-29 15:42:31 +02:00
|
|
|
'.woff', '.woff2', '')
|
2019-05-11 15:12:30 +02:00
|
|
|
cpp_include = ('*.h', '*.c', '*.cpp', '*.tcc')
|
2019-08-29 15:42:31 +02:00
|
|
|
ignore_types = ('.ico', '.woff', '.woff2', '')
|
2019-05-11 15:12:30 +02:00
|
|
|
|
|
|
|
LINT_FILE_CHECKS = []
|
|
|
|
LINT_CONTENT_CHECKS = []
|
2019-10-23 14:43:27 +02:00
|
|
|
LINT_POST_CHECKS = []
|
2019-05-11 15:12:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def run_check(lint_obj, fname, *args):
|
|
|
|
include = lint_obj['include']
|
|
|
|
exclude = lint_obj['exclude']
|
|
|
|
func = lint_obj['func']
|
|
|
|
if include is not None:
|
|
|
|
for incl in include:
|
|
|
|
if fnmatch.fnmatch(fname, incl):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
for excl in exclude:
|
|
|
|
if fnmatch.fnmatch(fname, excl):
|
|
|
|
return None
|
|
|
|
return func(*args)
|
|
|
|
|
|
|
|
|
|
|
|
def run_checks(lints, fname, *args):
|
|
|
|
for lint in lints:
|
|
|
|
add_errors(fname, run_check(lint, fname, *args))
|
|
|
|
|
|
|
|
|
|
|
|
def _add_check(checks, func, include=None, exclude=None):
|
|
|
|
checks.append({
|
|
|
|
'include': include,
|
|
|
|
'exclude': exclude or [],
|
|
|
|
'func': func,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def lint_file_check(**kwargs):
|
|
|
|
def decorator(func):
|
|
|
|
_add_check(LINT_FILE_CHECKS, func, **kwargs)
|
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def lint_content_check(**kwargs):
|
|
|
|
def decorator(func):
|
|
|
|
_add_check(LINT_CONTENT_CHECKS, func, **kwargs)
|
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-10-23 14:43:27 +02:00
|
|
|
def lint_post_check(func):
|
|
|
|
_add_check(LINT_POST_CHECKS, func)
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
|
|
def lint_re_check(regex, **kwargs):
|
|
|
|
prog = re.compile(regex, re.MULTILINE)
|
|
|
|
decor = lint_content_check(**kwargs)
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
def new_func(fname, content):
|
|
|
|
errors = []
|
|
|
|
for match in prog.finditer(content):
|
|
|
|
if 'NOLINT' in match.group(0):
|
|
|
|
continue
|
|
|
|
lineno = content.count("\n", 0, match.start()) + 1
|
|
|
|
err = func(fname, match)
|
|
|
|
if err is None:
|
|
|
|
continue
|
|
|
|
errors.append("{} See line {}.".format(err, lineno))
|
|
|
|
return errors
|
|
|
|
return decor(new_func)
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-05-11 15:12:30 +02:00
|
|
|
def lint_content_find_check(find, **kwargs):
|
|
|
|
decor = lint_content_check(**kwargs)
|
|
|
|
|
|
|
|
def decorator(func):
|
2019-05-27 13:06:45 +02:00
|
|
|
def new_func(fname, content):
|
|
|
|
find_ = find
|
|
|
|
if callable(find):
|
|
|
|
find_ = find(fname, content)
|
2019-10-23 14:43:27 +02:00
|
|
|
errors = []
|
2019-05-27 13:06:45 +02:00
|
|
|
for line, col in find_all(content, find_):
|
|
|
|
err = func(fname)
|
2019-10-23 14:43:27 +02:00
|
|
|
errors.append("{err} See line {line}:{col}."
|
|
|
|
"".format(err=err, line=line+1, col=col+1))
|
|
|
|
return errors
|
2019-05-11 15:12:30 +02:00
|
|
|
return decor(new_func)
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
@lint_file_check(include=['*.ino'])
|
|
|
|
def lint_ino(fname):
|
|
|
|
return "This file extension (.ino) is not allowed. Please use either .cpp or .h"
|
|
|
|
|
|
|
|
|
|
|
|
@lint_file_check(exclude=['*{}'.format(f) for f in file_types] + [
|
|
|
|
'.clang-*', '.dockerignore', '.editorconfig', '*.gitignore', 'LICENSE', 'pylintrc',
|
2019-06-09 17:03:51 +02:00
|
|
|
'MANIFEST.in', 'docker/Dockerfile*', 'docker/rootfs/*', 'script/*',
|
2019-05-11 15:12:30 +02:00
|
|
|
])
|
|
|
|
def lint_ext_check(fname):
|
|
|
|
return "This file extension is not a registered file type. If this is an error, please " \
|
|
|
|
"update the script/ci-custom.py script."
|
|
|
|
|
|
|
|
|
|
|
|
@lint_file_check(exclude=[
|
|
|
|
'docker/rootfs/*', 'script/*', 'setup.py'
|
|
|
|
])
|
|
|
|
def lint_executable_bit(fname):
|
|
|
|
ex = EXECUTABLE_BIT[fname]
|
|
|
|
if ex != 100644:
|
|
|
|
return 'File has invalid executable bit {}. If running from a windows machine please ' \
|
|
|
|
'see disabling executable bit in git.'.format(ex)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
@lint_content_find_check('\t', exclude=[
|
|
|
|
'esphome/dashboard/static/ace.js', 'esphome/dashboard/static/ext-searchbox.js',
|
|
|
|
])
|
2019-05-27 13:06:45 +02:00
|
|
|
def lint_tabs(fname):
|
2019-05-11 15:12:30 +02:00
|
|
|
return "File contains tab character. Please convert tabs to spaces."
|
|
|
|
|
|
|
|
|
|
|
|
@lint_content_find_check('\r')
|
2019-05-27 13:06:45 +02:00
|
|
|
def lint_newline(fname):
|
2019-05-11 15:12:30 +02:00
|
|
|
return "File contains windows newline. Please set your editor to unix newline mode."
|
|
|
|
|
|
|
|
|
2019-06-09 17:03:51 +02:00
|
|
|
@lint_content_check(exclude=['*.svg'])
|
2019-05-27 13:06:45 +02:00
|
|
|
def lint_end_newline(fname, content):
|
2019-05-11 15:12:30 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-10-23 14:43:27 +02:00
|
|
|
CPP_RE_EOL = r'\s*?(?://.*?)?$'
|
|
|
|
|
|
|
|
|
|
|
|
def highlight(s):
|
|
|
|
return '\033[36m{}\033[0m'.format(s)
|
|
|
|
|
|
|
|
|
|
|
|
@lint_re_check(r'^#define\s+([a-zA-Z0-9_]+)\s+([0-9bx]+)' + CPP_RE_EOL,
|
|
|
|
include=cpp_include, exclude=['esphome/core/log.h'])
|
|
|
|
def lint_no_defines(fname, match):
|
|
|
|
s = highlight('static const uint8_t {} = {};'.format(match.group(1), match.group(2)))
|
|
|
|
return ("#define macros for integer constants are not allowed, please use "
|
|
|
|
"{} style instead (replace uint8_t with the appropriate "
|
|
|
|
"datatype). See also Google style guide.".format(s))
|
|
|
|
|
|
|
|
|
|
|
|
@lint_re_check(r'^\s*delay\((\d+)\);' + CPP_RE_EOL, include=cpp_include)
|
|
|
|
def lint_no_long_delays(fname, match):
|
|
|
|
duration_ms = int(match.group(1))
|
|
|
|
if duration_ms < 50:
|
|
|
|
return None
|
|
|
|
return (
|
|
|
|
"{} - long calls to delay() are not allowed in ESPHome because everything executes "
|
|
|
|
"in one thread. Calling delay() will block the main thread and slow down ESPHome.\n"
|
|
|
|
"If there's no way to work around the delay() and it doesn't execute often, please add "
|
|
|
|
"a '// NOLINT' comment to the line."
|
|
|
|
"".format(highlight(match.group(0).strip()))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@lint_content_check(include=['esphome/const.py'])
|
|
|
|
def lint_const_ordered(fname, content):
|
|
|
|
lines = content.splitlines()
|
2019-10-19 22:31:32 +02:00
|
|
|
errors = []
|
2019-10-23 14:43:27 +02:00
|
|
|
for start in ['CONF_', 'ICON_', 'UNIT_']:
|
|
|
|
matching = [(i+1, line) for i, line in enumerate(lines) if line.startswith(start)]
|
|
|
|
ordered = list(sorted(matching, key=lambda x: x[1].replace('_', ' ')))
|
|
|
|
ordered = [(mi, ol) for (mi, _), (_, ol) in zip(matching, ordered)]
|
|
|
|
for (mi, ml), (oi, ol) in zip(matching, ordered):
|
|
|
|
if ml == ol:
|
|
|
|
continue
|
|
|
|
target = next(i for i, l in ordered if l == ml)
|
|
|
|
target_text = next(l for i, l in matching if target == i)
|
|
|
|
errors.append("Constant {} is not ordered, please make sure all constants are ordered. "
|
|
|
|
"See line {} (should go to line {}, {})"
|
|
|
|
"".format(highlight(ml), mi, target, target_text))
|
|
|
|
return errors
|
|
|
|
|
|
|
|
|
|
|
|
@lint_re_check(r'^\s*CONF_([A-Z_0-9a-z]+)\s+=\s+[\'"](.*?)[\'"]\s*?$', include=['*.py'])
|
|
|
|
def lint_conf_matches(fname, match):
|
|
|
|
const = match.group(1)
|
|
|
|
value = match.group(2)
|
|
|
|
const_norm = const.lower()
|
|
|
|
value_norm = value.replace('.', '_')
|
|
|
|
if const_norm == value_norm:
|
|
|
|
return None
|
|
|
|
return ("Constant {} does not match value {}! Please make sure the constant's name matches its "
|
|
|
|
"value!"
|
|
|
|
"".format(highlight('CONF_' + const), highlight(value)))
|
|
|
|
|
|
|
|
|
|
|
|
CONF_RE = r'^(CONF_[a-zA-Z0-9_]+)\s*=\s*[\'"].*?[\'"]\s*?$'
|
|
|
|
with codecs.open('esphome/const.py', 'r', encoding='utf-8') as f_handle:
|
|
|
|
constants_content = f_handle.read()
|
|
|
|
CONSTANTS = [m.group(1) for m in re.finditer(CONF_RE, constants_content, re.MULTILINE)]
|
|
|
|
|
|
|
|
CONSTANTS_USES = collections.defaultdict(list)
|
|
|
|
|
|
|
|
|
|
|
|
@lint_re_check(CONF_RE, include=['*.py'], exclude=['esphome/const.py'])
|
|
|
|
def lint_conf_from_const_py(fname, match):
|
|
|
|
name = match.group(1)
|
|
|
|
if name not in CONSTANTS:
|
|
|
|
CONSTANTS_USES[name].append(fname)
|
|
|
|
return None
|
|
|
|
return ("Constant {} has already been defined in const.py - please import the constant from "
|
|
|
|
"const.py directly.".format(highlight(name)))
|
|
|
|
|
|
|
|
|
|
|
|
@lint_post_check
|
|
|
|
def lint_constants_usage():
|
|
|
|
errors = []
|
|
|
|
for constant, uses in CONSTANTS_USES.items():
|
|
|
|
if len(uses) < 4:
|
|
|
|
continue
|
|
|
|
errors.append("Constant {} is defined in {} files. Please move all definitions of the "
|
|
|
|
"constant to const.py (Uses: {})"
|
|
|
|
"".format(highlight(constant), len(uses), ', '.join(uses)))
|
2019-10-19 22:31:32 +02:00
|
|
|
return errors
|
|
|
|
|
|
|
|
|
2019-05-27 13:06:45 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2019-06-09 17:03:51 +02:00
|
|
|
@lint_content_find_check(relative_py_search_text, include=['esphome/components/*.py'],
|
|
|
|
exclude=['esphome/components/web_server/__init__.py'])
|
2019-05-27 13:06:45 +02:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2019-05-11 15:12:30 +02:00
|
|
|
@lint_content_find_check('"esphome.h"', include=cpp_include, exclude=['tests/custom.h'])
|
2019-05-27 13:06:45 +02:00
|
|
|
def lint_esphome_h(fname):
|
2019-05-11 15:12:30 +02:00
|
|
|
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'])
|
2019-05-27 13:06:45 +02:00
|
|
|
def lint_pragma_once(fname, content):
|
2019-05-11 15:12:30 +02:00
|
|
|
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.")
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2019-05-12 23:04:36 +02:00
|
|
|
@lint_content_find_check('ESP_LOG', include=['*.h', '*.tcc'], exclude=[
|
|
|
|
'esphome/components/binary_sensor/binary_sensor.h',
|
|
|
|
'esphome/components/cover/cover.h',
|
|
|
|
'esphome/components/display/display_buffer.h',
|
|
|
|
'esphome/components/i2c/i2c.h',
|
|
|
|
'esphome/components/mqtt/mqtt_component.h',
|
|
|
|
'esphome/components/output/binary_output.h',
|
|
|
|
'esphome/components/output/float_output.h',
|
|
|
|
'esphome/components/sensor/sensor.h',
|
|
|
|
'esphome/components/stepper/stepper.h',
|
|
|
|
'esphome/components/switch/switch.h',
|
|
|
|
'esphome/components/text_sensor/text_sensor.h',
|
|
|
|
'esphome/core/component.h',
|
|
|
|
'esphome/core/esphal.h',
|
|
|
|
'esphome/core/log.h',
|
|
|
|
'tests/custom.h',
|
|
|
|
])
|
2019-05-27 13:06:45 +02:00
|
|
|
def lint_log_in_header(fname):
|
2019-05-12 23:04:36 +02:00
|
|
|
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)')
|
|
|
|
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
errors = collections.defaultdict(list)
|
2019-05-11 15:12:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def add_errors(fname, errs):
|
|
|
|
if not isinstance(errs, list):
|
|
|
|
errs = [errs]
|
|
|
|
errs = [x for x in errs if x is not None]
|
|
|
|
for err in errs:
|
|
|
|
if not isinstance(err, str):
|
|
|
|
raise ValueError("Error is not instance of string!")
|
|
|
|
if not errs:
|
|
|
|
return
|
|
|
|
errors[fname].extend(errs)
|
|
|
|
|
|
|
|
|
|
|
|
for fname in files:
|
|
|
|
_, ext = os.path.splitext(fname)
|
|
|
|
run_checks(LINT_FILE_CHECKS, fname, fname)
|
2019-08-27 19:10:44 +02:00
|
|
|
if ext in ignore_types:
|
2019-05-11 15:12:30 +02:00
|
|
|
continue
|
2019-04-17 12:06:00 +02:00
|
|
|
try:
|
2019-05-11 15:12:30 +02:00
|
|
|
with codecs.open(fname, 'r', encoding='utf-8') as f_handle:
|
2019-04-17 12:06:00 +02:00
|
|
|
content = f_handle.read()
|
|
|
|
except UnicodeDecodeError:
|
2019-05-11 15:12:30 +02:00
|
|
|
add_errors(fname, "File is not readable as UTF-8. Please set your editor to UTF-8 mode.")
|
2019-04-17 12:06:00 +02:00
|
|
|
continue
|
2019-05-27 13:06:45 +02:00
|
|
|
run_checks(LINT_CONTENT_CHECKS, fname, fname, content)
|
2019-05-11 15:12:30 +02:00
|
|
|
|
2019-10-23 14:43:27 +02:00
|
|
|
run_checks(LINT_POST_CHECKS, 'POST')
|
|
|
|
|
2019-05-11 15:12:30 +02:00
|
|
|
for f, errs in sorted(errors.items()):
|
2019-04-17 12:06:00 +02:00
|
|
|
print("\033[0;32m************* File \033[1;32m{}\033[0m".format(f))
|
|
|
|
for err in errs:
|
|
|
|
print(err)
|
|
|
|
print()
|
|
|
|
|
|
|
|
sys.exit(len(errors))
|