forked from UFund-Me/Qbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyfmt.sh
executable file
·94 lines (80 loc) · 1.92 KB
/
pyfmt.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
#!/usr/bin/env bash
# Usage:
# pyfmt.sh <path/to/python/dir/or/files>
set -eu
TOP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
# shellcheck disable=SC1090,SC1091
source "${TOP_DIR}/scripts/qbot_base.sh"
SKIP_STRING_NORMALIZATION=false
function check_cmd_existence() {
for cmd in "$@"; do
if [[ -z "$(command -v "${cmd}")" ]]; then
error "Command '${cmd}' not found. Install it manually by running: "
error " sudo -H pip3 install --upgrade --no-cache-dir ${cmd}"
exit 1
fi
done
}
function find_plain_py_srcs() {
find "$@" -type f -name "*.py" -and ! -name "*_pb2.py" -and ! -name "*_pb2_grpc.py"
}
function black_run() {
if [[ "${SKIP_STRING_NORMALIZATION}" == true ]]; then
black --skip-string-normalization "$@"
else
black "$@"
fi
}
function pyfmt_run() {
isort "$@"
black_run "$@"
flake8 "$@" # --exit-zero removed to enforce
}
function run_pyfmt() {
for target in "$@"; do
if [ -f "${target}" ]; then
if plain_py_ext "${target}"; then
pyfmt_run "${target}"
ok "Done formatting ${target}"
else
warning "Nothing to do. ${target} is not a (regular) Python file."
fi
else
while read -r file_ent; do
pyfmt_run "${file_ent}"
done < <(find_plain_py_srcs "${target}")
ok "Done formatting Python files under ${target}"
fi
done
}
function usage() {
info "Usage: $0 [-S] <path/to/python/dirs/or/files>"
}
function main() {
check_cmd_existence "isort" "black" "flake8"
local dirs_or_files=()
while [[ $# -gt 0 ]]; do
local arg
arg="$1"
shift
case "${arg}" in
-S)
SKIP_STRING_NORMALIZATION=true
;;
-h | --help)
usage
exit 0
;;
-*)
error "Unknown option: ${arg}"
usage
exit 1
;;
*)
dirs_or_files+=("${arg}")
;;
esac
done
run_pyfmt "${dirs_or_files[@]}"
}
main "$@"