-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
okrutnik.sh
executable file
·267 lines (226 loc) · 6.75 KB
/
okrutnik.sh
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env bash
# Okrutnik: a bash script that helps you write correct Python code
# Copyright (C) 2024 c0m4r
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# --------------------------------------------
# Vars
# --------------------------------------------
# Version
VERSION=2.1.8
# Toolset
TOOLSET="bandit black codespell mypy pylint pyright pylama ruff safety"
# Base dir
BASEDIR=$(dirname "$0")
# Okrutnik venv location
VENV="${BASEDIR}/.okrutnik_venv"
# Args
ARG1=$1
ARG2=$2
ARG3=$3
# Colors
ORANGE='\e[1;33m'
GREEN='\e[1;32m'
CYAN='\e[1;36m'
RED='\e[1;31m'
ENDCOLOR="\e[0m"
# Defaults
TOOLS_NUM=8
ITERATION=1
PASSED=0
FAILED=0
# --------------------------------------------
# Functions
# --------------------------------------------
# Print something
print() {
echo -e "${ORANGE}${1}${ENDCOLOR}"
}
# Pass or fail check
pass() {
if [[ $? -eq 0 ]]; then
echo -e "-> ${GREEN}pass${ENDCOLOR}"
PASSED=$(( $PASSED + 1 ))
else
echo -e "-> ${RED}your code sucks${ENDCOLOR}"
FAILED=$(( $FAILED + 1))
fi
ITERATION=$(( $ITERATION + 1 ))
}
# Install tools
okrutnik_install() {
okrutnik_which_python
echo -e "${CYAN}Connecting to an intergalactic relay${ENDCOLOR} 📡"
$PYTHON_BIN -m venv ${VENV}
PATH="${VENV}/bin:${PATH}"
source ${VENV}/bin/activate
okrutnik_which_pip
${PIP} install --upgrade pip setuptools wheel
${PIP} install --upgrade ${TOOLSET}
if [[ "$ARG2" == "-r" ]] && [[ -e $ARG3 ]]; then
print "$ARG3"
${PIP} install -r $ARG3
fi
custom_crossplane_hack
echo -e "${CYAN}Receiving communication...${ENDCOLOR}"
echo -e "-> ${GREEN}All your base are belong to us${ENDCOLOR} 👽"
}
okrutnik_uninstall() {
if [[ "${VENV}" ]] && [[ -d ${VENV} ]]; then
echo -n "${VENV} will be removed, "
read -r -p "are you sure? (y/N): " response \
&& [[ $response == [yY] ]] \
|| exit 1
rm -rv "${VENV}"
else
echo "Nothing to do. Already uninstalled."
fi
}
# Pip finder function
okrutnik_which_pip() {
if [[ -e ${VENV}/bin/pip3 ]]; then
PIP="pip3"
else
PIP="pip"
fi
}
# Python finder function
okrutnik_which_python() {
if command -v python3 &>/dev/null ; then
PYTHON_BIN="python3"
elif command -v python &>/dev/null ; then
PYTHON_BIN="python"
elif which python3 &>/dev/null ; then
PYTHON_BIN="python3"
elif which python &>/dev/null ; then
PYTHON_BIN="python"
else
echo "Python not found"
exit 1
fi
}
# Custom crossplane module hack
custom_crossplane_hack() {
crossplane_path=$(dirname ${VENV}/lib/python*/site-packages/crossplane)
if [[ -d $crossplane_path ]]; then
echo "Crossplane detected: committing a hack"
touch ${crossplane_path}/site-packages/crossplane/py.typed
sed -i 's/parse(filename,/parse(filename: str,/g;' ${crossplane_path}/site-packages/crossplane/parser.py
fi
}
# --------------------------------------------
# Validation / args handling
# --------------------------------------------
echo -e "${ORANGE}Okrutnik v${VERSION} by c0m4r${ENDCOLOR}"
# Check args
if [[ "$ARG1" == "-h" ]] || [[ "$ARG1" == "--help" ]]; then
echo "Usage: ./okrutnik.sh [options] <target>"
echo ""
echo "Before <target>:"
echo " -s, --stop Exit on failed linters or errors"
echo ""
echo "Standalone:"
echo " -h, --help Print this help message"
echo " --update Update installed tools"
echo " --install [-r requirements.txt] Install tools [and target requirements]"
echo " --uninstall Remove installed tools"
echo " --safety Run safety check"
exit 0
elif [[ "$ARG1" == "--update" ]]; then
okrutnik_install
exit 0
elif [[ "$ARG1" == "--install" ]]; then
okrutnik_install
exit 0
elif [[ "$ARG1" == "--uninstall" ]]; then
okrutnik_uninstall
exit 0
elif [[ ! "$ARG1" ]]; then
echo "Missing <target>"
exit 1
else
TARGET=$*
fi
# Check venv
if [[ -e ${VENV}/pyvenv.cfg ]]; then
PATH="${VENV}/bin:${PATH}"
else
okrutnik_install
fi
# --------------------------------------------
# Safety check
# --------------------------------------------
if [[ "$ARG1" == "--safety" ]]; then
safety check
exit $?
fi
# --------------------------------------------
# Linters
# --------------------------------------------
# Print Python version
okrutnik_which_python
${PYTHON_BIN} -V
# Switch exit on failed
if [[ "$ARG1" == "--stop" ]]; then
TARGET=$(echo $@ | sed -e 's/\-\-stop\ //g;')
set -e
fi
# Print targets
echo "Target(s): ${TARGET}"
# Ruff
print "ruff (${ITERATION}/${TOOLS_NUM})"
ruff check $TARGET ; pass
# codespell
print "codespell (${ITERATION}/${TOOLS_NUM})"
codespell -L paranoya --skip "./*venv*" --skip "./lib/python*" $TARGET ; pass
# pylama
print "pylama (${ITERATION}/${TOOLS_NUM})"
pylama $TARGET ; pass
# mypy
print "mypy (${ITERATION}/${TOOLS_NUM})"
mypy --install-types --non-interactive --strict $TARGET ; pass
# pylint
print "pylint (${ITERATION}/${TOOLS_NUM})"
pylint $TARGET ; pass
# pyright
print "pyright (${ITERATION}/${TOOLS_NUM})"
pyright $TARGET ; pass
# bandit
print "bandit (${ITERATION}/${TOOLS_NUM})"
bandit --quiet -r $TARGET ; pass
# --------------------------------------------
# Formatter
# --------------------------------------------
# black
print "black (${ITERATION}/${TOOLS_NUM})"
black --diff --color $TARGET
black -q $TARGET ; pass
# --------------------------------------------
# Summary
# --------------------------------------------
PERCENT=$(echo $PASSED $TOOLS_NUM \
| awk '{ quotient = $1 / $2 * 100 ; print int(quotient) }')
print "summary"
echo -e "-> ${PASSED}/${TOOLS_NUM} (${PERCENT}%)"
if [[ $PERCENT -lt 30 ]]; then
echo -e "-> ${RED}do you even code?${ENDCOLOR} 😂"
elif [[ $PERCENT -lt 50 ]]; then
echo -e "-> ${RED}damn, your code really sucks${ENDCOLOR} 🙄"
elif [[ $PERCENT -lt 70 ]]; then
echo -e "-> ${RED}still sucks...${ENDCOLOR} 🙃"
elif [[ $PERCENT -lt 100 ]]; then
echo -e "-> ${ORANGE}almost there${ENDCOLOR} 🤔"
else
echo -e "-> ${GREEN}too easy${ENDCOLOR} 😎"
fi