forked from smartbugs/smartbugs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabel_utils.py
51 lines (35 loc) · 1.65 KB
/
label_utils.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
import os
import json
DEFAULT_LABEL_PATH = os.path.join(os.path.dirname(__file__), 'labels.json')
def read_labels(label_path=DEFAULT_LABEL_PATH):
labels = []
with open(label_path) as f:
return json.load(f)
def write_labels(labels, label_path=DEFAULT_LABEL_PATH):
with open(label_path, 'w') as f:
def print_indent(string, level=0):
print(' ' * level + string, file=f)
print_indent('[')
for cur_lab, label in enumerate(labels):
print_indent('{', 1)
for field in ['id', 'source-path', 'contract-name', 'compiler-version', 'bytecode-path', 'origin']:
print_indent(f'"{field}": "{label[field]}",', 2)
if not label['vulnerabilities']:
print_indent('"vulnerabilities": []', 2)
else:
print_indent('"vulnerabilities": [', 2)
for i, vuln in enumerate(label['vulnerabilities']):
print_indent('{', 3)
if 'lines' in vuln:
print_indent(f'"lines": [{", ".join(str(x) for x in vuln["lines"])}],', 4)
print_indent(f'"category": "{vuln["category"]}"', 4)
if i == len(label['vulnerabilities']) - 1:
print_indent('}', 3)
else:
print_indent('},', 3)
print_indent(']', 2)
if cur_lab == len(labels) - 1:
print_indent('}', 1)
else:
print_indent('},', 1)
print_indent(']')