Spaces:
Paused
Paused
Upload 2 files
Browse files- analyzers/patch_generator.py +14 -0
- analyzers/rule_matcher.py +46 -0
analyzers/patch_generator.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import difflib
|
| 2 |
+
|
| 3 |
+
def generate_patch(original, fixed):
|
| 4 |
+
"""
|
| 5 |
+
original, fixed = strings
|
| 6 |
+
"""
|
| 7 |
+
diff = difflib.unified_diff(
|
| 8 |
+
original.splitlines(),
|
| 9 |
+
fixed.splitlines(),
|
| 10 |
+
fromfile="before.py",
|
| 11 |
+
tofile="after.py",
|
| 12 |
+
lineterm=""
|
| 13 |
+
)
|
| 14 |
+
return "\n".join(diff)
|
analyzers/rule_matcher.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ast
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def match_rule(rule, code_root="artifacts/code"):
|
| 5 |
+
"""
|
| 6 |
+
Returns list of violations or confirmations for a single rule ID
|
| 7 |
+
"""
|
| 8 |
+
findings = []
|
| 9 |
+
|
| 10 |
+
forbidden = rule.get("forbidden", [])
|
| 11 |
+
must = rule.get("must", [])
|
| 12 |
+
|
| 13 |
+
for dirpath, _, files in os.walk(code_root):
|
| 14 |
+
for f in files:
|
| 15 |
+
if not f.endswith(".py"):
|
| 16 |
+
continue
|
| 17 |
+
|
| 18 |
+
path = os.path.join(dirpath, f)
|
| 19 |
+
try:
|
| 20 |
+
tree = ast.parse(open(path, "r", encoding="utf-8").read())
|
| 21 |
+
except Exception as e:
|
| 22 |
+
continue
|
| 23 |
+
|
| 24 |
+
source = ast.unparse(tree)
|
| 25 |
+
|
| 26 |
+
# Forbidden checks
|
| 27 |
+
for bad in forbidden:
|
| 28 |
+
if bad in source:
|
| 29 |
+
findings.append({
|
| 30 |
+
"rule": rule["id"],
|
| 31 |
+
"status": "VIOLATED",
|
| 32 |
+
"file": path,
|
| 33 |
+
"evidence": bad
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
# Must checks (existence-based)
|
| 37 |
+
for need in must:
|
| 38 |
+
if need not in source:
|
| 39 |
+
findings.append({
|
| 40 |
+
"rule": rule["id"],
|
| 41 |
+
"status": "MISSING",
|
| 42 |
+
"file": path,
|
| 43 |
+
"evidence": need
|
| 44 |
+
})
|
| 45 |
+
|
| 46 |
+
return findings
|