esphome-docs/travis.py

52 lines
1.5 KiB
Python
Raw Normal View History

2019-02-11 14:32:13 +01:00
from pathlib import Path
2019-02-27 10:23:58 +01:00
import re
2019-02-11 14:32:13 +01:00
import sys
errors = []
def find_all(a_str, sub):
for i, line in enumerate(a_str.splitlines(keepends=False)):
column = 0
while True:
column = line.find(sub, column)
if column == -1:
break
yield i, column
column += len(sub)
2019-02-27 10:23:58 +01:00
section_regex = re.compile(r'^(=+|-+|\*+|~+)$')
2019-02-11 15:01:22 +01:00
for f in sorted(Path('.').glob('**/*.rst')):
2019-02-11 14:32:13 +01:00
try:
content = f.read_text('utf-8')
except UnicodeDecodeError:
errors.append("File {} is not readable as UTF-8. Please set your editor to UTF-8 mode."
"".format(f))
for line, col in find_all(content, '\t'):
errors.append("File {} contains tab character on line {}:{}. "
"Please convert tabs to spaces.".format(f, line, col))
for line, col in find_all(content, '\r'):
errors.append("File {} contains windows newline on line {}:{}. "
"Please set your editor to unix newline mode.".format(f, line, col))
2019-02-27 10:23:58 +01:00
lines = content.splitlines(keepends=False)
for i, line in enumerate(lines):
if i == 0:
continue
if not section_regex.match(line):
continue
line_above = lines[i - 1]
if len(line_above) != len(line):
errors.append("The title length must match the bar length below it. See {}:{}"
"".format(f, i))
2019-02-11 14:32:13 +01:00
for error in errors:
print(error)
sys.exit(len(errors))