generated from srijan-deepsource/custom-analyzer-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
73 lines (57 loc) · 1.81 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
66
67
68
69
70
71
72
73
import json
import os
CODEPATH = os.getenv("CODE_PATH", "/code")
TOOLBOX_PATH = os.getenv("TOOLBOX_PATH", "/toolbox")
ISSUES = []
CURRENT_FILEPATH = ""
def get_current_filepath() -> str:
return CURRENT_FILEPATH
def set_current_filepath(filepath: str) -> str:
global CURRENT_FILEPATH
CURRENT_FILEPATH = filepath
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)
with open(os.path.join(TOOLBOX_PATH, 'analysis_results.json'), "w") as fp:
fp.write(json.dumps(result))
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):
"""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": line,
"column": 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