scripts/bash/pt_venv.sh
#!/usr/bin/env bash

### BEGIN SCRIPT HEADER
SCRIPT_PATH=$(readlink -f "${BASH_SOURCE[0]}")
DIR_SCRIPT=$(dirname "${SCRIPT_PATH}")
COMMON="${DIR_SCRIPT}/common.sh"
source "$COMMON" || exit
### END SCRIPT HEADER

function check_prerequisites() {
    nb_errors=0

    test_is_not_root || ((nb_errors=nb_errors+1))
    test_command_exists "python" || ((nb_errors=nb_errors+1))
    test_command_exists "virtualenv" || ((nb_errors=nb_errors+1))

    return $nb_errors
}

function create_virtual_env_cwd() {
    nb_errors=0

    # The option '--copies' is used here to copy required binaries instead of
    # creating symbolic links which might not be handled properly on samba file
    # shares.
    if ! virtualenv . --copies;
    then
        print_error "Failed to create virtual Python environment."
        return 1
    fi

    print_success "Created virtual Python environment in: $(realpath .)"
    print_info "Use the command 'source ./bin/activate' to enter the virtual environment."
    print_info "Use the command 'deactivate' to exit the virtual environment."

    return 0
}

check_prerequisites || exit
create_virtual_env_cwd || exit