summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormar77i <mar77i@protonmail.ch>2020-01-24 23:00:02 +0100
committermar77i <mar77i@protonmail.ch>2020-01-24 23:00:02 +0100
commitbc7e816fefa48f6b94181ceb1a8fd4943853bb1f (patch)
tree513bf36f811f967c9e39b5766cdfea695ca6491f
parent4d68a0ba2d787a2aa93149072b5b80e9b437e6a8 (diff)
manage.sh: improve code structure
-rwxr-xr-xmanage.sh127
1 files changed, 61 insertions, 66 deletions
diff --git a/manage.sh b/manage.sh
index 99f4a0a..9cca077 100755
--- a/manage.sh
+++ b/manage.sh
@@ -15,45 +15,41 @@ usage() {
}
setup_venv() {
- local venvs=() venv=
+ local venvs=()
+ if [[ "$(type -t deactivate)" == function ]]; then
+ deactivate
+ fi
while read -r -d ''; do
if [[ -f "${REPLY}/bin/activate" ]]; then
venvs+=("${REPLY}")
fi
- done < <(find . -mindepth 1 -maxdepth 1 -type d -print0)
+ done < <(find . -mindepth 1 -maxdepth 1 -type d -print0 | sort -z)
if (( ${#venvs[@]} == 0 )); then
- venv=venv
- python -m venv "${venv}"
+ VIRTUAL_ENV="${VIRTUAL_ENV:-venv}"
else
- venv="${venvs[0]}"
+ VIRTUAL_ENV="${venvs[0]}"
fi
- if [[ -f env.sh ]]; then
- if ! grep -qER 'env\.sh' venv/bin/activate; then
- d='${VIRTUAL_ENV}'
- d="[[ -n \"${d}\" ]] && . \"\${d}/../env.sh\" --deactivate"
- sed -i -r $'/unset VIRTUAL_ENV/i\\ '"${d}" venv/bin/activate
- fi
- . venv/bin/activate
- pip install -qU pip -r requirements.txt
- . env.sh
- else
- . venv/bin/activate
- pip install -qU pip -r requirements.txt
+ if [[ ! -d "${VIRTUAL_ENV}" ]]; then
+ python -m venv "${VIRTUAL_ENV}"
+ d='${VIRTUAL_ENV}'
+ d="[[ -n \"${d}\" && -r \"${d}/../env.sh\" ]] &&"
+ d="${d} . \"${d}/../env.sh\" --deactivate"
+ sed -i -r $'/unset VIRTUAL_ENV/i\\ '"${d}" \
+ "${VIRTUAL_ENV}/bin/activate"
fi
+ . "${VIRTUAL_ENV}/bin/activate"
+ pip install -qU pip -r requirements.txt
+ if [[ ! -e env.sh && -x ./envsetup.sh ]]; then
+ echo "Warning: env.sh is missing and needs to be configured." >&2
+ ./envsetup.sh
+ fi
+ . env.sh
+ return 0
}
quiet_coverage() {
local prev_lp=0 lp
- quiet=0
- while (( $# > 0 )); do
- case "${1}" in
- --quiet|-q)
- quiet=1
- ;;
- esac
- shift
- done
- if (( quiet == 0 )); then
+ if (( "${1}" == 0 )); then
cat
return
fi
@@ -70,7 +66,7 @@ quiet_coverage() {
}
run_tests() {
- local browser= quiet_args=
+ local browser= quiet=0
while (( ${#} > 0 )); do
case "${1}" in
--browser)
@@ -82,7 +78,7 @@ run_tests() {
shift
;;
--quiet|-q)
- quiet_args+=("${1}")
+ quiet=1
;;
*)
echo "Error: invalid argument: ${1}" >&2
@@ -92,49 +88,48 @@ run_tests() {
shift
done
coverage run -m unittest || exit 1
- coverage report | quiet_coverage "${quiet_args[@]}"
+ coverage report | quiet_coverage "${quiet}"
coverage html
if [[ -n "${browser}" ]]; then
"${browser}" htmlcov/index.html 2>/dev/null &
fi
}
-ret=0
-case "${1}" in
- env)
- if [[ "${0}" -ef "${BASH_SOURCE[0]}" ]]; then
- echo "Error: For this action, the script must be sourced!" >&2
- exit 1
- fi
+if [[ ! "${0}" -ef "${BASH_SOURCE[0]}" ]]; then
+ # script was sourced
+ if [[ "${1}" == env ]]; then
setup_venv
- ;;
- shell)
- if [[ -z "${VIRTUAL_ENV}" ]]; then
- setup_venv
- fi
- python "${@:2}"
- ;;
- test)
- if [[ -z "${VIRTUAL_ENV}" ]]; then
- setup_venv
- fi
- run_tests "${@:2}"
- ;;
- serve)
- gunicorn --reload shortcut.api:api
- ;;
- -h|--help)
- usage "${BASH_SOURCE[0]}"
- ;;
- *)
- usage "${BASH_SOURCE[0]}"
- echo "Error: action missing." >&2
- ret=1
- ;;
-esac
+ return 0
+ else
+ echo "Error: For this action, the script must not be sourced!" >&2
+ return 1
+ fi
+elif [[ "${1}" == -h || "${1}" == --help ]]; then
+ usage "${BASH_SOURCE[0]}"
+ exit 0
+fi
-if [[ "${0}" -ef "${BASH_SOURCE[0]}" ]]; then
- exit "${ret}"
-else
- return "${ret}"
+if [[ -z "${VIRTUAL_ENV}" || ! -d "${VIRTUAL_ENV}" ]]; then
+ setup_venv
fi
+
+case "${1}" in
+env)
+ return "${?}"
+ ;;
+shell)
+ python "${@:2}"
+ ;;
+test)
+ run_tests "${@:2}"
+ ;;
+serve)
+ python -m "event_gateway.lobby" \
+ gunicorn --reload event_gateway.server:api
+ ;;
+*)
+ usage "${BASH_SOURCE[0]}"
+ echo "Error: action missing or invalid." >&2
+ exit 1
+ ;;
+esac