2019-12-07 18:28:55 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2024-01-03 06:00:52 +01:00
|
|
|
from helpers import (
|
|
|
|
print_error_for_file,
|
|
|
|
get_output,
|
|
|
|
git_ls_files,
|
|
|
|
filter_changed,
|
|
|
|
get_binary,
|
|
|
|
)
|
2019-05-12 23:04:36 +02:00
|
|
|
import argparse
|
2021-11-25 21:54:11 +01:00
|
|
|
import click
|
|
|
|
import colorama
|
2019-04-17 12:06:00 +02:00
|
|
|
import multiprocessing
|
|
|
|
import os
|
2021-07-25 23:54:32 +02:00
|
|
|
import queue
|
2019-04-17 12:06:00 +02:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-05-12 23:04:36 +02:00
|
|
|
import threading
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
2024-01-03 06:00:52 +01:00
|
|
|
|
|
|
|
def run_format(executable, args, queue, lock, failed_files):
|
2021-07-25 23:54:32 +02:00
|
|
|
"""Takes filenames out of queue and runs clang-format on them."""
|
2019-04-17 12:06:00 +02:00
|
|
|
while True:
|
|
|
|
path = queue.get()
|
2024-01-03 06:00:52 +01:00
|
|
|
invocation = [executable]
|
2019-04-17 12:06:00 +02:00
|
|
|
if args.inplace:
|
2022-02-10 09:55:11 +01:00
|
|
|
invocation.append("-i")
|
2021-07-25 23:54:32 +02:00
|
|
|
else:
|
2022-02-10 09:55:11 +01:00
|
|
|
invocation.extend(["--dry-run", "-Werror"])
|
2019-04-17 12:06:00 +02:00
|
|
|
invocation.append(path)
|
|
|
|
|
2022-02-10 09:55:11 +01:00
|
|
|
proc = subprocess.run(invocation, capture_output=True, encoding="utf-8")
|
2021-07-25 23:54:32 +02:00
|
|
|
if proc.returncode != 0:
|
|
|
|
with lock:
|
2021-11-25 21:54:11 +01:00
|
|
|
print_error_for_file(path, proc.stderr)
|
2021-07-25 23:54:32 +02:00
|
|
|
failed_files.append(path)
|
2019-04-17 12:06:00 +02:00
|
|
|
queue.task_done()
|
|
|
|
|
|
|
|
|
|
|
|
def progress_bar_show(value):
|
2022-02-10 09:55:11 +01:00
|
|
|
return value if value is not None else ""
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2021-11-25 21:54:11 +01:00
|
|
|
colorama.init()
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
2022-02-10 09:55:11 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-j",
|
|
|
|
"--jobs",
|
|
|
|
type=int,
|
|
|
|
default=multiprocessing.cpu_count(),
|
|
|
|
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="reformat files in-place"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-c", "--changed", action="store_true", help="only run on changed files"
|
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
files = []
|
2022-02-10 09:55:11 +01:00
|
|
|
for path in git_ls_files(["*.cpp", "*.h", "*.tcc"]):
|
2021-07-25 23:54:32 +02:00
|
|
|
files.append(os.path.relpath(path, os.getcwd()))
|
|
|
|
|
|
|
|
if args.files:
|
|
|
|
# Match against files specified on command-line
|
2022-02-10 09:55:11 +01:00
|
|
|
file_name_re = re.compile("|".join(args.files))
|
2021-07-25 23:54:32 +02:00
|
|
|
files = [p for p in files if file_name_re.search(p)]
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
if args.changed:
|
|
|
|
files = filter_changed(files)
|
|
|
|
|
|
|
|
files.sort()
|
|
|
|
|
2021-07-25 23:54:32 +02:00
|
|
|
failed_files = []
|
2019-04-17 12:06:00 +02:00
|
|
|
try:
|
2024-01-03 06:00:52 +01:00
|
|
|
executable = get_binary("clang-format", 13)
|
2019-04-17 12:06:00 +02:00
|
|
|
task_queue = queue.Queue(args.jobs)
|
|
|
|
lock = threading.Lock()
|
|
|
|
for _ in range(args.jobs):
|
2022-02-10 09:55:11 +01:00
|
|
|
t = threading.Thread(
|
2024-01-03 06:00:52 +01:00
|
|
|
target=run_format, args=(executable, args, task_queue, lock, failed_files)
|
2022-02-10 09:55:11 +01:00
|
|
|
)
|
2019-04-17 12:06:00 +02:00
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
# Fill the queue with files.
|
2022-02-10 09:55:11 +01:00
|
|
|
with click.progressbar(
|
|
|
|
files, width=30, file=sys.stderr, item_show_func=progress_bar_show
|
|
|
|
) as bar:
|
2019-04-17 12:06:00 +02:00
|
|
|
for name in bar:
|
|
|
|
task_queue.put(name)
|
|
|
|
|
|
|
|
# Wait for all threads to be done.
|
|
|
|
task_queue.join()
|
|
|
|
|
2024-01-03 06:00:52 +01:00
|
|
|
except FileNotFoundError as ex:
|
|
|
|
return 1
|
2019-04-17 12:06:00 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
2022-02-10 09:55:11 +01:00
|
|
|
print("Ctrl-C detected, goodbye.")
|
2024-01-03 06:00:52 +01:00
|
|
|
# Kill subprocesses (and ourselves!)
|
|
|
|
# No simple, clean alternative appears to be available.
|
2019-04-17 12:06:00 +02:00
|
|
|
os.kill(0, 9)
|
2024-01-03 06:00:52 +01:00
|
|
|
return 2 # Will not execute.
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2024-01-03 06:00:52 +01:00
|
|
|
return len(failed_files)
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
|
2022-02-10 09:55:11 +01:00
|
|
|
if __name__ == "__main__":
|
2024-01-03 06:00:52 +01:00
|
|
|
sys.exit(main())
|