(code: str, *, filename: str = "app.py")
| 22 | |
| 23 | |
| 24 | def run_pyspector(code: str, *, filename: str = "app.py") -> list[dict]: |
| 25 | from pyspector._rust_core import run_scan |
| 26 | from pyspector.config import get_default_rules |
| 27 | |
| 28 | rules_toml = get_default_rules() |
| 29 | |
| 30 | with tempfile.TemporaryDirectory() as tmpdir: |
| 31 | file_path = os.path.join(tmpdir, filename) |
| 32 | Path(file_path).write_text(_wrap_in_function(code)) |
| 33 | |
| 34 | import ast as _ast, json as _json |
| 35 | from pyspector.cli import AstEncoder |
| 36 | |
| 37 | with warnings.catch_warnings(): |
| 38 | warnings.filterwarnings("ignore") |
| 39 | try: |
| 40 | tree = _ast.parse(Path(file_path).read_text()) |
| 41 | ast_json = _json.dumps(tree, cls=AstEncoder) |
| 42 | except Exception: |
| 43 | ast_json = "{}" |
| 44 | |
| 45 | python_files = [{ |
| 46 | "file_path": filename, |
| 47 | "content": Path(file_path).read_text(), |
| 48 | "ast_json": ast_json, |
| 49 | }] |
| 50 | |
| 51 | results = run_scan(tmpdir, rules_toml, {"exclude": []}, python_files) |
| 52 | |
| 53 | return [{"rule_id": r.rule_id, "file_path": r.file_path, |
| 54 | "line_number": r.line_number, "code": r.code} |
| 55 | for r in results] |
| 56 | |
| 57 | |
| 58 | def findings_for(code, rule_id, **kw): |
no test coverage detected