-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat-python-all.sh
More file actions
executable file
·89 lines (77 loc) · 2.18 KB
/
format-python-all.sh
File metadata and controls
executable file
·89 lines (77 loc) · 2.18 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
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC1091
set -e
set -u
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/../common/log.sh"
. "$DIR/../common/parallel.sh"
. "$DIR/../common/yapf-format.sh"
. "$DIR/../common/black-format.sh"
. "$DIR/../common/isort-format.sh"
dryRun="true"
dir=""
excludeRegex=""
regex=$(getGeneralPythonFileFormatRegex)
function help() {
printError "Usage:" \
" [--force] : Force the format." \
" [--exclude-regex <regex> ] : Exclude file with this regex." \
" [--regex-pattern <pattern>] : Regex pattern to include files." \
" --dir <path> : In which directory to format files."
}
function parseArgs() {
local prev=""
for p in "$@"; do
if [ "$p" = "--force" ]; then
dryRun="false"
elif [ "$p" = "--help" ]; then
help
return 1
elif [ "$p" = "--dir" ]; then
true
elif [ "$prev" = "--dir" ]; then
dir="$p"
elif [ "$p" = "--exclude" ]; then
true
elif [ "$prev" = "--exclude" ]; then
excludeRegex="$p"
elif [ "$p" = "--include" ]; then
true
elif [ "$prev" = "--include" ]; then
regex="$p"
else
printError "! Unknown argument \`$p\`"
help
return 1
fi
prev="$p"
done
}
parseArgs "$@"
if [ "$dryRun" = "true" ]; then
printInfo "Dry-run formatting python files in dir '$dir'."
else
printInfo "Formatting python files in dir '$dir'."
fi
if [ "${GITHOOKS_PYTHON_FORMAT_USE_YAPF:-}" = "1" ]; then
tool="yapf"
func="formatPythonFileYapf"
assertYapfVersion "0.31.0" "1.0.0" "yapf"
else
tool="black"
func="formatPythonFileBlack"
assertBlackVersion "22.1.0" "24.0.0" "black"
fi
parallelForDir "$func" \
"$dir" \
"$regex" \
"$excludeRegex" \
"$dryRun" \
"$tool" || die "Python format failed."
assertISortVersion "5.12.0" "6.0.0" "isort"
parallelForDir "formatIncludesPythonFileISort" \
"$dir" \
"$regex" \
"$excludeRegex" \
"$dryRun" \
"isort" || die "Python format includes failed."