-
Notifications
You must be signed in to change notification settings - Fork 19
/
check_python_code_format
executable file
·52 lines (47 loc) · 1.39 KB
/
check_python_code_format
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
#! /bin/bash
#
# A simple script for verifying that all the the Python code correctly formatted
# with the Black code formatter.
#
# Usage:
# see --help
#
#
set -e
while [[ $# -gt 0 ]]
do
case "$1" in
-h|--help)
echo "Usage: $(basename $0) [OPTIONS] <git-range>"
echo ""
echo "This script runs the Black formatter on all the Python code"
echo "in the current repository."
echo "The files formatted are controlled by the local pyproject.toml file's"
echo "include and exclude regexes."
echo "and exits with exit-code 1 if any python code found is not formatted"
echo ""
echo "Recognized environment variables:"
echo ""
echo "BLACK_FORMATTER_VERSION:"
echo "The version of the black formatter used"
exit 0
;;
*)
break
;;
esac
done
tfile=$(mktemp)
trap cleanup EXIT
function cleanup() {
rm -f ${tfile}
}
if ! black --check --verbose . >& ${tfile}; then
cat ${tfile} | grep -v "good job" | grep -v "wasn't modified on disk since last run" >&2 || true
echo >&2 ""
echo >&2 "Please fix your code locally with: black ."
echo >&2 "This will automatically format the code as needed."
echo >&2 "Note that the Black version used is: " "$BLACK_FORMATTER_VERSION."
exit 1
fi
exit 0