forked from NVIDIA/NVFlare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtest.sh
executable file
·351 lines (302 loc) · 10.3 KB
/
runtest.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env bash
set -e
# output formatting
separator=""
blue=""
green=""
red=""
noColor=""
if [[ -t 1 ]] # stdout is a terminal
then
separator=$'--------------------------------------------------------------------------------\n'
blue="$(tput bold; tput setaf 4)"
green="$(tput bold; tput setaf 2)"
red="$(tput bold; tput setaf 1)"
noColor="$(tput sgr0)"
fi
<<<<<<< HEAD
function print_style_fail_msg() {
echo "${red}Check failed!${noColor}"
echo "Please run ${green}./runtest.sh${noColor} to check errors and fix them."
}
set +e
folders_to_check_license="nvflare test"
grep -r --include "*.py" -L "\(# Copyright (c) \(2021\|2021-2022\|2022\), NVIDIA CORPORATION. All rights reserved.\)\|\(This file is released into the public domain.\)" ${folders_to_check_license} > no_license.lst
if [ -s no_license.lst ]; then
# The file is not-empty.
cat no_license.lst
echo "License text not found on the above files."
echo "Please fix them."
rm -f no_license.lst
exit 1
else
echo "All Python files in folder (${folders_to_check_license}) have license header"
rm -f no_license.lst
=======
WORK_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
NUM_PARALLEL=1
target="${@: -1}"
if [[ "${target}" == -* ]] ;then
target=""
>>>>>>> upstream/main
fi
function install_deps {
if [[ ! -f /tmp/.flare_deps_installed ]]; then
echo "pip installing development dependencies"
python3 -m pip install -r requirements-dev.txt
echo "dependencies installed" > /tmp/.flare_deps_installed
fi;
}
function clean {
echo "remove flare_deps_installed flag"
if [[ -f /tmp/.flare_deps_installed ]]; then
rm -r /tmp/.flare_deps_installed
fi;
echo "remove coverage history"
python3 -m coverage erase
echo "uninstalling nvflare development files..."
python3 setup.py develop --user --uninstall
echo "removing temporary files in ${WORK_DIR}"
find ${WORK_DIR}/nvflare -type d -name "__pycache__" -exec rm -r "{}" \;
find ${WORK_DIR}/nvflare -type f -name "*.py[co]" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type d -name ".eggs" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type d -name "nvflare.egg-info" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type d -name "build" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type d -name "dist" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type d -name ".mypy_cache" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type d -name ".pytype" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type d -name ".coverage" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type f -name ".coverage.*" -exec rm -r "{}" \;
find ${WORK_DIR} -depth -maxdepth 1 -type d -name "__pycache__" -exec rm -r "{}" \;
}
function torch_validate {
echo "validate torch installation"
python3 -c 'import torch; print(torch.__version__); print(torch.rand(5,3))'
}
function print_error_msg() {
echo "${red}Error: $1.${noColor}"
echo ""
}
function print_style_fail_msg() {
echo "${red}Check failed!${noColor}"
echo "Please run auto style fixes: ${green}./runtests.sh -f {noColor}"
}
function report_status() {
status="$1"
if [ ${status} -ne 0 ]
then
print_style_fail_msg
exit ${status}
else
echo "${green}passed!${noColor}"
fi
}
function is_pip_installed() {
echo "check target installed by pip"
return $(python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader(sys.argv[1]) else 1)" $1)
}
function dry_run() {
echo "${separator}${blue}dryrun${noColor}"
echo " " "$1"
}
function check_license() {
folders_to_check_license="nvflare tests"
grep -r --include "*.py" --exclude-dir "*protos*" -L "\(# Copyright (c) \(2021\|2021-2022\|2022\), NVIDIA CORPORATION. All rights reserved.\)\|\(This file is released into the public domain.\)" ${folders_to_check_license} > no_license.lst
if [ -s no_license.lst ]; then
# The file is not-empty.
cat no_license.lst
echo "License text not found on the above files."
echo "Please fix them."
rm -f no_license.lst
exit 1
else
echo "All Python files in folder (${folders_to_check_license}) have license header"
rm -f no_license.lst
fi
}
function flake8_check() {
#python3 -m flake8 nvflare
echo "${separator}${blue}flake8${noColor}"
python3 -m flake8 --version
python3 -m flake8 "$1" --count --statistics
report_status "$?"
}
function black_check() {
echo "${separator}${blue}black-check${noColor}"
python3 -m black --check "$1"
report_status "$?"
echo "Done with black code style checks"
}
function black_fix() {
echo "${separator}${blue}black-fix${noColor}"
python3 -m black "$1"
}
function isort_check() {
echo "${separator}${blue}isort-check${noColor}"
python3 -m isort --check "$1"
report_status "$?"
}
function isort_fix() {
echo "${separator}${blue}isort-fix${noColor}"
python3 -m isort "$1"
}
# wait for approval
#function pylint_check() {
# echo "${separator}${blue}pylint${noColor}"
# python3 -m pylint --version
# ignore_codes="E1101,E1102,E0601,E1130,E1123,E0102,E1120,E1137,E1136"
# python3 -m pylint "$1" -E --disable=$ignore_codes -j $NUM_PARALLEL
# report_status "$?"
#}
function pytype_check() {
echo "${separator}${blue}pytype${noColor}"
pytype_ver=$(python3 -m pytype --version)
if [[ "$OSTYPE" == "darwin"* && "$pytype_ver" == "2021."* ]]; then
echo "${red}pytype not working on macOS 2021 (https://github.com/google/pytype/issues/661). Please upgrade to 2022*.${noColor}"
exit 1
else
python3 -m pytype --version
python3 -m pytype -j ${NUM_PARALLEL} --python-version="$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")" "$1"
report_status "$?"
fi
}
function mypy_check() {
echo "${separator}${blue}mypy${noColor}"
python3 -m mypy --version
python3 -m mypy "$1"
report_status "$?"
}
function check_style_type_import() {
check_target="$1"
# remove pylint for now
# pylint_check $check_target
black_check $check_target
isort_check $check_target
flake8_check $check_target
# pytype causing check fails, comment for now
# pytype_check $check_target
# gives a lot false alarm, comment out for now
# mypy_check $check_target
}
function fix_style_import() {
fix_target="$1"
black_fix "${fix_target}"
isort_fix "${fix_target}"
}
################################################################################
export PYTHONPATH=${WORK_DIR}:$PYTHONPATH && echo "PYTHONPATH is ${PYTHONPATH}"
function help() {
echo "Add description of the script functions here."
echo
echo "Syntax: runtests.sh [-h|--help]
[-l|--check-license]
[-s|--check-format]
[-f|--fix-format]
[-u|--unit-tests]
[-r|--test-report]
[-c|--coverage]
<target> "
echo "<target> : target directories or files used for unit tests, or license check or format checks etc. "
echo " "
echo "options:"
echo ""
echo " -h | --help : display usage help"
echo " -l | --check-license : check copy license"
echo " -s | --check-format : check code styles, formatting, typing, import"
echo " -f | --fix-format : auto fix style formats, import"
echo " -u | --unit-tests : unit tests"
echo " -r | --test-report : used with -u command, turn on unit test report flag. It has no effect without -u "
echo " -c | --coverage : used with -u command, turn on coverage flag, It has no effect without -u "
echo " -d | --dry-run : set dry run flag, print out command"
echo " --clean : clean py and other artifacts generated, clean flag to allow re-install dependencies"
# echo " -i | --integration-tests : integration tests"
exit 1
}
coverage_report=false
unit_test_report=false
dry_run_flag=false
flare_deps_installed=false
if [[ -f /tmp/.flare_deps_installed ]]; then
flare_deps_installed=true
fi
# parse arguments
cmd=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
help
exit
;;
-l|--check-license)
cmd="check_license"
;;
-s |--check-format) # check format and styles
cmd="check_style_type_import"
if [[ -z $target ]]; then
target="nvflare tests"
fi
;;
-f |--fix-format)
if [ ! -z "${target}" ]; then
cmd="fix_style_import ${target}"
else
cmd="fix_style_import nvflare && fix_style_import tests"
fi
;;
-c|--coverage)
coverage_report=true
;;
-r|--test-report)
echo "set unit test flag"
unit_test_report=true
;;
-u |--unit*)
cmd_prefix="torch_validate; python3 -m pytest --numprocesses=auto "
echo "coverage_report=" ${coverage_report}
if [ "${coverage_report}" == true ]; then
cmd_prefix="${cmd_prefix} --cov=${target} --cov-report html:cov_html"
fi
if [ "${unit_test_report}" == true ]; then
cmd_prefix="${cmd_prefix} --junitxml=unit_test.xml "
fi
cmd="$cmd_prefix"
if [[ -z $target ]]; then
target="tests/unit_test"
fi
;;
--clean)
cmd="clean"
;;
-d|--dry-run)
dry_run_flag=true
;;
-*)
help
exit
;;
esac
shift
done
if [[ -z $cmd ]]; then
cmd="check_license -l;
check_style_type_import nvflare tests;
fix_style_import nvflare;
fix_style_import tests ;
python3 -m pytest --numprocesses=auto --cov=nvflare --cov-report html:cov_html --junitxml=unit_test.xml tests/unit_test;
"
else
cmd="$cmd $target"
fi
echo "running command: "
echo " install_deps "
echo " " "$cmd"
echo " "
if [[ $dry_run_flag = "true" ]]; then
dry_run "$cmd"
else
install_deps
eval "$cmd"
fi
echo "Done"