-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeclimate-cpplint.py
More file actions
71 lines (60 loc) · 1.89 KB
/
codeclimate-cpplint.py
File metadata and controls
71 lines (60 loc) · 1.89 KB
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
#!/usr/bin/env python3
import subprocess
import sys
import json
import re
CONFIG_FILE_LOCATION = "/config.json"
CODE_LOCATION = "/code"
ECLIPSE_REGEX = re.compile(r'^(.*):([0-9]+):\s*(\w+):\s+(.*?)\s+\[([^\]]+)\]\s+\[([0-9]+)\]')
def print_issue(line: str):
matches = ECLIPSE_REGEX.fullmatch(line)
if matches is None:
return
(file, line, severity, message, check, level) = matches.group(1, 2, 3, 4, 5, 6)
issue = {
"type": "issue",
"check_name": check,
"description": message,
"categories": ["Style"],
"location": {
"path": file,
"lines": {
"begin": int(line),
"end": int(line)
}
}
}
json.dump(issue, sys.stdout)
print("\0")
def run_cpplint(workspace, files, config):
cmd = ["cpplint", "--output=eclipse", "--quiet", "--recursive"]
if type(config) is dict:
for option in config:
if option in ["output", "quiet"]:
continue
cmd.append("--%s=%s" % (option, config[option]))
else:
print("Warning: 'config' should be an object but is %s" % type(config).__name__, file=sys.stderr)
cmd.extend(files)
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if e.returncode == 1:
output = e.output
else:
raise e
if len(output):
output = output.decode('utf-8')
for line in output.splitlines():
print_issue(line)
def main():
config_file_path = CONFIG_FILE_LOCATION
workspace_path = CODE_LOCATION
with open(config_file_path, 'r') as config_file:
config = json.load(config_file)
engine_config = config["config"]
run_cpplint(workspace_path, config["include_paths"], engine_config)
print("\0")
pass
if __name__ == "__main__":
main()