generated from srijan-deepsource/custom-analyzer-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
65 lines (53 loc) · 1.7 KB
/
helper.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
import json
import tempfile
import os
import subprocess
def prepare_result(issues):
"""Prepare the result for the DeepSource analyzer framework to publish."""
return {
"issues": issues,
"metrics": [],
"is_passed": True if issues else False,
"errors": [],
"extra_data": ""
}
def publish_results(result):
"""Publish the analysis results."""
# write results into a json file:
print("Raising issues: ", result)
res_file = tempfile.NamedTemporaryFile().name
with open(res_file, "w") as fp:
fp.write(json.dumps(result))
# publish result via marvin:
subprocess.run(["/toolbox/marvin", "--publish-report", res_file])
def get_vcs_filepath(filepath):
"""Remove the /code/ prefix."""
if filepath.startswith("/code/"):
filepath = filepath[6:]
return filepath
def make_issue(issue_code, issue_txt, filepath, line, col, endline=None, endcol=None):
"""Prepare issue structure for the given issue data."""
filepath = get_vcs_filepath(filepath)
return {
"issue_code": issue_code,
"issue_text": issue_txt,
"location": {
"path": filepath,
"position": {
"begin": {
"line": line,
"column": col
},
"end": {
"line": endline or line,
"column": endcol or col
}
}
}
}
def get_files(base_dir):
"""Return a generator with filepaths in base_dir."""
for subdir, _, filenames in os.walk(base_dir):
for filename in filenames:
filepath = os.path.join(subdir, filename)
yield filepath