1
+ #! /usr/bin/env bash
2
+ # YAPF + 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
+
5
+ # Cause the script to exit if a single command fails
6
+ set -eo pipefail
7
+
8
+ FLAKE8_VERSION_REQUIRED=" 3.7.7"
9
+ YAPF_VERSION_REQUIRED=" 0.23.0"
10
+
11
+ check_command_exist () {
12
+ VERSION=" "
13
+ case " $1 " in
14
+ yapf)
15
+ VERSION=$YAPF_VERSION_REQUIRED
16
+ ;;
17
+ flake8)
18
+ VERSION=$FLAKE8_VERSION_REQUIRED
19
+ ;;
20
+ * )
21
+ echo " $1 is not a required dependency"
22
+ exit 1
23
+ esac
24
+ if ! [ -x " $( command -v $1 ) " ]; then
25
+ echo " $1 not installed. pip install $1 ==$VERSION "
26
+ exit 1
27
+ fi
28
+ }
29
+
30
+ check_command_exist yapf
31
+ check_command_exist flake8
32
+
33
+ ver=$( yapf --version)
34
+ if ! echo $ver | grep -q 0.23.0; then
35
+ echo " Wrong YAPF version installed: 0.23.0 is required, not $ver . $YAPF_DOWNLOAD_COMMAND_MSG "
36
+ exit 1
37
+ fi
38
+
39
+ # this stops git rev-parse from failing if we run this from the .git directory
40
+ builtin cd " $( dirname " ${BASH_SOURCE:- $0 } " ) "
41
+
42
+ ROOT=" $( git rev-parse --show-toplevel) "
43
+ builtin cd " $ROOT " || exit 1
44
+
45
+ # Add the upstream remote if it doesn't exist
46
+ if ! git remote -v | grep -q upstream; then
47
+ git remote add ' upstream' ' https://github.com/ray-project/ray_lightning_accelerators.git'
48
+ fi
49
+
50
+ FLAKE8_VERSION=$( flake8 --version | awk ' {print $1}' )
51
+ YAPF_VERSION=$( yapf --version | awk ' {print $2}' )
52
+
53
+ # params: tool name, tool version, required version
54
+ tool_version_check () {
55
+ if [[ $2 != $3 ]]; then
56
+ echo " WARNING: Ray uses $1 $3 , You currently are using $2 . This might generate different results."
57
+ fi
58
+ }
59
+
60
+ tool_version_check " flake8" $FLAKE8_VERSION $FLAKE8_VERSION_REQUIRED
61
+ tool_version_check " yapf" $YAPF_VERSION $YAPF_VERSION_REQUIRED
62
+
63
+ if which clang-format > /dev/null; then
64
+ CLANG_FORMAT_VERSION=$( clang-format --version | awk ' {print $3}' )
65
+ tool_version_check " clang-format" $CLANG_FORMAT_VERSION " 7.0.0"
66
+ else
67
+ echo " WARNING: clang-format is not installed!"
68
+ fi
69
+
70
+ # Only fetch main since that's the branch we're diffing against.
71
+ git fetch upstream main || true
72
+
73
+ YAPF_FLAGS=(
74
+ ' --style' " $ROOT /.style.yapf"
75
+ ' --recursive'
76
+ ' --parallel'
77
+ )
78
+
79
+ YAPF_EXCLUDES=(
80
+ # '--exclude' 'python/ray/cloudpickle/*'
81
+ # '--exclude' 'python/build/*'
82
+ # '--exclude' 'python/ray/core/src/ray/gcs/*'
83
+ # '--exclude' 'python/ray/thirdparty_files/*'
84
+ )
85
+
86
+ # Format specified files
87
+ format () {
88
+ yapf --in-place " ${YAPF_FLAGS[@]} " -- " $@ "
89
+ }
90
+
91
+ # Format files that differ from main branch. Ignores dirs that are not slated
92
+ # for autoformat yet.
93
+ format_changed () {
94
+ # The `if` guard ensures that the list of filenames is not empty, which
95
+ # could cause yapf to receive 0 positional arguments, making it hang
96
+ # waiting for STDIN.
97
+ #
98
+ # `diff-filter=ACRM` and $MERGEBASE is to ensure we only format files that
99
+ # exist on both branches.
100
+ MERGEBASE=" $( git merge-base upstream/main HEAD) "
101
+
102
+ if ! git diff --diff-filter=ACRM --quiet --exit-code " $MERGEBASE " -- ' *.py' & > /dev/null; then
103
+ git diff --name-only --diff-filter=ACRM " $MERGEBASE " -- ' *.py' | xargs -P 5 \
104
+ yapf --in-place " ${YAPF_EXCLUDES[@]} " " ${YAPF_FLAGS[@]} "
105
+ if which flake8 > /dev/null; then
106
+ git diff --name-only --diff-filter=ACRM " $MERGEBASE " -- ' *.py' | xargs -P 5 \
107
+ flake8 --inline-quotes ' "' --no-avoid-escape --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605
108
+ fi
109
+ fi
110
+
111
+ if ! git diff --diff-filter=ACRM --quiet --exit-code " $MERGEBASE " -- ' *.pyx' ' *.pxd' ' *.pxi' & > /dev/null; then
112
+ if which flake8 > /dev/null; then
113
+ git diff --name-only --diff-filter=ACRM " $MERGEBASE " -- ' *.pyx' ' *.pxd' ' *.pxi' | xargs -P 5 \
114
+ flake8 --inline-quotes ' "' --no-avoid-escape --ignore=C408,E121,E123,E126,E211,E225,E226,E227,E24,E704,E999,W503,W504,W605
115
+ fi
116
+ fi
117
+ }
118
+
119
+ # Format all files, and print the diff to stdout for travis.
120
+ format_all () {
121
+ yapf --diff " ${YAPF_FLAGS[@]} " " ${YAPF_EXCLUDES[@]} " ray_lightning
122
+ flake8 --inline-quotes ' "' --no-avoid-escape --ignore=C408,E121,E123,E126,E211,E225,E226,E227,E24,E704,E999,W503,W504,W605 ray_lightning
123
+ }
124
+
125
+ # This flag formats individual files. --files *must* be the first command line
126
+ # arg to use this option.
127
+ if [[ " $1 " == ' --files' ]]; then
128
+ format " ${@: 2} "
129
+ # If `--all` is passed, then any further arguments are ignored and the
130
+ # entire python directory is formatted.
131
+ elif [[ " $1 " == ' --all' ]]; then
132
+ format_all
133
+ else
134
+ # Format only the files that changed in last commit.
135
+ format_changed
136
+ fi
137
+
138
+ if ! git diff --quiet & > /dev/null; then
139
+ echo ' Reformatted changed files. Please review and stage the changes.'
140
+ echo ' Files updated:'
141
+ echo
142
+
143
+ git --no-pager diff --name-only
144
+
145
+ exit 1
146
+ fi
147
+
148
+ echo ' Linting check finished successfully.'
0 commit comments