Download app/modules/analyzer_python.py from rk147/secforge: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/analyzer_python.py
- Command line
-
hf download hf://spaces/rk147/secforge/app/modules/analyzer_python.py
-
curl -L -o analyzer_python.py https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/analyzer_python.py
2.84 kB
| """Python code analyzer using pylint.""" | |
| import subprocess | |
| import tempfile | |
| import json | |
| import os | |
| from app.modules.analyzer_base import CodeAnalyzer, AnalysisResult | |
| class PythonAnalyzer(CodeAnalyzer): | |
| def __init__(self, language: str = "python"): | |
| super().__init__(language) | |
| def analyze(self, code: str) -> AnalysisResult: | |
| with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False, encoding="utf-8") as f: | |
| f.write(code) | |
| temp_path = f.name | |
| try: | |
| result = subprocess.run( | |
| ["python", "-m", "pylint", "--output-format=json", | |
| "--disable=C0114,C0115,C0116", temp_path], | |
| capture_output=True, text=True, timeout=15 | |
| ) | |
| raw_issues = [] | |
| if result.stdout.strip(): | |
| try: | |
| raw_issues = json.loads(result.stdout) | |
| except json.JSONDecodeError: | |
| pass | |
| issues = [] | |
| for issue in raw_issues[:5]: | |
| severity_map = { | |
| "fatal": "high", | |
| "error": "high", | |
| "warning": "medium", | |
| "convention": "low", | |
| "refactor": "low", | |
| "info": "low", | |
| } | |
| # FIX: key off issue["type"] not message-id prefix — message-id split on "-" was wrong | |
| issue_type = issue.get("type", "") | |
| severity = severity_map.get(issue_type, "low") | |
| category_map = { | |
| "error": "correctness", | |
| "fatal": "correctness", | |
| "warning": "style", | |
| "convention": "style", | |
| "refactor": "style", | |
| } | |
| category = category_map.get(issue_type, "style") | |
| # Override category for known security/performance symbols | |
| symbol = issue.get("symbol", "") | |
| if symbol in ("eval-used", "exec-used", "pickle", "shell-injection"): | |
| category = "security" | |
| elif symbol in ("too-many-statements", "too-many-branches", "unnecessary-pass"): | |
| category = "performance" | |
| formatted_issue = self.format_issue( | |
| line=issue.get("line", 0), | |
| symbol=symbol, | |
| message=issue.get("message", ""), | |
| category=category, | |
| severity=severity, | |
| ) | |
| issues.append(formatted_issue) | |
| return AnalysisResult(self.language, issues) | |
| except Exception as e: | |
| return AnalysisResult(self.language, error=str(e)) | |
| finally: | |
| if os.path.exists(temp_path): | |
| os.unlink(temp_path) |