-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathpycompile_check.sh
More file actions
executable file
·27 lines (23 loc) · 932 Bytes
/
pycompile_check.sh
File metadata and controls
executable file
·27 lines (23 loc) · 932 Bytes
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
#!/bin/bash
# Stop hook: Run Python syntax check on all .py files in the workspace
# Returns deny if any Python file has syntax errors, with the error output as feedback
#
# This hook validates that the agent hasn't broken any Python files.
# Environment variable CHECK_DIR can override the default working directory.
CHECK_DIR="${CHECK_DIR:-.}"
# Find all Python files and check for syntax errors
ERRORS=""
while IFS= read -r -d '' file; do
# Run python syntax check
result=$(python3 -m py_compile "$file" 2>&1)
if [ $? -ne 0 ]; then
ERRORS="${ERRORS}\n${result}"
fi
done < <(find "$CHECK_DIR" -name "*.py" -print0 2>/dev/null)
if [ -n "$ERRORS" ]; then
# Escape the output for JSON
ESCAPED_OUTPUT=$(echo -e "$ERRORS" | head -50 | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')
echo "{\"decision\": \"deny\", \"additionalContext\": $ESCAPED_OUTPUT}"
exit 2
fi
exit 0