2 from __future__
import print_function
7 from concurrent.futures
import ProcessPoolExecutor
9 from fnmatch
import fnmatch
14 return "{: >4d} ".
format(line)
18 lines = code.split(
"\n")
23 nlup = int(maxlines / 2)
24 nllo = maxlines - nlup - 1
27 lines = lines[:nlup] + [
" " * 5 +
"// ..."] + lines[-nllo:]
29 return "\n".
join(lines)
38 r"(#ifndef [A-Za-z0-9_]*\n#define [A-Za-z0-9_]*.*)\n((:?.|\n)+?)#endif",
42 match_global = re.search(
43 r"#ifndef (.*)\n#define \1.*\n[\s\S]+#endif[A-Za-z0-9\-_/* ]*$", text
50 if match_global
is not None and len(match_local) <= 1:
53 errbuf +=
"This looks like a file-spanning include guard\n"
54 errbuf +=
"This is discouraged as per [ACTS-450]"
55 errbuf +=
"(https://its.cern.ch/jira/browse/ACTS-450)" +
"\n" * 2
57 start = text[: match_global.start()].
count(
"\n") + 1
58 errbuf +=
code_print(match_global.group(0), start)
61 if valid_global
or len(match_local) > 1:
64 lineno = text[: m.start()].
count(
"\n") + 1
67 errbuf +=
"This looks like a local #ifndef / include-guard\n"
68 errbuf +=
"This is discouraged as per [ACTS-450]"
69 errbuf +=
"(https://its.cern.ch/jira/browse/ACTS-450)" +
"\n" * 2
73 return valid_local, valid_global, errbuf
77 p = argparse.ArgumentParser()
80 Input files: either file path, dir path (will glob for headers) or custom glob pattern
82 p.add_argument(
"input", help=input_help.strip())
84 "--fail-local",
"-l", action=
"store_true", help=
"Fail on local include guards"
87 "--fail-global",
"-g", action=
"store_true", help=
"Fail on global include guards"
89 p.add_argument(
"--quiet-local",
"-ql", action=
"store_true")
90 p.add_argument(
"--quiet-global",
"-qg", action=
"store_true")
91 p.add_argument(
"--exclude",
"-e", action=
"append", default=[])
97 if os.path.isfile(args.input):
98 headers = [args.input]
99 elif os.path.isdir(args.input):
100 patterns = [
"**/*.hpp",
"**/*.h"]
102 [glob(os.path.join(args.input, p), recursive=
True)
for p
in patterns], []
105 headers = glob(args.input, recursive=
True)
112 if any([fnmatch(h, e)
for e
in args.exclude]):
125 if not valid_local
or not valid_global:
126 head =
"Issue(s) in file {}:\n".
format(h)
127 print(
"-" * len(head))
134 print(
"Issues found in {} files".
format(nlocal + nglobal))
135 print(
"{} files have local include guards".
format(nlocal))
136 print(
"{} files have global include guards".
format(nglobal))
144 if "__main__" == __name__: