-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.sh
More file actions
executable file
·127 lines (116 loc) · 4.6 KB
/
format.sh
File metadata and controls
executable file
·127 lines (116 loc) · 4.6 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
#!/bin/bash
# LibSurgeon Code Formatter
# Formats all Python files using black, isort, and flake8
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Parse arguments
CHECK_ONLY=false
while [[ $# -gt 0 ]]; do
case $1 in
--check|-c)
CHECK_ONLY=true
shift
;;
--help|-h)
echo "Usage: $0 [--check]"
echo " --check, -c Check only, don't modify files"
echo " --help, -h Show this help"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Find Python files
FILES=$(find . -name "*.py" \
! -path "./.venv/*" \
! -path "./venv/*" \
! -path "./__pycache__/*" \
! -path "./.git/*" \
! -path "./build/*" \
! -path "./dist/*" \
! -path "./*.egg-info/*" \
| sort)
FILE_COUNT=$(echo "$FILES" | wc -l)
echo -e "${BLUE}Found $FILE_COUNT Python file(s)${NC}"
echo ""
ALL_PASSED=true
# isort
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE} Import Sorting (isort)${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if command -v isort &> /dev/null || python -m isort --version &> /dev/null; then
if $CHECK_ONLY; then
if python -m isort --check-only --profile black $FILES; then
echo -e "${GREEN}✓ isort: passed${NC}"
else
echo -e "${RED}✗ isort: issues found${NC}"
ALL_PASSED=false
fi
else
python -m isort --profile black $FILES
echo -e "${GREEN}✓ isort: completed${NC}"
fi
else
echo -e "${YELLOW} Warning: isort not installed. Run: pip install isort${NC}"
fi
echo ""
# black
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE} Code Formatting (black)${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if command -v black &> /dev/null || python -m black --version &> /dev/null; then
if $CHECK_ONLY; then
if python -m black --check $FILES; then
echo -e "${GREEN}✓ black: passed${NC}"
else
echo -e "${RED}✗ black: issues found${NC}"
ALL_PASSED=false
fi
else
python -m black $FILES
echo -e "${GREEN}✓ black: completed${NC}"
fi
else
echo -e "${YELLOW} Warning: black not installed. Run: pip install black${NC}"
fi
echo ""
# flake8 (check only)
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE} Linting (flake8)${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if command -v flake8 &> /dev/null || python -m flake8 --version &> /dev/null; then
if python -m flake8 --max-line-length 88 --extend-ignore E203,E501,W503 $FILES; then
echo -e "${GREEN}✓ flake8: no issues${NC}"
else
echo -e "${RED}✗ flake8: issues found${NC}"
ALL_PASSED=false
fi
else
echo -e "${YELLOW} Warning: flake8 not installed. Run: pip install flake8${NC}"
fi
echo ""
# Summary
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE} Summary${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
if $ALL_PASSED; then
if $CHECK_ONLY; then
echo -e "${GREEN}✓ All checks passed!${NC}"
else
echo -e "${GREEN}✓ Formatting complete!${NC}"
fi
exit 0
else
echo -e "${RED}✗ Some checks failed${NC}"
exit 1
fi