105 lines
2.3 KiB
Bash
Executable File
105 lines
2.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
_exit() {
|
|
if [[ "$#" -gt 1 ]]; then
|
|
# When there are multiple args, the last arg is the exit code.
|
|
for arg in "${@:1:($# - 1)}"; do
|
|
echo "$arg"
|
|
done
|
|
exit "${@: -1}"
|
|
fi
|
|
echo "$1"
|
|
exit 0
|
|
}
|
|
|
|
help_text="\
|
|
Ensure the python virtual environment is created
|
|
Usage: ensure_venv.sh [OPTIONS] requirements
|
|
|
|
Positionals:
|
|
requirements The requirements file (defaults to 'requirements.txt')
|
|
|
|
Options:
|
|
-d,--dir The virtual environment dir
|
|
-r,--requirements FILE The requirements file (defaults to 'requirements.txt')
|
|
-q,--quiet Don't write to stdout/err
|
|
-h,--help Print this help message and exit
|
|
|
|
"
|
|
|
|
declare tools_dir="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
|
|
declare repo_dir="$tools_dir/.."
|
|
declare venv_dir="$repo_dir/.venv"
|
|
declare requirements_txt=""
|
|
declare -i quiet=0
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-d)
|
|
;&
|
|
--dir)
|
|
if [[ -z "$2" ]]; then
|
|
_exit "Requirements file (-r,--requirements) must be non-empty" 1
|
|
fi
|
|
venv_dir="$2"
|
|
shift 2
|
|
;;
|
|
-r)
|
|
;&
|
|
--requirements)
|
|
if [[ -z "$2" ]]; then
|
|
_exit "Requirements file (-r,--requirements) must be non-empty" 1
|
|
fi
|
|
requirements_txt="$2"
|
|
shift 2
|
|
;;
|
|
-q)
|
|
;&
|
|
--quiet)
|
|
quiet=1
|
|
shift
|
|
;;
|
|
-h)
|
|
;&
|
|
--help)
|
|
_exit "$help_text" 0
|
|
;;
|
|
*)
|
|
if [[ -z "$1" ]]; then
|
|
_exit "Invalid positional argument '$1'" "$help_text" 1
|
|
elif [[ -z "$requirements_txt" ]]; then
|
|
requirements_txt="$1"
|
|
else
|
|
_exit "Unexpected argument '$1'" "$help_text" 1
|
|
fi
|
|
shift 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
_run() {
|
|
declare venv_activate_sh="$venv_dir/bin/activate"
|
|
if [[ -z "$requirements_txt" ]]; then
|
|
requirements_txt="$repo_dir/requirements.txt"
|
|
fi
|
|
|
|
if [[ ! -f "$venv_activate_sh" ]]; then
|
|
echo "Creating python virtual environment '.venv'"
|
|
python3 -m pip install --upgrade pip
|
|
python3 -m venv $venv_dir
|
|
source $venv_activate_sh
|
|
echo "Upgrading pip"
|
|
python -m pip install --upgrade pip
|
|
elif [ -z ${VIRTUAL_ENV+x} ]; then
|
|
source $venv_activate_sh
|
|
fi
|
|
|
|
pip install -q -r $requirements_txt
|
|
}
|
|
|
|
if [ $quiet -eq 1 ]; then
|
|
_run > /dev/null 2&>1
|
|
else
|
|
_run
|
|
fi
|