Repository navigation
Expand file tree
/
Copy pathformat.sh
More file actions
executable file
·129 lines (103 loc) · 3.67 KB
/
Copy pathformat.sh
File metadata and controls
executable file
·129 lines (103 loc) · 3.67 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
#!/usr/bin/env bash
# YAPF Formatter, adapted from Ray and Skypilot.
USAGE="
Usage: format.sh [OPTIONS] [FILENAMES...]
If no filenames are given, format all files that differ from origin/main.
Options:
--all Format all files
--check Report errors but do not modify the files
Examples:
Format files that differ from origin/main ./format.sh
Format files a.txt and b/c.txt ./format.sh a.txt b/c.txt
Format all files in the repository ./format.sh --all
Only check the formatting (like the CI) ./format.sh --all --check"
# Cause the script to exit if a single command fails
set -eo pipefail
# This stops git rev-parse from failing if we run this from the .git directory
builtin cd "$(dirname "${BASH_SOURCE:-$0}")"
ROOT="$(git rev-parse --show-toplevel)"
builtin cd "$ROOT" || exit 1
#==============================================================================#
# Checking Formatter and Linter Versions
YAPF_VERSION=$(yapf --version | awk '{print $2}')
PYLINT_VERSION=$(pylint --version | head -n 1 | awk '{print $2}')
PYLINT_QUOTES_VERSION=$(pip list | grep pylint-quotes | awk '{print $2}')
MYPY_VERSION=$(mypy --version | awk '{print $2}')
# Params: tool name, tool version, required version
tool_version_check() {
if [[ $2 != $3 ]]; then
echo "Wrong $1 version installed: $3 is required, not $2."
exit 1
fi
}
tool_version_check "yapf" $YAPF_VERSION "0.32.0"
tool_version_check "pylint" $PYLINT_VERSION "2.8.2"
tool_version_check "pylint-quotes" $PYLINT_QUOTES_VERSION "0.2.3"
tool_version_check "mypy" "$MYPY_VERSION" "1.8.0"
#==============================================================================#
# Parse CLI arguments (based on https://stackoverflow.com/a/14203146)
while [[ $# -gt 0 ]]; do
case $1 in
--all)
ALL=true
shift
;;
--check)
CHECK=true
shift
;;
-*|--*)
echo "Error: unknown option $1"
echo "$USAGE"
exit 1
;;
*)
FILES=("${@:1}")
break
esac
done
if [ "$ALL" = true ] ; then
FILES=("api_server" "examples" "skyshift")
elif [ -z "${FILES}" ]; then
MERGEBASE="$(git merge-base origin/main HEAD)"
FILES="$(git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.py' '*.pyi')"
fi
#==============================================================================#
# Run Yapf
echo '[SkyShift] Running yapf'
if [ "$CHECK" = true ] ; then
ARGS=("--diff")
else
ARGS=("--in-place")
fi
printf "%s\n" "${FILES[@]}" | xargs -P 5 yapf "${ARGS[@]}" --recursive --parallel
#==============================================================================#
# Run Isort
echo '[SkyShift] Running isort'
if [ "$CHECK" = true ] ; then
ARGS=("--check-only" "--diff" "--color")
else
ARGS=()
fi
printf "%s\n" "${FILES[@]}" | xargs -P 5 isort "${ARGS[@]}"
#==============================================================================#
# Run mypy
echo '[SkyShift] Running mypy'
mypy --show-traceback api_server skyshift
#==============================================================================#
# Run Pylint
echo '[SkyShift] Running pylint'
printf "%s\n" "${FILES[@]}" | xargs -P 5 pylint \
--load-plugins pylint_quotes \
--disable invalid-string-quote,duplicate-code \
--max-line-length=120
#==============================================================================#
echo '[SkyShift] Completed all checks!'
if ! git diff --quiet &>/dev/null; then
echo
echo 'Reformatted files. Please review and stage the changes.'
echo
echo 'Changes not staged for commit:'
git --no-pager diff --name-only
exit 1
fi