-
Notifications
You must be signed in to change notification settings - Fork 14
/
bandit_parser.py
executable file
·115 lines (95 loc) · 3.29 KB
/
bandit_parser.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import argparse
import json
import os.path
import sys
class BanditResult:
def __init__(self, rs):
self.code = rs["code"]
self.filename = rs["filename"]
self.issue_confidence = rs["issue_confidence"]
self.issue_severity = rs["issue_severity"]
self.issue_text = rs["issue_text"]
self.line_number = rs["line_number"]
self.line_range = rs["line_range"]
self.more_info = rs["more_info"]
self.test_id = rs["test_id"]
self.test_name = rs["test_name"]
def __str__(self):
return "{}(s:{}-c:{}) {}:{}".format(self.test_name,
self.issue_severity,
self.issue_confidence,
self.filename,
self.line_number)
class Bandit:
def __init__(self, d):
self.errors = d["errors"]
self.timestamp = d["generated_at"]
self.metrics = d["metrics"]
self.results = []
try:
for rs in d["results"]:
r = BanditResult(rs)
self.results.append(r)
except KeyError as e:
print("failed to initialise BanditResult: " + str(e))
sys.exit(-1)
def __str__(self):
s = self.timestamp + '\n'
i = 1
for r in bandit.results:
s += (str(i) + ": " + str(r) + '\n')
i += 1
return s
def parse_args():
parser = argparse.ArgumentParser(description="Parser for bandit output")
parser.add_argument("in_file", help="the bandit output file to parse")
parser.add_argument("out_file", help="the issues output file to save")
parser.add_argument("-d", "--diagnostic",
dest="diagnostic",
action="store_true",
help="enable diagnostic mode")
return parser.parse_args()
def diagnose(s):
if args.diagnostic:
print(s)
def write_issue(f, r, i):
f.write("index: {}\\n".format(i))
f.write("issue_id: {} {}\\n".format(r.test_id, r.test_name))
f.write("issue_text: {}\\n".format(r.issue_text))
f.write("severity: {}\\n".format(r.issue_severity))
f.write("confidence: {}\\n".format(r.issue_confidence))
f.write("file: {}\\n".format(r.filename))
f.write("line: {}\\n".format(r.line_number))
f.write("code: {}\\n".format(r.code))
f.write("\\n")
if __name__ == "__main__":
args = parse_args()
if not os.path.exists(args.in_file):
print("bandit output file not found!")
sys.exit(-1)
diagnose(args.in_file)
if os.path.exists(args.out_file):
print("issue output file already exist!")
sys.exit(-1)
diagnose(args.out_file)
try:
with open(args.in_file) as f:
data = json.load(f)
except ValueError as e:
print("failed to load json file: " + str(e))
sys.exit(-1)
diagnose(data)
try:
bandit = Bandit(data)
except KeyError as e:
print("failed to initialise Bandit: " + str(e))
sys.exit(-1)
diagnose(bandit)
with open(args.out_file, 'w') as f:
i = 1
for r in bandit.results:
write_issue(f, r, i)
i += 1
if len(bandit.results):
sys.exit(1)
sys.exit(0)