-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-pylint
executable file
·89 lines (71 loc) · 2.21 KB
/
run-pylint
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
#!/bin/bash
# shellcheck disable=SC1091
# Find directory where this script resides
CUR_DIR=$( dirname "$0" )
CUR_DIR=$( readlink -f "$CUR_DIR" )
SCRIPT_NAME=$( basename "$0" )
# Script help text
runhelp() {
echo ""
echo "Usage: Run Sandhill PyLint checks"
echo ""
echo "Will run tests from: $CUR_DIR"
echo ""
}
if [[ $1 == "-h" || $1 == "--help" || $1 == "help" ]]; then
runhelp
exit 0
fi
###############################
## Parse a value for a given config line
## $1 -> File to search
## $2 -> Name of parameter to get value for
## Prints the string value, or empty string if not found
config_param_get() {
# Use of xargs is to remove wrapped quotes from value
grep -E "^ *$2 *=" "$1" 2> /dev/null | tail -n 1 | cut -d= -f2- | sed -e 's/ *$//' -e 's/^ *//' | xargs
}
in_docker() {
[[ -f /.dockerenv ]]
}
cmd_exists() {
which "$1" >/dev/null
}
in_virtualenv() {
python3 -c "import sys; sys.exit(sys.prefix == sys.base_prefix);"
}
main() {
# Go to the script directory
echo "Linting from: $CUR_DIR"
pushd "$CUR_DIR" > /dev/null || true
# Activate the virtualenv
[[ -r env/bin/activate ]] && source env/bin/activate
if ! cmd_exists "pylint"; then
echo "ERROR: Could not find pylint. Need either virtualenv, to be inside container, or set SANDHILL_CONTAINER within instance/sandhill.cfg file."
exit 1
fi
# Read in local env vars
[[ -r .env ]] && source .env
# Run the linting
pylint --fail-under=10.00 sandhill/ instance/
PYLINT_EXIT_CODE=$?
# Return
popd > /dev/null || true
exit $PYLINT_EXIT_CODE
}
find_container_by_prefix() {
if [[ -f $CUR_DIR/instance/sandhill.cfg ]]; then
declare CONTAINER_PREFIX
CONTAINER_PREFIX=$(config_param_get "$CUR_DIR/instance/sandhill.cfg" SANDHILL_CONTAINER)
cmd_exists "docker" && \
[[ -n $CONTAINER_PREFIX ]] && \
docker ps --filter "name=${CONTAINER_PREFIX}" --format '{{.Names}}'
fi
}
CONTAINER_NAME=$(find_container_by_prefix)
if ! in_virtualenv && ! in_docker && [[ -n $CONTAINER_NAME ]]; then
echo "Running command from within Docker: $CONTAINER_NAME"
docker exec -t "$CONTAINER_NAME" ./"$SCRIPT_NAME"
else
main
fi