Spaces:
Sleeping
Sleeping
File size: 4,834 Bytes
cf05092 b196357 cf05092 b196357 cf05092 902cd29 cf05092 b196357 cf05092 b196357 cf05092 902cd29 b196357 902cd29 b196357 899a7c7 902cd29 899a7c7 902cd29 899a7c7 902cd29 899a7c7 902cd29 899a7c7 cf05092 b196357 902cd29 b196357 | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | from __future__ import annotations
import json
import os
from concurrent.futures import ThreadPoolExecutor
import subprocess
from pathlib import Path
import sys
from pydantic import BaseModel
class LinterIssue(BaseModel):
tool: str
line: int
severity: str
code: str
message: str
_PYLINT_SEVERITY_MAP = {
"fatal": "high",
"error": "high",
"warning": "medium",
"refactor": "low",
"convention": "low",
"info": "low",
}
_BANDIT_SEVERITY_MAP = {
"high": "high",
"medium": "medium",
"low": "low",
}
def _timeout_seconds() -> int:
return int(os.getenv("GRAPHREVIEW_LINTER_TIMEOUT_SECONDS", "20"))
def run_pylint(path: Path) -> list[LinterIssue]:
cmd = [
sys.executable,
"-m",
"pylint",
str(path),
"--output-format=json2",
"--score=n",
"--reports=n",
"--errors-only",
]
try:
proc = subprocess.run(
cmd,
capture_output=True,
text=True,
check=False,
timeout=_timeout_seconds(),
)
except subprocess.TimeoutExpired:
return []
payload = (proc.stdout or "").strip()
if not payload:
return []
try:
data = json.loads(payload)
except json.JSONDecodeError:
return []
messages = data.get("messages", []) if isinstance(data, dict) else []
issues: list[LinterIssue] = []
for message in messages:
severity = _PYLINT_SEVERITY_MAP.get(str(message.get("type", "")).lower(), "low")
issues.append(
LinterIssue(
tool="pylint",
line=int(message.get("line", 0)),
severity=severity,
code=str(message.get("messageId", "PL0000")),
message=str(message.get("message", "")),
)
)
return issues
def run_bandit(path: Path) -> list[LinterIssue]:
cmd = [sys.executable, "-m", "bandit", "-q", "-f", "json", str(path)]
try:
proc = subprocess.run(
cmd,
capture_output=True,
text=True,
check=False,
timeout=_timeout_seconds(),
)
except subprocess.TimeoutExpired:
return []
payload = (proc.stdout or "").strip()
if not payload:
return []
try:
data = json.loads(payload)
except json.JSONDecodeError:
return []
results = data.get("results", []) if isinstance(data, dict) else []
issues: list[LinterIssue] = []
for item in results:
raw_sev = str(item.get("issue_severity", "LOW")).lower()
issues.append(
LinterIssue(
tool="bandit",
line=int(item.get("line_number", 0)),
severity=_BANDIT_SEVERITY_MAP.get(raw_sev, "low"),
code=str(item.get("test_id", "B000")),
message=str(item.get("issue_text", "")),
)
)
return issues
def run_pyright(path: Path) -> list[LinterIssue]:
pyright_bin = str((Path(sys.executable).resolve().parent / "pyright"))
cmd = [pyright_bin if Path(pyright_bin).exists() else "pyright", "--strict", "--outputjson", str(path)]
try:
proc = subprocess.run(
cmd,
capture_output=True,
text=True,
check=False,
timeout=_timeout_seconds(),
)
except FileNotFoundError:
# Optional dependency in lightweight/docker environments.
return []
except subprocess.TimeoutExpired:
return []
payload = (proc.stdout or "").strip()
if not payload:
return []
try:
parsed = json.loads(payload)
except json.JSONDecodeError:
return []
issues: list[LinterIssue] = []
for item in parsed.get("generalDiagnostics", []):
if not isinstance(item, dict):
continue
if str(item.get("severity") or "").lower() != "error":
continue
line = int(((item.get("range") or {}).get("start") or {}).get("line") or 0) + 1
issues.append(
LinterIssue(
tool="pyright",
line=line,
severity="high",
code=str(item.get("rule") or "PYRIGHT"),
message=str(item.get("message") or ""),
)
)
return issues
def run_linters(path: Path) -> list[LinterIssue]:
with ThreadPoolExecutor(max_workers=3) as pool:
py_future = pool.submit(run_pylint, path)
ba_future = pool.submit(run_bandit, path)
fl_future = pool.submit(run_pyright, path)
issues = py_future.result()
issues.extend(ba_future.result())
issues.extend(fl_future.result())
return sorted(issues, key=lambda item: (item.line, item.tool, item.code, item.message))
|