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:
@@ -11,6 +11,7 @@ from helpers import (
|
||||
load_idedata,
|
||||
root_path,
|
||||
basepath,
|
||||
get_binary,
|
||||
)
|
||||
import argparse
|
||||
import click
|
||||
@@ -26,6 +27,7 @@ import tempfile
|
||||
import threading
|
||||
|
||||
|
||||
|
||||
def clang_options(idedata):
|
||||
cmd = []
|
||||
|
||||
@@ -110,10 +112,12 @@ def clang_options(idedata):
|
||||
return cmd
|
||||
|
||||
|
||||
def run_tidy(args, options, tmpdir, queue, lock, failed_files):
|
||||
pids = set()
|
||||
|
||||
def run_tidy(executable, args, options, tmpdir, queue, lock, failed_files):
|
||||
while True:
|
||||
path = queue.get()
|
||||
invocation = ["clang-tidy-14"]
|
||||
invocation = [executable]
|
||||
|
||||
if tmpdir is not None:
|
||||
invocation.append("--export-fixes")
|
||||
@@ -193,22 +197,6 @@ def main():
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
get_output("clang-tidy-14", "-version")
|
||||
except:
|
||||
print(
|
||||
"""
|
||||
Oops. It looks like clang-tidy-14 is not installed.
|
||||
|
||||
Please check you can run "clang-tidy-14 -version" in your terminal and install
|
||||
clang-tidy (v14) if necessary.
|
||||
|
||||
Note you can also upload your code as a pull request on GitHub and see the CI check
|
||||
output to apply clang-tidy.
|
||||
"""
|
||||
)
|
||||
return 1
|
||||
|
||||
idedata = load_idedata(args.environment)
|
||||
options = clang_options(idedata)
|
||||
|
||||
@@ -242,12 +230,13 @@ def main():
|
||||
|
||||
failed_files = []
|
||||
try:
|
||||
executable = get_binary("clang-tidy", 14)
|
||||
task_queue = queue.Queue(args.jobs)
|
||||
lock = threading.Lock()
|
||||
for _ in range(args.jobs):
|
||||
t = threading.Thread(
|
||||
target=run_tidy,
|
||||
args=(args, options, tmpdir, task_queue, lock, failed_files),
|
||||
args=(executable, args, options, tmpdir, task_queue, lock, failed_files),
|
||||
)
|
||||
t.daemon = True
|
||||
t.start()
|
||||
@@ -262,12 +251,17 @@ 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.")
|
||||
if tmpdir:
|
||||
shutil.rmtree(tmpdir)
|
||||
# Kill subprocesses (and ourselves!)
|
||||
# No simple, clean alternative appears to be available.
|
||||
os.kill(0, 9)
|
||||
return 2 # Will not execute.
|
||||
|
||||
if args.fix and failed_files:
|
||||
print("Applying fixes ...")
|
||||
@@ -277,8 +271,8 @@ def main():
|
||||
print("Error applying fixes.\n", file=sys.stderr)
|
||||
raise
|
||||
|
||||
sys.exit(len(failed_files))
|
||||
return len(failed_files)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
sys.exit(main())
|
||||
|
Reference in New Issue
Block a user