2020-09-10 20:58:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-09-10 02:06:05 +00:00
|
|
|
from collections import defaultdict
|
2020-09-25 22:58:58 +00:00
|
|
|
import os
|
2020-09-10 02:06:05 +00:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2020-09-25 22:58:58 +00:00
|
|
|
os.chdir(os.path.dirname(__file__) + "/..")
|
2020-09-10 02:06:05 +00:00
|
|
|
|
2020-09-26 19:49:35 +00:00
|
|
|
ignore_files = ["src/engine/keys.h", "src/engine/client/keynames.h"]
|
|
|
|
|
2020-09-25 22:58:58 +00:00
|
|
|
def recursive_file_list(path):
|
|
|
|
result = []
|
|
|
|
for dirpath, dirnames, filenames in os.walk(path):
|
2020-09-26 19:49:35 +00:00
|
|
|
result += filter(lambda p: p not in ignore_files, [os.path.join(dirpath, filename) for filename in filenames])
|
2020-09-25 22:58:58 +00:00
|
|
|
return result
|
2020-09-10 02:06:05 +00:00
|
|
|
|
2020-09-25 22:58:58 +00:00
|
|
|
def filter_cpp(filenames):
|
|
|
|
return [filename for filename in filenames
|
|
|
|
if any(filename.endswith(ext) for ext in ".c .cpp .h".split())]
|
2020-09-10 02:06:05 +00:00
|
|
|
|
2020-09-25 22:58:58 +00:00
|
|
|
def reformat(filenames):
|
|
|
|
subprocess.check_call(["clang-format", "-i"] + filenames)
|
2020-09-10 02:06:05 +00:00
|
|
|
|
2020-09-25 22:58:58 +00:00
|
|
|
def warn(filenames):
|
|
|
|
return subprocess.call(["clang-format", "-Werror", "--dry-run"] + filenames)
|
2020-09-10 02:06:05 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
import argparse
|
2020-09-25 16:44:40 +00:00
|
|
|
p = argparse.ArgumentParser(description="Check and fix style of changed files")
|
2020-09-10 02:06:05 +00:00
|
|
|
p.add_argument("-n", "--dry-run", action="store_true", help="Don't fix, only warn")
|
|
|
|
args = p.parse_args()
|
2020-09-25 22:58:58 +00:00
|
|
|
filenames = filter_cpp(recursive_file_list("src"))
|
2020-09-10 02:06:05 +00:00
|
|
|
if not args.dry_run:
|
2020-09-25 22:58:58 +00:00
|
|
|
reformat(filenames)
|
2020-09-10 02:06:05 +00:00
|
|
|
else:
|
2020-09-25 22:58:58 +00:00
|
|
|
sys.exit(warn(filenames))
|
2020-09-10 02:06:05 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|