-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathpython-checks.sh
More file actions
executable file
·153 lines (128 loc) · 4 KB
/
python-checks.sh
File metadata and controls
executable file
·153 lines (128 loc) · 4 KB
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
#!/bin/bash
#
# python-checks.sh: Runs formatting and linting checks on Python files based on arguments.
#
set -e # Exit immediately if a command exits with a non-zero status.
# --- Function Definitions ---
show_help() {
echo "
Usage: ./python-checks.sh <FLAG> <PATH>
This script runs Python formatting (Black, iSort) and linting (Flake8) checks
on a specified relative path.
Arguments:
1. <FLAG> : The action to perform (--run-black, --run-isort, --run-lint, --run-all).
2. <PATH> : Relative path to the folder (Must start with agents/ or notebooks/).
Examples:
./python-checks.sh --run-black agents/academic-research
./python-checks.sh --run-isort agents/academic-research
./python-checks.sh --run-lint agents/academic-research
./python-checks.sh --run-all notebooks/evaluation
./python-checks.sh --help
"
exit 0
}
run_black() {
echo -e "\n--- Running Black Formatting Check ---"
local path="$1"
if [ -n "$path" ]; then
# Note: If you eventually use nbqa for notebooks, add that logic inside this if block
if [[ "$path" == notebooks/* ]]; then
black --check --diff "$path"
else
black --check --diff "$path"
fi
echo -e "Black check passed for: $path"
fi
}
run_isort() {
echo -e "\n--- Running iSort Import Check ---"
local path="$1"
if [ -n "$path" ]; then
if [[ "$path" == notebooks/* ]]; then
isort --check-only --diff "$path"
else
isort --check-only --diff "$path"
fi
echo -e "iSort check passed for: $path"
fi
}
run_lint() {
echo -e "\n--- Running Flake8 Linting Check ---"
local path="$1"
if [ -n "$path" ]; then
if [ -d "$path" ]; then
flake8 "$path"
echo -e "Flake8 check passed for: $path"
else
echo "Error: Directory '$path' not found."
exit 1
fi
fi
}
check_and_install_tools() {
REQUIRED_TOOLS="black flake8 isort nbqa"
TOOLS_MISSING=0
if ! command -v black &> /dev/null; then
TOOLS_MISSING=1
fi
if [ $TOOLS_MISSING -eq 1 ]; then
echo "Installing required Python tools ($REQUIRED_TOOLS)..."
python3 -m pip install $REQUIRED_TOOLS
fi
TOOLS_DIR=$(dirname "$(command -v black || echo "")")
if [ -n "$TOOLS_DIR" ] && [ -d "$TOOLS_DIR" ]; then
if [[ ":$PATH:" != *":$TOOLS_DIR:"* ]]; then
export PATH="$TOOLS_DIR:$PATH"
echo "Updated PATH to include Python tools at: $TOOLS_DIR"
fi
fi
}
# --- Argument Parsing & Validation ---
ACTION="$1"
TARGET_PATH="$2"
# 1. Handle Help
if [[ "$ACTION" == "--help" || "$ACTION" == "-h" ]]; then
show_help
fi
# 2. Validate Action Flag
if [[ -z "$ACTION" ]]; then
echo "Error: Missing argument. Use './python-checks.sh --help' for usage."
exit 1
fi
if [[ "$ACTION" != "--run-all" && "$ACTION" != "--run-black" && "$ACTION" != "--run-isort" && "$ACTION" != "--run-lint" ]]; then
echo "Error: Unknown flag '$ACTION'. Use --run-all, --run-black, --run-isort, or --run-lint."
exit 1
fi
# 3. Validate Path Structure (Must start with agents/ or notebooks/)
if [[ -z "$TARGET_PATH" ]]; then
echo "Error: Missing path argument for $ACTION."
exit 1
fi
if ! [[ "$TARGET_PATH" =~ ^(agents/|notebooks/).* ]]; then
echo "Error: Path '$TARGET_PATH' must start with 'agents/' or 'notebooks/'."
exit 1
fi
if [ ! -d "$TARGET_PATH" ]; then
echo "Error: Directory '$TARGET_PATH' does not exist."
exit 1
fi
# --- Main Execution ---
echo "Starting local Python code quality checks..."
check_and_install_tools
case "$ACTION" in
--run-all)
run_black "$TARGET_PATH"
run_isort "$TARGET_PATH"
run_lint "$TARGET_PATH"
;;
--run-black)
run_black "$TARGET_PATH"
;;
--run-isort)
run_isort "$TARGET_PATH"
;;
--run-lint)
run_lint "$TARGET_PATH"
;;
esac
echo -e "\n✅ Requested action ($ACTION) completed successfully for $TARGET_PATH!"