-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.sh
executable file
·81 lines (73 loc) · 1.79 KB
/
format.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
#!/bin/bash
# Check if we can run pip
# This also serves as a check for python3
python3 -m pip --version > /dev/null
if [[ $? -ne 0 ]]
then
echo "ERROR: cannot run 'python3 -m pip'"
exit 1
fi
# Check if the virtual environment with black exists
if [ ! -d black_formatting_env ]
then
echo "Formatting environment not found, installing it..."
python3 -m venv black_formatting_env
./black_formatting_env/bin/python3 -m pip install click==8.0.4 black==19.3b0
fi
# Now we know exactly which black to use
black="./black_formatting_env/bin/python3 -m black"
# Formatting command
files=$(find morpholopy -name '*.py')
files="${files} morphology-pipeline"
cmd="$black -t py38 morphology-pipeline ${files}"
# Print the help
function show_help {
echo -e "This script formats all Python scripts using black"
echo -e " -h, --help \t Show this help"
echo -e " -t, --test \t Test if the Python scripts are well formatted"
}
# Parse arguments
TEST=0
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
# print the help and exit
-h|--help)
show_help
exit
;;
# check if the code is well formatted
-t|--test)
TEST=1
shift
;;
# unknown option
*)
echo "Argument '$1' not implemented"
show_help
exit
;;
esac
done
# Run the required commands
if [[ $TEST -eq 1 ]]
then
# Note trapping the exit status from both commands in the pipe. Also note
# do not use -q in grep as that closes the pipe on first match and we get
# a SIGPIPE error.
echo "Testing if Python scripts are correctly formatted"
$cmd --check
status=$?
# Check formatting
if [[ ! ${status} -eq 0 ]]
then
echo "ERROR: needs formatting"
exit 1
else
echo "Everything is correctly formatted"
fi
else
echo "Formatting all Python scripts"
$cmd
fi