-
Notifications
You must be signed in to change notification settings - Fork 14
/
oclint-json-compilation-database
executable file
·87 lines (77 loc) · 3.47 KB
/
oclint-json-compilation-database
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
import os
import platform
import json
import argparse
import re
import subprocess
import sys
OCLINT_BIN_FOLDER = os.path.dirname(os.path.abspath(__file__))
OCLINT_BIN = OCLINT_BIN_FOLDER + os.sep + "oclint"
if platform.system() == "Windows":
OCLINT_BIN += ".exe"
arg_parser = argparse.ArgumentParser(description='OCLint for JSON Compilation Database (compile_commands.json)')
arg_parser.add_argument("-v", action="store_true", dest="invocation", help="show invocation command with arguments")
arg_parser.add_argument('-debug', '--debug', action="store_true", dest="debug", help="invoke OCLint in debug mode")
arg_parser.add_argument('-i', '-include', '--include', action='append', dest='includes', help="extract files matching pattern")
arg_parser.add_argument('-e', '-exclude', '--exclude', action='append', dest='excludes', help="remove files matching pattern")
arg_parser.add_argument('-p', action='store', metavar='build-path', dest='build_path', default=os.getcwd(),
help="specify the directory containing compile_commands.json")
arg_parser.add_argument('oclint_args', nargs='*', help="arguments that are passed to OCLint invocation")
args = arg_parser.parse_args()
def get_source_path(file_attr, dir_attr):
if file_attr.startswith(os.sep):
return file_attr
elif dir_attr.endswith(os.sep):
return dir_attr + file_attr
else:
return dir_attr + os.sep + file_attr
def source_exist_at(path):
return os.path.isfile(path)
def source_list_inclusion_filter(source_list, inclusion_filter):
filtered_list = []
for path in source_list:
if re.search(inclusion_filter, path):
filtered_list.append(path)
return filtered_list
def source_list_exclusion_filter(source_list, exclusion_filter):
filtered_list = []
for path in source_list:
if not re.search(exclusion_filter, path):
filtered_list.append(path)
return filtered_list
if not source_exist_at(OCLINT_BIN):
print("Error: OCLint executable file not found.")
sys.exit(99)
json_compilation_database = os.path.join(args.build_path, 'compile_commands.json')
if not source_exist_at(json_compilation_database):
print("Error: compile_commands.json not found at %s." % args.build_path)
sys.exit(98)
compilation_database = json.load(open(json_compilation_database))
source_list = []
for file_item in compilation_database:
file_path = file_item["file"]
if not platform.system() == "Windows":
file_path = get_source_path(file_item["file"], file_item["directory"])
if source_exist_at(file_path) and not file_path in source_list:
source_list.append(file_path)
if args.includes:
matched_list = []
for inclusion_filter in args.includes:
matched_list.extend( source_list_inclusion_filter(source_list, inclusion_filter) )
source_list = matched_list
if args.excludes:
for exclusion_filter in args.excludes:
source_list = source_list_exclusion_filter(source_list, exclusion_filter)
oclint_arguments = [OCLINT_BIN, '-p', args.build_path]
if args.oclint_args:
oclint_arguments += args.oclint_args
if args.debug:
oclint_arguments.append('-debug')
oclint_arguments += source_list
if args.invocation:
print('------------------------------ OCLint ------------------------------')
print(subprocess.list2cmdline(oclint_arguments))
print('--------------------------------------------------------------------')
exit_code = subprocess.call(oclint_arguments)
sys.exit(exit_code)