forked from facebookresearch/ParlAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoformat.sh
executable file
·174 lines (158 loc) · 4.48 KB
/
autoformat.sh
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# exit on errors
set -e
# configuration options for our doc formatting
DOCOPTS="--pre-summary-newline --wrap-descriptions 88 --wrap-summaries 88 \
--make-summary-multi-line"
usage () {
# prints the help message
cat <<EOF
Usage: $0
Autoformats (or checks) code so it conforms with ParlAI standards. By default,
runs black, flake8, and docformatter only on the changed files in the current branch.
Optional Arguments
-a, --all run on all files, not just changed ones.
-b, --black only run black.
-c, --check perform a check, but don't make any changes.
-d, --doc only run docformatter.
-f, --flake8 only run flake8.
-h, --help print this help message and exit
-i, --internal run within parlai_internal
EOF
}
reroot() {
# possibly rewrite all filenames if root is nonempty
if [[ "$1" != "" ]]; then
cat | xargs -I '{}' realpath --relative-to=. $1/'{}'
else
cat
fi
}
onlyexists() {
# filter filenames based on what exists on disk
while read fn; do
if [ -f "${fn}" ]; then
echo "$fn"
fi
done
}
# parse the command line args
RUN_ALL_FILES=0
RUNALL=1
INTERNAL=0
CHECK=0
CMD=""
while true; do
case $1 in
-h | --help)
usage
exit 0
;;
-a | --all)
RUN_ALL_FILES=1
;;
-f | --flake8)
[[ "$CMD" != "" ]] && (echo "Don't mix args." && false);
RUNALL=0
CMD="flake8"
;;
-c | --check)
CHECK=1
;;
-d | --doc)
[[ "$CMD" != "" ]] && (echo "Don't mix args." && false);
CMD="docformatter"
RUNALL=0
;;
-i | --internal)
INTERNAL=1
;;
-b | --black)
[[ "$CMD" != "" ]] && (echo "Don't mix args." && false);
CMD="black"
RUNALL=0
;;
"")
break
;;
*)
usage
echo
echo "Cannot handle arg $1."
exit 1
;;
esac
shift
done
# decide which repo we're working on
if [[ $INTERNAL -eq 1 ]]; then
ROOT="$(git -C ./parlai_internal/ rev-parse --show-toplevel)"
REPO="-C ./parlai_internal"
else
ROOT=""
REPO=""
fi
# find out what files we're working on
if [[ $RUN_ALL_FILES -eq 1 ]]; then
CHECK_FILES="$(git $REPO ls-files | grep '\.py$' | reroot $ROOT | onlyexists $ROOT | tr '\n' ' ')"
else
CHECK_FILES="$(git $REPO diff --name-only master... | grep '\.py$' | reroot $ROOT | onlyexists | tr '\n' ' ')"
fi
# if we're doing all the tests, we should run them in serial
if [[ $RUNALL -eq 1 ]]
then
if [[ $CHECK -eq 1 ]]; then A="$A --check"; fi
if [[ $INTERNAL -eq 1 ]]; then A="$A --internal"; fi
if [[ $RUN_ALL_FILES -eq 1 ]]; then A="$A --all"; fi
bash $0 --black $A
bash $0 --doc $A
bash $0 --flake8 $A
exit 0
fi
# finally do the actual checks
if [ "$CHECK_FILES" != "" ]
then
if [[ "$CMD" == "black" ]]
then
command -v black >/dev/null || \
( echo "Please run \`pip install black\` and rerun $0." && false )
if [[ $CHECK -eq 0 ]]
then
black $CHECK_FILES
else
if ! ( black --check $CHECK_FILES 2>/dev/null ); then
echo -e "\033[0;31mSome files need to be blacked.\033[0m"
echo "Please run \`bash ./autoformat.sh\` and commit the changes."
exit 1
fi
fi
elif [[ "$CMD" == "docformatter" ]]
then
command -v docformatter > /dev/null || \
( echo "Please run \`pip install docformatter\` and rerun $0." && false )
if [[ $CHECK -eq 0 ]]
then
docformatter -i $DOCOPTS $CHECK_FILES
else
if ! docformatter -c $DOCOPTS $CHECK_FILES > /dev/null 2>&1; then
echo -e "\033[0;31mSome docstrings need to be formatted.\033[0m"
echo "Please run \`./autoformat.sh\` and commit the changes."
exit 1
fi
fi
elif [[ "$CMD" == "flake8" ]]
then
command -v flake8 >/dev/null || \
( echo "Please run \`pip install flake8\` and rerun $0." && false )
# soft complaint on too-long-lines
flake8 --select=E501 --show-source $CHECK_FILES
# hard complaint on really long lines
flake8 --max-line-length=127 --show-source $CHECK_FILES
else
echo "Don't know how to \`$CMD\`."
exit 1
fi
fi