Cleanup: delete .arcconfig
[blender-dev-tools.git] / check_source / check_header_duplicate.py
blobc6fa37426060b63ced50017d9ae90317cdb60234
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-or-later
4 """
5 Run this script to check if headers are included multiple times.
7 python3 check_header_duplicate.py ../../
9 Now build the code to find duplicate errors, resolve them manually.
11 Then restore the headers to their original state:
13 python3 check_header_duplicate.py --restore ../../
14 """
16 # Use GCC's __INCLUDE_LEVEL__ to find direct duplicate includes
18 UUID = 0
21 def source_filepath_guard(filepath):
22 global UUID
24 footer = """
25 #if __INCLUDE_LEVEL__ == 1
26 # ifdef _DOUBLEHEADERGUARD_%d
27 # error "duplicate header!"
28 # endif
29 #endif
31 #if __INCLUDE_LEVEL__ == 1
32 # define _DOUBLEHEADERGUARD_%d
33 #endif
34 """ % (UUID, UUID)
35 UUID += 1
37 with open(filepath, 'a', encoding='utf-8') as f:
38 f.write(footer)
41 def source_filepath_restore(filepath):
42 import os
43 os.system("git co %s" % filepath)
46 def scan_source_recursive(dirpath, is_restore):
47 import os
48 from os.path import join, splitext
50 # ensure git working dir is ok
51 os.chdir(dirpath)
53 def source_list(path, filename_check=None):
54 for dirpath, dirnames, filenames in os.walk(path):
55 # skip '.git'
56 dirnames[:] = [d for d in dirnames if not d.startswith(".")]
58 for filename in filenames:
59 filepath = join(dirpath, filename)
60 if filename_check is None or filename_check(filepath):
61 yield filepath
63 def is_source(filename):
64 ext = splitext(filename)[1]
65 return (ext in {".hpp", ".hxx", ".h", ".hh"})
67 def is_ignore(filename):
68 pass
70 for filepath in sorted(source_list(dirpath, is_source)):
71 print("file:", filepath)
72 if is_ignore(filepath):
73 continue
75 if is_restore:
76 source_filepath_restore(filepath)
77 else:
78 source_filepath_guard(filepath)
81 def main():
82 import sys
83 is_restore = ("--restore" in sys.argv[1:])
84 scan_source_recursive(sys.argv[-1], is_restore)
87 if __name__ == "__main__":
88 main()