1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-01 10:52:19 +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:
Oxan van Leeuwen
2021-07-25 23:54:32 +02:00
committed by GitHub
parent 66cdb761dc
commit 3749c11f21
4 changed files with 57 additions and 77 deletions

View File

@@ -1,10 +1,9 @@
#!/usr/bin/env python3
from __future__ import print_function
import argparse
import multiprocessing
import os
import queue
import re
import subprocess
import sys
@@ -13,59 +12,47 @@ import threading
import click
sys.path.append(os.path.dirname(__file__))
from helpers import basepath, 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())
from helpers import get_output, git_ls_files, filter_changed
def run_format(args, queue, lock):
"""Takes filenames out of queue and runs clang-tidy on them."""
def run_format(args, queue, lock, failed_files):
"""Takes filenames out of queue and runs clang-format on them."""
while True:
path = queue.get()
invocation = ['clang-format-11']
if args.inplace:
invocation.append('-i')
else:
invocation.extend(['--dry-run', '-Werror'])
invocation.append(path)
proc = subprocess.Popen(invocation, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, err = proc.communicate()
with lock:
if proc.returncode != 0:
print(' '.join(invocation))
print(output.decode('utf-8'))
print(err.decode('utf-8'))
proc = subprocess.run(invocation, capture_output=True, encoding='utf-8')
if proc.returncode != 0:
with lock:
print()
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()
def progress_bar_show(value):
if value is None:
return ''
return value
return value if value is not None else ''
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-j', '--jobs', type=int,
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=[],
help='files to be processed (regex on path)')
parser.add_argument('-i', '--inplace', action='store_true',
help='apply fix-its')
parser.add_argument('-q', '--quiet', action='store_false',
help='Run clang-tidy in quiet mode')
help='reformat files in-place')
parser.add_argument('-c', '--changed', action='store_true',
help='Only run on changed files')
help='only run on changed files')
args = parser.parse_args()
try:
@@ -75,7 +62,7 @@ def main():
Oops. It looks like clang-format is not installed.
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
output to apply clang-format.
@@ -83,28 +70,26 @@ def main():
return 1
files = []
for path in git_ls_files():
filetypes = ('.cpp', '.h', '.tcc')
ext = os.path.splitext(path)[1]
if ext in filetypes:
path = os.path.relpath(path, os.getcwd())
files.append(path)
# Match against re
file_name_re = re.compile('|'.join(args.files))
files = [p for p in files if file_name_re.search(p)]
for path in git_ls_files(['*.cpp', '*.h', '*.tcc']):
files.append(os.path.relpath(path, os.getcwd()))
if args.files:
# Match against files specified on command-line
file_name_re = re.compile('|'.join(args.files))
files = [p for p in files if file_name_re.search(p)]
if args.changed:
files = filter_changed(files)
files.sort()
return_code = 0
failed_files = []
try:
task_queue = queue.Queue(args.jobs)
lock = threading.Lock()
for _ in range(args.jobs):
t = threading.Thread(target=run_format,
args=(args, task_queue, lock))
args=(args, task_queue, lock, failed_files))
t.daemon = True
t.start()
@@ -122,7 +107,7 @@ def main():
print('Ctrl-C detected, goodbye.')
os.kill(0, 9)
sys.exit(return_code)
sys.exit(len(failed_files))
if __name__ == '__main__':