2022-03-21 11:14:41 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# We check only our code, that's why we skip contrib
|
|
|
|
GIT_ROOT=$(git rev-parse --show-cdup)
|
2023-03-20 17:30:20 +00:00
|
|
|
GIT_ROOT=${GIT_ROOT:-./}
|
2022-03-21 11:14:41 +00:00
|
|
|
tmp=$(mktemp)
|
2023-03-20 17:30:20 +00:00
|
|
|
|
|
|
|
# Find all *.py, *.python files and executable files without extension
|
|
|
|
# that are determined as python scripts by 'file' util
|
|
|
|
# in the repo except the contrib directory.
|
|
|
|
find_cmd=(
|
|
|
|
find "$GIT_ROOT" -type f -not -path "${GIT_ROOT}contrib/*"
|
|
|
|
\(
|
2023-09-23 19:13:13 +00:00
|
|
|
\(
|
|
|
|
-name '*.py' -or -name "*.python" -or
|
|
|
|
\(
|
|
|
|
-executable -not -name "*.*" -exec sh -c 'file {} | grep -q "Python script"' \;
|
|
|
|
\)
|
|
|
|
\)
|
|
|
|
# We skip modules generated by the protocol buffer compiler from *.proto files.
|
|
|
|
-and -not -name '*_pb2.py' -and -not -name '*_pb2_grpc.py'
|
2023-03-20 17:30:20 +00:00
|
|
|
\)
|
|
|
|
)
|
|
|
|
|
2022-07-19 13:17:05 +00:00
|
|
|
if ! "${find_cmd[@]}" -exec black --check --diff {} + 1>"$tmp" 2>&1; then
|
2022-03-21 11:14:41 +00:00
|
|
|
# Show the result only if some files need formatting
|
|
|
|
cat "$tmp"
|
2022-07-19 13:17:05 +00:00
|
|
|
# Apply formatting
|
|
|
|
"${find_cmd[@]}" -exec black {} + 1>/dev/null 2>&1
|
|
|
|
# Automatically add changed files to stage
|
|
|
|
"${find_cmd[@]}" -exec git add -u {} + 1>/dev/null 2>&1
|
2022-03-21 11:14:41 +00:00
|
|
|
fi
|
2023-03-20 17:30:20 +00:00
|
|
|
|
2022-03-21 11:14:41 +00:00
|
|
|
rm "$tmp"
|