TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
file_linter.py
1import os
2import subprocess
3import sys
4
5print("Python {}.{}.{}".format(*sys.version_info)) # Python 3.8
6with open("git_diff.txt") as in_file:
7 modified_files = sorted(in_file.read().splitlines())
8 print("{} files were modified.".format(len(modified_files)))
9
10 cpp_exts = tuple(".c .c++ .cc .cpp .cu .cuh .cxx .h .h++ .hh .hpp .hxx".split())
11 cpp_files = [file for file in modified_files if file.lower().endswith(cpp_exts)]
12 print(f"{len(cpp_files)} C++ files were modified.")
13 if not cpp_files:
14 sys.exit(0)
15
16 subprocess.run(["clang-tidy", "--fix", "-p=build", "--extra-arg=-std=c++11", *cpp_files, "--"],
17 check=True, text=True, stderr=subprocess.STDOUT)
18
19 subprocess.run(["clang-format", "-i", "-style=file", *cpp_files],
20 check=True, text=True, stderr=subprocess.STDOUT)
21
22 upper_files = [file for file in cpp_files if file != file.lower()]
23 if upper_files:
24 print(f"{len(upper_files)} files contain uppercase characters:")
25 print("\n".join(upper_files) + "\n")
26
27 space_files = [file for file in cpp_files if " " in file or "-" in file]
28 if space_files:
29 print(f"{len(space_files)} files contain space or dash characters:")
30 print("\n".join(space_files) + "\n")
31
32 nodir_files = [file for file in cpp_files if file.count(os.sep) != 1]
33 if nodir_files:
34 print(f"{len(nodir_files)} files are not in one and only one directory:")
35 print("\n".join(nodir_files) + "\n")
36
37 bad_files = len(upper_files + space_files + nodir_files)
38 if bad_files:
39 sys.exit(bad_files)