Jump to content

/home/alex/dev/venv-runner.sh (1)

From RaySoft
#!/bin/bash -
# ------------------------------------------------------------------------------
# venv-runner.sh
# ==============
#
# Scope     Native
# Copyright (C) 2025 by RaySoft, Zurich, Switzerland
# License   GNU General Public License (GPL) 2.0
#           https://www.gnu.org/licenses/gpl2.txt
#
# ------------------------------------------------------------------------------

set -o 'noglob' -o 'nounset' -o 'pipefail' # -o 'errexit' -o 'xtrace'

# ------------------------------------------------------------------------------

project_path="$(realpath "$0" | sed 's|/src/venv-runner.sh$||')"

script_path="${project_path}/src/${project_path##*/}"

venv_path="${project_path}/venv"

if [[ ! -d "${venv_path}" ]]; then
  echo "Error finding virtual Python environment: ${venv_path}!"

  exit 1
fi

# Activate virtual Python environment
source "${venv_path}/bin/activate"

if [[ -f "${script_path}.py" ]]; then
  python3 "${script_path}.py" "$@"
elif [[ -f "${script_path}.sh" ]]; then
  bash "${script_path}.sh" "$@"
else
  echo "Error finding script: ${script_path}.*!"

  # Deactivate virtual Python environment
  deactivate

  exit 1
fi

# Deactivate virtual Python environment
deactivate

# ------------------------------------------------------------------------------

exit 0

Usage

Set project path
project_path="${HOME}/dev/sync2mw"
Create the virtual Python environment
python3 -m 'venv' --system-site-packages \
  --prompt "venv '${project_path##*/}'" "${project_path}/venv"
Install dependencies
source "${project_path}/venv/bin/activate"

python3 -m 'pip' install --upgrade 'pip' 'mwclient'

python3 -m 'pip' check

deactivate
Link venv-runner.sh script
ln --force {"${HOME}/dev","${project_path}/src"}'/venv-runner.sh'
Run the script
"${project_path}/src/venv-runner.sh" --pattern='venv-runner.sh'