1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-01 10:52:19 +01:00

clang-format and clang-tidy scripts: More robust algorithm to find correct executable (#6041)

* More robust algorithm to find correct executable

* Revise message wording

* Add clang-tidy and clang-format to requirements.txt.
Add to message explaining install process.

* Extracted get_binary to helpers.py. Use execptions for clean exit.

* Add parameter types

* clang-{tidy,format} in requirements_test.txt
clean up script exit

* Kill processes on ^C

* Move clang-tidy and clang-format into requirements_dev.txt
This commit is contained in:
Clyde Stubbs
2024-01-03 16:00:52 +11:00
committed by GitHub
parent ae52164d9c
commit a2e152ad12
6 changed files with 78 additions and 46 deletions

View File

@@ -1,6 +1,12 @@
#!/usr/bin/env python3
from helpers import print_error_for_file, get_output, git_ls_files, filter_changed
from helpers import (
print_error_for_file,
get_output,
git_ls_files,
filter_changed,
get_binary,
)
import argparse
import click
import colorama
@@ -13,11 +19,12 @@ import sys
import threading
def run_format(args, queue, lock, failed_files):
def run_format(executable, args, queue, lock, failed_files):
"""Takes filenames out of queue and runs clang-format on them."""
while True:
path = queue.get()
invocation = ["clang-format-13"]
invocation = [executable]
if args.inplace:
invocation.append("-i")
else:
@@ -58,22 +65,6 @@ def main():
)
args = parser.parse_args()
try:
get_output("clang-format-13", "-version")
except:
print(
"""
Oops. It looks like clang-format is not installed.
Please check you can run "clang-format-13 -version" in your terminal and install
clang-format (v13) 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.
"""
)
return 1
files = []
for path in git_ls_files(["*.cpp", "*.h", "*.tcc"]):
files.append(os.path.relpath(path, os.getcwd()))
@@ -90,11 +81,12 @@ def main():
failed_files = []
try:
executable = get_binary("clang-format", 13)
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, failed_files)
target=run_format, args=(executable, args, task_queue, lock, failed_files)
)
t.daemon = True
t.start()
@@ -109,13 +101,18 @@ def main():
# Wait for all threads to be done.
task_queue.join()
except FileNotFoundError as ex:
return 1
except KeyboardInterrupt:
print()
print("Ctrl-C detected, goodbye.")
# Kill subprocesses (and ourselves!)
# No simple, clean alternative appears to be available.
os.kill(0, 9)
return 2 # Will not execute.
sys.exit(len(failed_files))
return len(failed_files)
if __name__ == "__main__":
main()
sys.exit(main())