mirror of
https://github.com/esphome/esphome.git
synced 2024-11-14 02:58:11 +01:00
Fix clang-format script behaviour without -i + code cleanup (#2002)
Co-authored-by: Stefan Agner <stefan@agner.ch>
This commit is contained in:
parent
66cdb761dc
commit
3749c11f21
4 changed files with 57 additions and 77 deletions
9
.github/workflows/ci.yml
vendored
9
.github/workflows/ci.yml
vendored
|
@ -46,16 +46,21 @@ jobs:
|
||||||
echo "::add-matcher::.github/workflows/matchers/clang-tidy.json"
|
echo "::add-matcher::.github/workflows/matchers/clang-tidy.json"
|
||||||
echo "::add-matcher::.github/workflows/matchers/gcc.json"
|
echo "::add-matcher::.github/workflows/matchers/gcc.json"
|
||||||
|
|
||||||
|
# Also run git-diff-index so that the step is marked as failed on formatting errors,
|
||||||
|
# since clang-format doesn't do anything but change files if -i is passed.
|
||||||
- name: Run clang-format
|
- name: Run clang-format
|
||||||
run: script/clang-format -i
|
run: |
|
||||||
|
script/clang-format -i
|
||||||
|
git diff-index --quiet HEAD --
|
||||||
if: ${{ matrix.id == 'clang-format' }}
|
if: ${{ matrix.id == 'clang-format' }}
|
||||||
|
|
||||||
- name: Run clang-tidy
|
- name: Run clang-tidy
|
||||||
run: script/clang-tidy --all-headers --fix --split-num 4 --split-at ${{ matrix.split }}
|
run: script/clang-tidy --all-headers --fix --split-num 4 --split-at ${{ matrix.split }}
|
||||||
if: ${{ matrix.id == 'clang-tidy' }}
|
if: ${{ matrix.id == 'clang-tidy' }}
|
||||||
|
|
||||||
- name: Suggest changes
|
- name: Suggested changes
|
||||||
run: script/ci-suggest-changes
|
run: script/ci-suggest-changes
|
||||||
|
if: always()
|
||||||
|
|
||||||
ci:
|
ci:
|
||||||
# Don't use the esphome-lint docker image because it may contain outdated requirements.
|
# Don't use the esphome-lint docker image because it may contain outdated requirements.
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import os
|
import os
|
||||||
|
import queue
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
@ -13,59 +12,47 @@ import threading
|
||||||
import click
|
import click
|
||||||
|
|
||||||
sys.path.append(os.path.dirname(__file__))
|
sys.path.append(os.path.dirname(__file__))
|
||||||
from helpers import basepath, get_output, git_ls_files, filter_changed
|
from helpers import get_output, git_ls_files, filter_changed
|
||||||
|
|
||||||
is_py2 = sys.version[0] == '2'
|
|
||||||
|
|
||||||
if is_py2:
|
|
||||||
import Queue as queue
|
|
||||||
else:
|
|
||||||
import queue as queue
|
|
||||||
|
|
||||||
root_path = os.path.abspath(os.path.normpath(os.path.join(__file__, '..', '..')))
|
|
||||||
basepath = os.path.join(root_path, 'esphome')
|
|
||||||
rel_basepath = os.path.relpath(basepath, os.getcwd())
|
|
||||||
|
|
||||||
|
|
||||||
def run_format(args, queue, lock):
|
def run_format(args, queue, lock, failed_files):
|
||||||
"""Takes filenames out of queue and runs clang-tidy on them."""
|
"""Takes filenames out of queue and runs clang-format on them."""
|
||||||
while True:
|
while True:
|
||||||
path = queue.get()
|
path = queue.get()
|
||||||
invocation = ['clang-format-11']
|
invocation = ['clang-format-11']
|
||||||
if args.inplace:
|
if args.inplace:
|
||||||
invocation.append('-i')
|
invocation.append('-i')
|
||||||
|
else:
|
||||||
|
invocation.extend(['--dry-run', '-Werror'])
|
||||||
invocation.append(path)
|
invocation.append(path)
|
||||||
|
|
||||||
proc = subprocess.Popen(invocation, stdout=subprocess.PIPE,
|
proc = subprocess.run(invocation, capture_output=True, encoding='utf-8')
|
||||||
stderr=subprocess.PIPE)
|
|
||||||
output, err = proc.communicate()
|
|
||||||
with lock:
|
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
print(' '.join(invocation))
|
with lock:
|
||||||
print(output.decode('utf-8'))
|
print()
|
||||||
print(err.decode('utf-8'))
|
print("\033[0;32m************* File \033[1;32m{}\033[0m".format(path))
|
||||||
|
print(proc.stdout)
|
||||||
|
print(proc.stderr)
|
||||||
|
print()
|
||||||
|
failed_files.append(path)
|
||||||
queue.task_done()
|
queue.task_done()
|
||||||
|
|
||||||
|
|
||||||
def progress_bar_show(value):
|
def progress_bar_show(value):
|
||||||
if value is None:
|
return value if value is not None else ''
|
||||||
return ''
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('-j', '--jobs', type=int,
|
parser.add_argument('-j', '--jobs', type=int,
|
||||||
default=multiprocessing.cpu_count(),
|
default=multiprocessing.cpu_count(),
|
||||||
help='number of tidy instances to be run in parallel.')
|
help='number of format instances to be run in parallel.')
|
||||||
parser.add_argument('files', nargs='*', default=[],
|
parser.add_argument('files', nargs='*', default=[],
|
||||||
help='files to be processed (regex on path)')
|
help='files to be processed (regex on path)')
|
||||||
parser.add_argument('-i', '--inplace', action='store_true',
|
parser.add_argument('-i', '--inplace', action='store_true',
|
||||||
help='apply fix-its')
|
help='reformat files in-place')
|
||||||
parser.add_argument('-q', '--quiet', action='store_false',
|
|
||||||
help='Run clang-tidy in quiet mode')
|
|
||||||
parser.add_argument('-c', '--changed', action='store_true',
|
parser.add_argument('-c', '--changed', action='store_true',
|
||||||
help='Only run on changed files')
|
help='only run on changed files')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -75,7 +62,7 @@ def main():
|
||||||
Oops. It looks like clang-format is not installed.
|
Oops. It looks like clang-format is not installed.
|
||||||
|
|
||||||
Please check you can run "clang-format-11 -version" in your terminal and install
|
Please check you can run "clang-format-11 -version" in your terminal and install
|
||||||
clang-format (v7) if necessary.
|
clang-format (v11) if necessary.
|
||||||
|
|
||||||
Note you can also upload your code as a pull request on GitHub and see the CI check
|
Note you can also upload your code as a pull request on GitHub and see the CI check
|
||||||
output to apply clang-format.
|
output to apply clang-format.
|
||||||
|
@ -83,13 +70,11 @@ def main():
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
for path in git_ls_files():
|
for path in git_ls_files(['*.cpp', '*.h', '*.tcc']):
|
||||||
filetypes = ('.cpp', '.h', '.tcc')
|
files.append(os.path.relpath(path, os.getcwd()))
|
||||||
ext = os.path.splitext(path)[1]
|
|
||||||
if ext in filetypes:
|
if args.files:
|
||||||
path = os.path.relpath(path, os.getcwd())
|
# Match against files specified on command-line
|
||||||
files.append(path)
|
|
||||||
# Match against re
|
|
||||||
file_name_re = re.compile('|'.join(args.files))
|
file_name_re = re.compile('|'.join(args.files))
|
||||||
files = [p for p in files if file_name_re.search(p)]
|
files = [p for p in files if file_name_re.search(p)]
|
||||||
|
|
||||||
|
@ -98,13 +83,13 @@ def main():
|
||||||
|
|
||||||
files.sort()
|
files.sort()
|
||||||
|
|
||||||
return_code = 0
|
failed_files = []
|
||||||
try:
|
try:
|
||||||
task_queue = queue.Queue(args.jobs)
|
task_queue = queue.Queue(args.jobs)
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
for _ in range(args.jobs):
|
for _ in range(args.jobs):
|
||||||
t = threading.Thread(target=run_format,
|
t = threading.Thread(target=run_format,
|
||||||
args=(args, task_queue, lock))
|
args=(args, task_queue, lock, failed_files))
|
||||||
t.daemon = True
|
t.daemon = True
|
||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
|
@ -122,7 +107,7 @@ def main():
|
||||||
print('Ctrl-C detected, goodbye.')
|
print('Ctrl-C detected, goodbye.')
|
||||||
os.kill(0, 9)
|
os.kill(0, 9)
|
||||||
|
|
||||||
sys.exit(return_code)
|
sys.exit(len(failed_files))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import os
|
import os
|
||||||
|
import queue
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
@ -19,13 +18,6 @@ sys.path.append(os.path.dirname(__file__))
|
||||||
from helpers import basepath, shlex_quote, get_output, build_compile_commands, \
|
from helpers import basepath, shlex_quote, get_output, build_compile_commands, \
|
||||||
build_all_include, temp_header_file, git_ls_files, filter_changed
|
build_all_include, temp_header_file, git_ls_files, filter_changed
|
||||||
|
|
||||||
is_py2 = sys.version[0] == '2'
|
|
||||||
|
|
||||||
if is_py2:
|
|
||||||
import Queue as queue
|
|
||||||
else:
|
|
||||||
import queue as queue
|
|
||||||
|
|
||||||
|
|
||||||
def run_tidy(args, tmpdir, queue, lock, failed_files):
|
def run_tidy(args, tmpdir, queue, lock, failed_files):
|
||||||
while True:
|
while True:
|
||||||
|
@ -49,8 +41,8 @@ def run_tidy(args, tmpdir, queue, lock, failed_files):
|
||||||
# Use pexpect for a pseudy-TTY with colored output
|
# Use pexpect for a pseudy-TTY with colored output
|
||||||
output, rc = pexpect.run(invocation_s, withexitstatus=True, encoding='utf-8',
|
output, rc = pexpect.run(invocation_s, withexitstatus=True, encoding='utf-8',
|
||||||
timeout=15 * 60)
|
timeout=15 * 60)
|
||||||
with lock:
|
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
|
with lock:
|
||||||
print()
|
print()
|
||||||
print("\033[0;32m************* File \033[1;32m{}\033[0m".format(path))
|
print("\033[0;32m************* File \033[1;32m{}\033[0m".format(path))
|
||||||
print(output)
|
print(output)
|
||||||
|
@ -78,15 +70,15 @@ def main():
|
||||||
help='files to be processed (regex on path)')
|
help='files to be processed (regex on path)')
|
||||||
parser.add_argument('--fix', action='store_true', help='apply fix-its')
|
parser.add_argument('--fix', action='store_true', help='apply fix-its')
|
||||||
parser.add_argument('-q', '--quiet', action='store_false',
|
parser.add_argument('-q', '--quiet', action='store_false',
|
||||||
help='Run clang-tidy in quiet mode')
|
help='run clang-tidy in quiet mode')
|
||||||
parser.add_argument('-c', '--changed', action='store_true',
|
parser.add_argument('-c', '--changed', action='store_true',
|
||||||
help='Only run on changed files')
|
help='only run on changed files')
|
||||||
parser.add_argument('--split-num', type=int, help='Split the files into X jobs.',
|
parser.add_argument('--split-num', type=int, help='split the files into X jobs.',
|
||||||
default=None)
|
default=None)
|
||||||
parser.add_argument('--split-at', type=int, help='Which split is this? Starts at 1',
|
parser.add_argument('--split-at', type=int, help='which split is this? starts at 1',
|
||||||
default=None)
|
default=None)
|
||||||
parser.add_argument('--all-headers', action='store_true',
|
parser.add_argument('--all-headers', action='store_true',
|
||||||
help='Create a dummy file that checks all headers')
|
help='create a dummy file that checks all headers')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -107,13 +99,11 @@ def main():
|
||||||
build_compile_commands()
|
build_compile_commands()
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
for path in git_ls_files():
|
for path in git_ls_files(['*.cpp']):
|
||||||
filetypes = ('.cpp',)
|
files.append(os.path.relpath(path, os.getcwd()))
|
||||||
ext = os.path.splitext(path)[1]
|
|
||||||
if ext in filetypes:
|
if args.files:
|
||||||
path = os.path.relpath(path, os.getcwd())
|
# Match against files specified on command-line
|
||||||
files.append(path)
|
|
||||||
# Match against re
|
|
||||||
file_name_re = re.compile('|'.join(args.files))
|
file_name_re = re.compile('|'.join(args.files))
|
||||||
files = [p for p in files if file_name_re.search(p)]
|
files = [p for p in files if file_name_re.search(p)]
|
||||||
|
|
||||||
|
@ -133,7 +123,6 @@ def main():
|
||||||
tmpdir = tempfile.mkdtemp()
|
tmpdir = tempfile.mkdtemp()
|
||||||
|
|
||||||
failed_files = []
|
failed_files = []
|
||||||
return_code = 0
|
|
||||||
try:
|
try:
|
||||||
task_queue = queue.Queue(args.jobs)
|
task_queue = queue.Queue(args.jobs)
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
|
@ -151,7 +140,6 @@ def main():
|
||||||
|
|
||||||
# Wait for all threads to be done.
|
# Wait for all threads to be done.
|
||||||
task_queue.join()
|
task_queue.join()
|
||||||
return_code = len(failed_files)
|
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print()
|
print()
|
||||||
|
@ -168,8 +156,8 @@ def main():
|
||||||
print('Error applying fixes.\n', file=sys.stderr)
|
print('Error applying fixes.\n', file=sys.stderr)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
return return_code
|
sys.exit(len(failed_files))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
main()
|
||||||
|
|
|
@ -145,8 +145,10 @@ def filter_changed(files):
|
||||||
return files
|
return files
|
||||||
|
|
||||||
|
|
||||||
def git_ls_files():
|
def git_ls_files(patterns=None):
|
||||||
command = ["git", "ls-files", "-s"]
|
command = ["git", "ls-files", "-s"]
|
||||||
|
if patterns is not None:
|
||||||
|
command.extend(patterns)
|
||||||
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
|
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
|
||||||
output, err = proc.communicate()
|
output, err = proc.communicate()
|
||||||
lines = [x.split() for x in output.decode("utf-8").splitlines()]
|
lines = [x.split() for x in output.decode("utf-8").splitlines()]
|
||||||
|
|
Loading…
Reference in a new issue