format.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #!/usr/bin/env bash
  2. # Black + Clang formatter (if installed). This script formats all changed files from the last mergebase.
  3. # You are encouraged to run this locally before pushing changes for review.
  4. # Cause the script to exit if a single command fails
  5. set -euo pipefail
  6. FLAKE8_VERSION_REQUIRED="3.9.1"
  7. BLACK_VERSION_REQUIRED="22.10.0"
  8. SHELLCHECK_VERSION_REQUIRED="0.7.1"
  9. MYPY_VERSION_REQUIRED="0.782"
  10. ISORT_VERSION_REQUIRED="5.10.1"
  11. check_python_command_exist() {
  12. VERSION=""
  13. case "$1" in
  14. black)
  15. VERSION=$BLACK_VERSION_REQUIRED
  16. ;;
  17. flake8)
  18. VERSION=$FLAKE8_VERSION_REQUIRED
  19. ;;
  20. mypy)
  21. VERSION=$MYPY_VERSION_REQUIRED
  22. ;;
  23. isort)
  24. VERSION=$ISORT_VERSION_REQUIRED
  25. ;;
  26. *)
  27. echo "$1 is not a required dependency"
  28. exit 1
  29. esac
  30. if ! [ -x "$(command -v "$1")" ]; then
  31. echo "$1 not installed. Install the python package with: pip install $1==$VERSION"
  32. exit 1
  33. fi
  34. }
  35. check_docstyle() {
  36. echo "Checking docstyle..."
  37. violations=$(git ls-files | grep '.py$' | xargs grep -E '^[ ]+[a-z_]+ ?\([a-zA-Z]+\): ' | grep -v 'str(' | grep -v noqa || true)
  38. if [[ -n "$violations" ]]; then
  39. echo
  40. echo "=== Found Ray docstyle violations ==="
  41. echo "$violations"
  42. echo
  43. echo "Per the Google pydoc style, omit types from pydoc args as they are redundant: https://docs.ray.io/en/latest/ray-contribute/getting-involved.html#code-style "
  44. echo "If this is a false positive, you can add a '# noqa' comment to the line to ignore."
  45. exit 1
  46. fi
  47. return 0
  48. }
  49. check_python_command_exist black
  50. check_python_command_exist flake8
  51. check_python_command_exist mypy
  52. check_python_command_exist isort
  53. # this stops git rev-parse from failing if we run this from the .git directory
  54. builtin cd "$(dirname "${BASH_SOURCE:-$0}")"
  55. ROOT="$(git rev-parse --show-toplevel)"
  56. builtin cd "$ROOT" || exit 1
  57. # NOTE(edoakes): black version differs based on installation method:
  58. # Option 1) 'black, 21.12b0 (compiled: no)'
  59. # Option 2) 'black, version 21.12b0'
  60. # For newer versions (at least 22.10.0), a second line is printed which must be dropped:
  61. #
  62. # black, 22.10.0 (compiled: yes)
  63. # Python (CPython) 3.9.13
  64. BLACK_VERSION_STR=$(black --version)
  65. if [[ "$BLACK_VERSION_STR" == *"compiled"* ]]
  66. then
  67. BLACK_VERSION=$(echo "$BLACK_VERSION_STR" | head -n 1 | awk '{print $2}')
  68. else
  69. BLACK_VERSION=$(echo "$BLACK_VERSION_STR" | head -n 1 | awk '{print $3}')
  70. fi
  71. FLAKE8_VERSION=$(flake8 --version | head -n 1 | awk '{print $1}')
  72. MYPY_VERSION=$(mypy --version | awk '{print $2}')
  73. ISORT_VERSION=$(isort --version | grep VERSION | awk '{print $2}')
  74. GOOGLE_JAVA_FORMAT_JAR=/tmp/google-java-format-1.7-all-deps.jar
  75. # params: tool name, tool version, required version
  76. tool_version_check() {
  77. if [ "$2" != "$3" ]; then
  78. echo "WARNING: Ray uses $1 $3, You currently are using $2. This might generate different results."
  79. fi
  80. }
  81. tool_version_check "flake8" "$FLAKE8_VERSION" "$FLAKE8_VERSION_REQUIRED"
  82. tool_version_check "black" "$BLACK_VERSION" "$BLACK_VERSION_REQUIRED"
  83. tool_version_check "mypy" "$MYPY_VERSION" "$MYPY_VERSION_REQUIRED"
  84. tool_version_check "isort" "$ISORT_VERSION" "$ISORT_VERSION_REQUIRED"
  85. if command -v shellcheck >/dev/null; then
  86. SHELLCHECK_VERSION=$(shellcheck --version | awk '/^version:/ {print $2}')
  87. tool_version_check "shellcheck" "$SHELLCHECK_VERSION" "$SHELLCHECK_VERSION_REQUIRED"
  88. else
  89. echo "INFO: Ray uses shellcheck for shell scripts, which is not installed. You may install shellcheck=$SHELLCHECK_VERSION_REQUIRED with your system package manager."
  90. fi
  91. if command -v clang-format >/dev/null; then
  92. CLANG_FORMAT_VERSION=$(clang-format --version | awk '{print $3}')
  93. tool_version_check "clang-format" "$CLANG_FORMAT_VERSION" "12.0.0"
  94. else
  95. echo "WARNING: clang-format is not installed!"
  96. fi
  97. if command -v java >/dev/null; then
  98. if [ ! -f "$GOOGLE_JAVA_FORMAT_JAR" ]; then
  99. echo "Java code format tool google-java-format.jar is not installed, start to install it."
  100. wget https://github.com/google/google-java-format/releases/download/google-java-format-1.7/google-java-format-1.7-all-deps.jar -O "$GOOGLE_JAVA_FORMAT_JAR"
  101. fi
  102. else
  103. echo "WARNING:java is not installed, skip format java files!"
  104. fi
  105. if [[ $(flake8 --version) != *"flake8_quotes"* ]]; then
  106. echo "WARNING: Ray uses flake8 with flake8_quotes. Might error without it. Install with: pip install flake8-quotes"
  107. fi
  108. if [[ $(flake8 --version) != *"flake8-bugbear"* ]]; then
  109. echo "WARNING: Ray uses flake8 with flake8-bugbear. Might error without it. Install with: pip install flake8-bugbear"
  110. fi
  111. SHELLCHECK_FLAGS=(
  112. --exclude=1090 # "Can't follow non-constant source. Use a directive to specify location."
  113. --exclude=1091 # "Not following {file} due to some error"
  114. --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
  115. )
  116. # TODO(dmitri): When more of the codebase is typed properly, the mypy flags
  117. # should be set to do a more stringent check.
  118. MYPY_FLAGS=(
  119. '--follow-imports=skip'
  120. '--ignore-missing-imports'
  121. )
  122. MYPY_FILES=(
  123. # Relative to python/ray
  124. 'autoscaler/node_provider.py'
  125. 'autoscaler/sdk/__init__.py'
  126. 'autoscaler/sdk/sdk.py'
  127. 'autoscaler/_private/commands.py'
  128. 'autoscaler/_private/autoscaler.py'
  129. '_private/gcs_utils.py'
  130. )
  131. BLACK_EXCLUDES=(
  132. '--force-exclude' 'python/ray/cloudpickle/*'
  133. '--force-exclude' 'python/build/*'
  134. '--force-exclude' 'python/ray/core/src/ray/gcs/*'
  135. '--force-exclude' 'python/ray/thirdparty_files/*'
  136. '--force-exclude' 'python/ray/_private/thirdparty/*'
  137. )
  138. GIT_LS_EXCLUDES=(
  139. ':(exclude)python/ray/cloudpickle/'
  140. ':(exclude)python/ray/_private/runtime_env/_clonevirtualenv.py'
  141. )
  142. JAVA_EXCLUDES=(
  143. 'java/api/src/main/java/io/ray/api/ActorCall.java'
  144. 'java/api/src/main/java/io/ray/api/CppActorCall.java'
  145. 'java/api/src/main/java/io/ray/api/PyActorCall.java'
  146. 'java/api/src/main/java/io/ray/api/RayCall.java'
  147. )
  148. JAVA_EXCLUDES_REGEX=""
  149. for f in "${JAVA_EXCLUDES[@]}"; do
  150. JAVA_EXCLUDES_REGEX="$JAVA_EXCLUDES_REGEX|(${f//\//\/})"
  151. done
  152. JAVA_EXCLUDES_REGEX=${JAVA_EXCLUDES_REGEX#|}
  153. # TODO(barakmich): This should be cleaned up. I've at least excised the copies
  154. # of these arguments to this location, but the long-term answer is to actually
  155. # make a flake8 config file
  156. FLAKE8_PYX_IGNORES="--ignore=C408,E121,E123,E126,E211,E225,E226,E227,E24,E704,E999,W503,W504,W605"
  157. shellcheck_scripts() {
  158. shellcheck "${SHELLCHECK_FLAGS[@]}" "$@"
  159. }
  160. # Runs mypy on each argument in sequence. This is different than running mypy
  161. # once on the list of arguments.
  162. mypy_on_each() {
  163. pushd python/ray
  164. for file in "$@"; do
  165. echo "Running mypy on $file"
  166. mypy ${MYPY_FLAGS[@]+"${MYPY_FLAGS[@]}"} "$file"
  167. done
  168. popd
  169. }
  170. # Format specified files
  171. format_files() {
  172. local shell_files=() python_files=() bazel_files=()
  173. local name
  174. for name in "$@"; do
  175. local base="${name%.*}"
  176. local suffix="${name#"${base}"}"
  177. local shebang=""
  178. read -r shebang < "${name}" || true
  179. case "${shebang}" in
  180. '#!'*)
  181. shebang="${shebang#/usr/bin/env }"
  182. shebang="${shebang%% *}"
  183. shebang="${shebang##*/}"
  184. ;;
  185. esac
  186. if [ "${base}" = "WORKSPACE" ] || [ "${base}" = "BUILD" ] || [ "${suffix}" = ".BUILD" ] || [ "${suffix}" = ".bazel" ] || [ "${suffix}" = ".bzl" ]; then
  187. bazel_files+=("${name}")
  188. elif [ -z "${suffix}" ] && [ "${shebang}" != "${shebang#python}" ] || [ "${suffix}" != "${suffix#.py}" ]; then
  189. python_files+=("${name}")
  190. elif [ -z "${suffix}" ] && [ "${shebang}" != "${shebang%sh}" ] || [ "${suffix}" != "${suffix#.sh}" ]; then
  191. shell_files+=("${name}")
  192. else
  193. echo "error: failed to determine file type: ${name}" 1>&2
  194. return 1
  195. fi
  196. done
  197. if [ 0 -lt "${#python_files[@]}" ]; then
  198. isort "${python_files[@]}"
  199. black "${python_files[@]}"
  200. fi
  201. if command -v shellcheck >/dev/null; then
  202. if shellcheck --shell=sh --format=diff - < /dev/null; then
  203. if [ 0 -lt "${#shell_files[@]}" ]; then
  204. local difference
  205. difference="$(shellcheck_scripts --format=diff "${shell_files[@]}" || true && printf "-")"
  206. difference="${difference%-}"
  207. printf "%s" "${difference}" | patch -p1
  208. fi
  209. else
  210. echo "error: this version of shellcheck does not support diffs"
  211. fi
  212. fi
  213. }
  214. format_all_scripts() {
  215. command -v flake8 &> /dev/null;
  216. HAS_FLAKE8=$?
  217. # Run isort before black to fix imports and let black deal with file format.
  218. echo "$(date)" "isort...."
  219. git ls-files -- '*.py' "${GIT_LS_EXCLUDES[@]}" | xargs -P 10 \
  220. isort
  221. echo "$(date)" "Black...."
  222. git ls-files -- '*.py' "${GIT_LS_EXCLUDES[@]}" | xargs -P 10 \
  223. black "${BLACK_EXCLUDES[@]}"
  224. echo "$(date)" "MYPY...."
  225. mypy_on_each "${MYPY_FILES[@]}"
  226. if [ $HAS_FLAKE8 ]; then
  227. echo "$(date)" "Flake8...."
  228. git ls-files -- '*.py' "${GIT_LS_EXCLUDES[@]}" | xargs -P 5 \
  229. flake8 --config=.flake8
  230. git ls-files -- '*.pyx' '*.pxd' '*.pxi' "${GIT_LS_EXCLUDES[@]}" | xargs -P 5 \
  231. flake8 --config=.flake8 "$FLAKE8_PYX_IGNORES"
  232. fi
  233. if command -v shellcheck >/dev/null; then
  234. local shell_files non_shell_files
  235. non_shell_files=($(git ls-files -- ':(exclude)*.sh'))
  236. shell_files=($(git ls-files -- '*.sh'))
  237. if [ 0 -lt "${#non_shell_files[@]}" ]; then
  238. shell_files+=($(git --no-pager grep -l -- '^#!\(/usr\)\?/bin/\(env \+\)\?\(ba\)\?sh' "${non_shell_files[@]}" || true))
  239. fi
  240. if [ 0 -lt "${#shell_files[@]}" ]; then
  241. echo "$(date)" "shellcheck scripts...."
  242. shellcheck_scripts "${shell_files[@]}"
  243. fi
  244. fi
  245. }
  246. # Format all files, and print the diff to stdout for travis.
  247. # Mypy is run only on files specified in the array MYPY_FILES.
  248. format_all() {
  249. format_all_scripts "${@}"
  250. echo "$(date)" "clang-format...."
  251. if command -v clang-format >/dev/null; then
  252. git ls-files -- '*.cc' '*.h' '*.proto' "${GIT_LS_EXCLUDES[@]}" | xargs -P 5 clang-format -i
  253. fi
  254. echo "$(date)" "format java...."
  255. if command -v java >/dev/null & [ -f "$GOOGLE_JAVA_FORMAT_JAR" ]; then
  256. git ls-files -- '*.java' "${GIT_LS_EXCLUDES[@]}" | sed -E "\:$JAVA_EXCLUDES_REGEX:d" | xargs -P 5 java -jar "$GOOGLE_JAVA_FORMAT_JAR" -i
  257. fi
  258. echo "$(date)" "done!"
  259. }
  260. # Format files that differ from main branch. Ignores dirs that are not slated
  261. # for autoformat yet.
  262. format_changed() {
  263. # The `if` guard ensures that the list of filenames is not empty, which
  264. # could cause the formatter to receive 0 positional arguments, making
  265. # Black error.
  266. #
  267. # `diff-filter=ACRM` and $MERGEBASE is to ensure we only format files that
  268. # exist on both branches.
  269. MERGEBASE="$(git merge-base upstream/master HEAD)"
  270. if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.py' &>/dev/null; then
  271. git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
  272. isort
  273. fi
  274. if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.py' &>/dev/null; then
  275. git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
  276. black "${BLACK_EXCLUDES[@]}"
  277. if which flake8 >/dev/null; then
  278. git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
  279. flake8 --config=.flake8
  280. fi
  281. fi
  282. if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.pyx' '*.pxd' '*.pxi' &>/dev/null; then
  283. if which flake8 >/dev/null; then
  284. git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.pyx' '*.pxd' '*.pxi' | xargs -P 5 \
  285. flake8 --config=.flake8 "$FLAKE8_PYX_IGNORES"
  286. fi
  287. fi
  288. if which clang-format >/dev/null; then
  289. if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.cc' '*.h' &>/dev/null; then
  290. git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.cc' '*.h' | xargs -P 5 \
  291. clang-format -i
  292. fi
  293. fi
  294. if command -v java >/dev/null & [ -f "$GOOGLE_JAVA_FORMAT_JAR" ]; then
  295. if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.java' &>/dev/null; then
  296. git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.java' | sed -E "\:$JAVA_EXCLUDES_REGEX:d" | xargs -P 5 java -jar "$GOOGLE_JAVA_FORMAT_JAR" -i
  297. fi
  298. fi
  299. if command -v shellcheck >/dev/null; then
  300. local shell_files non_shell_files
  301. non_shell_files=($(git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- ':(exclude)*.sh'))
  302. shell_files=($(git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.sh'))
  303. if [ 0 -lt "${#non_shell_files[@]}" ]; then
  304. shell_files+=($(git --no-pager grep -l -- '^#!\(/usr\)\?/bin/\(env \+\)\?\(ba\)\?sh' "${non_shell_files[@]}" || true))
  305. fi
  306. if [ 0 -lt "${#shell_files[@]}" ]; then
  307. shellcheck_scripts "${shell_files[@]}"
  308. fi
  309. fi
  310. }
  311. # This flag formats individual files. --files *must* be the first command line
  312. # arg to use this option.
  313. if [ "${1-}" == '--files' ]; then
  314. format_files "${@:2}"
  315. # If `--all` or `--scripts` are passed, then any further arguments are ignored.
  316. # Format the entire python directory and other scripts.
  317. elif [ "${1-}" == '--all-scripts' ]; then
  318. format_all_scripts "${@}"
  319. if [ -n "${FORMAT_SH_PRINT_DIFF-}" ]; then git --no-pager diff; fi
  320. # Format the all Python, C++, Java and other script files.
  321. elif [ "${1-}" == '--all' ]; then
  322. format_all "${@}"
  323. if [ -n "${FORMAT_SH_PRINT_DIFF-}" ]; then git --no-pager diff; fi
  324. else
  325. # Add the upstream remote if it doesn't exist
  326. if ! git remote -v | grep -q upstream; then
  327. git remote add 'upstream' 'https://github.com/ray-project/ray.git'
  328. fi
  329. # Only fetch master since that's the branch we're diffing against.
  330. git fetch upstream master || true
  331. # Format only the files that changed in last commit.
  332. format_changed
  333. fi
  334. check_docstyle
  335. # Ensure import ordering
  336. # Make sure that for every import psutil; import setproctitle
  337. # There's a import ray above it.
  338. PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE:-python}
  339. $PYTHON_EXECUTABLE ci/lint/check_import_order.py . -s ci -s python/ray/thirdparty_files -s python/build -s lib
  340. if ! git diff --quiet &>/dev/null; then
  341. echo 'Reformatted changed files. Please review and stage the changes.'
  342. echo 'Files updated:'
  343. echo
  344. git --no-pager diff --name-only
  345. exit 1
  346. fi