generated from srijan-deepsource/custom-analyzer-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze.py
59 lines (49 loc) · 1.48 KB
/
analyze.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
import json
import os
import subprocess
from helper import (
make_issue,
prepare_result,
publish_results,
get_files,
)
codepath = os.environ.get("CODE_PATH", "/Users/sauravsrijan/work/macros/demo/django")
resultpath = "/tmp/results.json"
app_path = os.path.dirname(os.path.abspath(__file__))
files_to_analyze = [filename for filename in get_files(codepath) if filename.endswith(".py")]
analysis_command = [
"/toolbox/venv/bin/semgrep",
"--json",
"-o",
resultpath,
# Load the rules config
"-f",
os.path.join(app_path, "django-rules")
]
def _get_issues():
"""Run the checks."""
issues = []
if not files_to_analyze:
return issues
# There are files to analyze
subprocess.run(analysis_command + files_to_analyze)
# Read the json, convert it into DS's format.
with open(resultpath) as fp:
raised_issues = json.load(fp)["results"]
for issue in raised_issues:
issue_code = issue["check_id"].split("::")[-1]
issue_text = issue["extra"]["message"]
filepath = issue["path"]
startline = issue["start"]["line"]
startcol = issue["start"]["col"]
endline = issue["end"]["line"]
endcol = issue["end"]["col"]
issues.append(
make_issue(
issue_code, issue_text, filepath, startline, startcol, endline, endcol
)
)
return issues
issues = _get_issues()
# Publish to DeepSource
publish_results(prepare_result(issues))