2018-04-11 10:11:35 -07:00
|
|
|
#!/usr/bin/env bash
|
2018-10-18 21:56:22 -07:00
|
|
|
# YAPF + Clang formatter (if installed). This script formats all changed files from the last mergebase.
|
|
|
|
# You are encouraged to run this locally before pushing changes for review.
|
2018-04-11 10:11:35 -07:00
|
|
|
|
|
|
|
# Cause the script to exit if a single command fails
|
2020-07-30 16:39:28 -07:00
|
|
|
set -euo pipefail
|
2018-04-11 10:11:35 -07:00
|
|
|
|
2020-01-31 10:42:09 -08:00
|
|
|
FLAKE8_VERSION_REQUIRED="3.7.7"
|
|
|
|
YAPF_VERSION_REQUIRED="0.23.0"
|
2020-07-30 16:39:28 -07:00
|
|
|
SHELLCHECK_VERSION_REQUIRED="0.7.1"
|
2020-01-31 10:42:09 -08:00
|
|
|
|
|
|
|
check_command_exist() {
|
|
|
|
VERSION=""
|
|
|
|
case "$1" in
|
|
|
|
yapf)
|
|
|
|
VERSION=$YAPF_VERSION_REQUIRED
|
|
|
|
;;
|
|
|
|
flake8)
|
|
|
|
VERSION=$FLAKE8_VERSION_REQUIRED
|
|
|
|
;;
|
2020-07-30 16:39:28 -07:00
|
|
|
shellcheck)
|
|
|
|
VERSION=$SHELLCHECK_VERSION_REQUIRED
|
|
|
|
;;
|
2020-01-31 10:42:09 -08:00
|
|
|
*)
|
|
|
|
echo "$1 is not a required dependency"
|
|
|
|
exit 1
|
|
|
|
esac
|
2020-07-21 19:56:41 -07:00
|
|
|
if ! [ -x "$(command -v "$1")" ]; then
|
2020-01-31 10:42:09 -08:00
|
|
|
echo "$1 not installed. pip install $1==$VERSION"
|
|
|
|
exit 1
|
2020-07-29 21:15:09 +02:00
|
|
|
fi
|
2020-01-31 10:42:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
check_command_exist yapf
|
|
|
|
check_command_exist flake8
|
|
|
|
|
2019-07-03 15:59:47 -07:00
|
|
|
ver=$(yapf --version)
|
2020-07-21 19:56:41 -07:00
|
|
|
if ! echo "$ver" | grep -q 0.23.0; then
|
2020-01-31 10:42:09 -08:00
|
|
|
echo "Wrong YAPF version installed: 0.23.0 is required, not $ver. $YAPF_DOWNLOAD_COMMAND_MSG"
|
2019-07-03 15:59:47 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-05-19 16:07:28 -07:00
|
|
|
# this stops git rev-parse from failing if we run this from the .git directory
|
|
|
|
builtin cd "$(dirname "${BASH_SOURCE:-$0}")"
|
2018-04-11 10:11:35 -07:00
|
|
|
|
2018-05-19 16:07:28 -07:00
|
|
|
ROOT="$(git rev-parse --show-toplevel)"
|
2018-06-05 20:22:11 -07:00
|
|
|
builtin cd "$ROOT" || exit 1
|
|
|
|
|
2019-08-06 17:30:33 -07:00
|
|
|
# Add the upstream remote if it doesn't exist
|
|
|
|
if ! git remote -v | grep -q upstream; then
|
2018-06-05 20:22:11 -07:00
|
|
|
git remote add 'upstream' 'https://github.com/ray-project/ray.git'
|
|
|
|
fi
|
|
|
|
|
2019-07-04 23:22:01 -07:00
|
|
|
FLAKE8_VERSION=$(flake8 --version | awk '{print $1}')
|
|
|
|
YAPF_VERSION=$(yapf --version | awk '{print $2}')
|
2020-07-30 16:39:28 -07:00
|
|
|
SHELLCHECK_VERSION=$(shellcheck --version | awk '/^version:/ {print $2}')
|
2019-07-04 23:22:01 -07:00
|
|
|
|
2019-07-08 17:15:09 +08:00
|
|
|
# params: tool name, tool version, required version
|
|
|
|
tool_version_check() {
|
2020-07-21 19:56:41 -07:00
|
|
|
if [ "$2" != "$3" ]; then
|
2019-07-08 17:15:09 +08:00
|
|
|
echo "WARNING: Ray uses $1 $3, You currently are using $2. This might generate different results."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:24:19 -07:00
|
|
|
tool_version_check "flake8" "$FLAKE8_VERSION" "$FLAKE8_VERSION_REQUIRED"
|
|
|
|
tool_version_check "yapf" "$YAPF_VERSION" "$YAPF_VERSION_REQUIRED"
|
2020-07-30 16:39:28 -07:00
|
|
|
tool_version_check "shellcheck" "$SHELLCHECK_VERSION" "$SHELLCHECK_VERSION_REQUIRED"
|
2019-07-04 23:22:01 -07:00
|
|
|
|
|
|
|
if which clang-format >/dev/null; then
|
|
|
|
CLANG_FORMAT_VERSION=$(clang-format --version | awk '{print $3}')
|
2020-07-21 19:56:41 -07:00
|
|
|
tool_version_check "clang-format" "$CLANG_FORMAT_VERSION" "7.0.0"
|
2019-07-04 23:22:01 -07:00
|
|
|
else
|
2019-07-08 17:15:09 +08:00
|
|
|
echo "WARNING: clang-format is not installed!"
|
2019-07-04 23:22:01 -07:00
|
|
|
fi
|
|
|
|
|
2018-06-05 20:22:11 -07:00
|
|
|
# Only fetch master since that's the branch we're diffing against.
|
2019-07-03 15:59:47 -07:00
|
|
|
git fetch upstream master || true
|
2018-06-05 20:22:11 -07:00
|
|
|
|
2020-07-30 16:39:28 -07:00
|
|
|
SHELLCHECK_FLAGS=(
|
|
|
|
--exclude=1090 # "Can't follow non-constant source. Use a directive to specify location."
|
|
|
|
--exclude=1091 # "Not following {file} due to some error"
|
|
|
|
--exclude=2207 # "Prefer mapfile or read -a to split command output (or quote to avoid splitting)." -- these aren't compatible with macOS's old Bash
|
|
|
|
)
|
|
|
|
|
|
|
|
SHELLCHECK_BAZEL_FLAGS=(
|
|
|
|
--exclude=2043 # "This loop will only ever run once. Bad quoting or missing glob/expansion?" -- Bazel preprocessing can trigger this needlessly
|
|
|
|
)
|
|
|
|
|
2018-06-05 20:22:11 -07:00
|
|
|
YAPF_FLAGS=(
|
|
|
|
'--style' "$ROOT/.style.yapf"
|
|
|
|
'--recursive'
|
|
|
|
'--parallel'
|
|
|
|
)
|
|
|
|
|
|
|
|
YAPF_EXCLUDES=(
|
2018-06-08 02:25:55 -07:00
|
|
|
'--exclude' 'python/ray/cloudpickle/*'
|
|
|
|
'--exclude' 'python/build/*'
|
|
|
|
'--exclude' 'python/ray/core/src/ray/gcs/*'
|
2020-02-05 14:16:58 -08:00
|
|
|
'--exclude' 'python/ray/thirdparty_files/*'
|
2018-06-05 20:22:11 -07:00
|
|
|
)
|
|
|
|
|
2020-08-06 10:44:37 +02:00
|
|
|
FLAKE8_EXCLUDES="python/ray/core/generated/,streaming/python/generated,doc/source/conf.py,python/ray/cloudpickle/,python/ray/thirdparty_files/"
|
|
|
|
|
2020-07-30 16:39:28 -07:00
|
|
|
shellcheck_scripts() {
|
|
|
|
shellcheck "${SHELLCHECK_FLAGS[@]}" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
shellcheck_bazel() {
|
|
|
|
"${ROOT}"/ci/travis/bazel.py shellcheck "mnemonic(\"Genrule\", deps(//:*))" shellcheck "${SHELLCHECK_FLAGS[@]}" "${SHELLCHECK_BAZEL_FLAGS[@]}" "$@"
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:22:11 -07:00
|
|
|
# Format specified files
|
|
|
|
format() {
|
2020-07-30 16:39:28 -07:00
|
|
|
local shell_files=() python_files=() bazel_files=()
|
|
|
|
|
|
|
|
local name
|
|
|
|
for name in "$@"; do
|
|
|
|
local base="${name%.*}"
|
|
|
|
local suffix="${name#${base}}"
|
|
|
|
|
|
|
|
local shebang=""
|
|
|
|
read -r shebang < "${name}" || true
|
|
|
|
case "${shebang}" in
|
|
|
|
'#!'*)
|
|
|
|
shebang="${shebang#/usr/bin/env }"
|
|
|
|
shebang="${shebang%% *}"
|
|
|
|
shebang="${shebang##*/}"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ "${base}" = "WORKSPACE" ] || [ "${base}" = "BUILD" ] || [ "${suffix}" = ".BUILD" ] || [ "${suffix}" = ".bazel" ] || [ "${suffix}" = ".bzl" ]; then
|
|
|
|
bazel_files+=("${name}")
|
|
|
|
elif [ -z "${suffix}" ] && [ "${shebang}" != "${shebang#python}" ] || [ "${suffix}" != "${suffix#.py}" ]; then
|
|
|
|
python_files+=("${name}")
|
|
|
|
elif [ -z "${suffix}" ] && [ "${shebang}" != "${shebang%sh}" ] || [ "${suffix}" != "${suffix#.sh}" ]; then
|
|
|
|
shell_files+=("${name}")
|
|
|
|
else
|
|
|
|
echo "error: failed to determine file type: ${name}" 1>&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ 0 -lt "${#python_files[@]}" ]; then
|
|
|
|
yapf --in-place "${YAPF_FLAGS[@]}" -- "${python_files[@]}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if shellcheck --shell=sh --format=diff - < /dev/null; then
|
|
|
|
if [ 0 -lt "${#bazel_files[@]}" ]; then
|
|
|
|
if ! shellcheck_bazel; then
|
|
|
|
echo "Bazel genrule() scripts cannot be fixed automatically; please fix manually." 1>&2
|
|
|
|
shellcheck_bazel --format=diff
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ 0 -lt "${#shell_files[@]}" ]; then
|
|
|
|
local difference
|
|
|
|
difference="$(shellcheck_scripts --format=diff "${shell_files[@]}" || true && printf "-")"
|
|
|
|
difference="${difference%-}"
|
|
|
|
printf "%s" "${difference}" | patch -p1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "error: this version of shellcheck does not support diffs"
|
|
|
|
fi
|
2018-06-05 20:22:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Format files that differ from main branch. Ignores dirs that are not slated
|
|
|
|
# for autoformat yet.
|
|
|
|
format_changed() {
|
|
|
|
# The `if` guard ensures that the list of filenames is not empty, which
|
|
|
|
# could cause yapf to receive 0 positional arguments, making it hang
|
|
|
|
# waiting for STDIN.
|
|
|
|
#
|
2019-11-12 11:45:28 -08:00
|
|
|
# `diff-filter=ACRM` and $MERGEBASE is to ensure we only format files that
|
2018-06-05 20:22:11 -07:00
|
|
|
# exist on both branches.
|
|
|
|
MERGEBASE="$(git merge-base upstream/master HEAD)"
|
|
|
|
|
2019-11-12 11:45:28 -08:00
|
|
|
if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.py' &>/dev/null; then
|
|
|
|
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
|
2018-08-24 15:01:12 -07:00
|
|
|
yapf --in-place "${YAPF_EXCLUDES[@]}" "${YAPF_FLAGS[@]}"
|
2018-11-06 14:59:22 -08:00
|
|
|
if which flake8 >/dev/null; then
|
2019-11-12 11:45:28 -08:00
|
|
|
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
|
2020-08-06 10:44:37 +02:00
|
|
|
flake8 --inline-quotes '"' --no-avoid-escape --exclude="$FLAKE8_EXCLUDES,rllib/" --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605
|
|
|
|
# Ignore F821 for rllib flake8 checking (produces errors for type annotations using quotes (non-imported classes)).
|
|
|
|
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
|
|
|
|
flake8 --inline-quotes '"' --no-avoid-escape --exclude="$FLAKE8_EXCLUDES" --filename="rllib/" --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605,F821
|
2019-02-22 17:33:08 -08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-11-12 11:45:28 -08:00
|
|
|
if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.pyx' '*.pxd' '*.pxi' &>/dev/null; then
|
2019-02-22 17:33:08 -08:00
|
|
|
if which flake8 >/dev/null; then
|
2019-11-12 11:45:28 -08:00
|
|
|
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.pyx' '*.pxd' '*.pxi' | xargs -P 5 \
|
2020-08-06 10:44:37 +02:00
|
|
|
flake8 --inline-quotes '"' --no-avoid-escape --exclude="$FLAKE8_EXCLUDES" --ignore=C408,E121,E123,E126,E211,E225,E226,E227,E24,E704,E999,W503,W504,W605
|
2018-11-06 14:59:22 -08:00
|
|
|
fi
|
2018-06-05 20:22:11 -07:00
|
|
|
fi
|
2018-10-18 21:56:22 -07:00
|
|
|
|
|
|
|
if which clang-format >/dev/null; then
|
2019-11-12 11:45:28 -08:00
|
|
|
if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.cc' '*.h' &>/dev/null; then
|
|
|
|
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.cc' '*.h' | xargs -P 5 \
|
2018-10-18 21:56:22 -07:00
|
|
|
clang-format -i
|
|
|
|
fi
|
|
|
|
fi
|
2020-07-30 16:39:28 -07:00
|
|
|
|
|
|
|
if command -v shellcheck >/dev/null; then
|
|
|
|
if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- 'WORKSPACE' 'WORKSPACE.*' 'BUILD.*' '*.bzl' '*.bazel' &>/dev/null; then
|
|
|
|
shellcheck_bazel
|
|
|
|
fi
|
|
|
|
|
2020-07-31 09:25:55 -07:00
|
|
|
local shell_files non_shell_files
|
|
|
|
non_shell_files=($(git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- ':(exclude)*.sh'))
|
|
|
|
shell_files=($(git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.sh'))
|
|
|
|
if [ 0 -lt "${#non_shell_files[@]}" ]; then
|
2020-08-02 00:59:05 -07:00
|
|
|
shell_files+=($(git --no-pager grep -l -- '^#!\(/usr\)\?/bin/\(env \+\)\?\(ba\)\?sh' "${non_shell_files[@]}" || true))
|
2020-07-31 09:25:55 -07:00
|
|
|
fi
|
2020-07-30 16:39:28 -07:00
|
|
|
if [ 0 -lt "${#shell_files[@]}" ]; then
|
|
|
|
shellcheck_scripts "${shell_files[@]}"
|
|
|
|
fi
|
|
|
|
fi
|
2018-06-05 20:22:11 -07:00
|
|
|
}
|
|
|
|
|
2018-06-08 02:25:55 -07:00
|
|
|
# Format all files, and print the diff to stdout for travis.
|
2018-06-05 20:22:11 -07:00
|
|
|
format_all() {
|
2020-08-06 10:44:37 +02:00
|
|
|
# Ignore F821 for rllib flake8 checking (produces errors for type annotations using quotes (non-imported classes)).
|
|
|
|
flake8 --inline-quotes '"' --no-avoid-escape --exclude="$FLAKE8_EXCLUDES,rllib/" --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605
|
|
|
|
flake8 --inline-quotes '"' --no-avoid-escape --exclude="$FLAKE8_EXCLUDES" --filename="rllib/" --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605,F821
|
2020-07-30 16:39:28 -07:00
|
|
|
|
2018-06-08 02:25:55 -07:00
|
|
|
yapf --diff "${YAPF_FLAGS[@]}" "${YAPF_EXCLUDES[@]}" test python
|
2020-07-30 16:39:28 -07:00
|
|
|
|
|
|
|
local shell_files
|
|
|
|
# shellcheck disable=SC2207
|
|
|
|
shell_files=($(
|
|
|
|
git -C "${ROOT}" ls-files --exclude-standard HEAD -- "*.sh" &&
|
2020-08-02 00:59:05 -07:00
|
|
|
{ git -C "${ROOT}" --no-pager grep -l '^#!\(/usr\)\?/bin/\(env \+\)\?\(ba\)\?sh' ":(exclude)*.sh" || true; }
|
2020-07-30 16:39:28 -07:00
|
|
|
))
|
|
|
|
if [ 0 -lt "${#shell_files[@]}" ]; then
|
|
|
|
shellcheck_scripts "${shell_files[@]}"
|
|
|
|
fi
|
|
|
|
shellcheck_bazel
|
2018-06-05 20:22:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# This flag formats individual files. --files *must* be the first command line
|
|
|
|
# arg to use this option.
|
2020-07-30 16:39:28 -07:00
|
|
|
if [ "${1-}" == '--files' ]; then
|
2018-06-05 20:22:11 -07:00
|
|
|
format "${@:2}"
|
|
|
|
# If `--all` is passed, then any further arguments are ignored and the
|
|
|
|
# entire python directory is formatted.
|
2020-07-30 16:39:28 -07:00
|
|
|
elif [ "${1-}" == '--all' ]; then
|
2018-06-05 20:22:11 -07:00
|
|
|
format_all
|
|
|
|
else
|
|
|
|
# Format only the files that changed in last commit.
|
|
|
|
format_changed
|
|
|
|
fi
|
|
|
|
|
2020-02-05 14:16:58 -08:00
|
|
|
# Ensure import ordering
|
|
|
|
# Make sure that for every import psutil; import setpproctitle
|
|
|
|
# There's a import ray above it.
|
2020-03-15 03:27:41 +08:00
|
|
|
|
|
|
|
PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE:-python}
|
|
|
|
|
|
|
|
$PYTHON_EXECUTABLE ci/travis/check_import_order.py . -s ci -s python/ray/pyarrow_files -s python/ray/thirdparty_files -s python/build -s lib
|
2020-02-05 14:16:58 -08:00
|
|
|
|
2018-06-05 20:22:11 -07:00
|
|
|
if ! git diff --quiet &>/dev/null; then
|
|
|
|
echo 'Reformatted changed files. Please review and stage the changes.'
|
2018-05-19 16:07:28 -07:00
|
|
|
echo 'Files updated:'
|
|
|
|
echo
|
|
|
|
|
|
|
|
git --no-pager diff --name-only
|
|
|
|
|
|
|
|
exit 1
|
2018-04-11 10:11:35 -07:00
|
|
|
fi
|