+ echo "${FUNCNAME[0]}(): Adding $venv/bin to PATH"
+ PATH=$venv/bin:$PATH
+
+} # ENd lf-venv-activate()
+
+################################################################################
+#
+# NAME
+# lf-venv-create()
+#
+# SYNOPSIS
+# # shellcheck disable=SC1090
+# source ~/lf-env.sh
+#
+# lf-venv-create python2
+# or
+# lf-venv-create python3.6
+#
+# DESCRIPTION
+# This function will create a Python Virtual Environment based on the
+# python specified. The 'python' argument must be in the PATH. The venv
+# will be located in ~/.venv## where ## comes from the 'python' argument.
+# I.E. python3 -> ~/.venv3. The resulting venv will be left 'read-only' to
+# discourage the installation of any other packages (except by
+# lf-venv-add()).
+#
+# RETURN VALUES
+# None
+#
+################################################################################
+
+function lf-venv-create()
+{
+ if (( $# != 1 )); then
+ lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Missing Python argument"
+ return 1
+ fi
+ python=$1
+ if ! type $python > /dev/null; then
+ lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
+ return 1
+ fi
+ local venv=~/.venv${python#python}
+ local suffix=$python-$$
+ local pip_log=/tmp/pip_log.$suffix
+ if [[ -f $venv/$_lf_done_file ]]; then
+ echo "Venv Already Exists: '$venv'"
+ return
+ fi
+ # Make sure noting is left over
+ if [[ -d $venv ]]; then
+ chmod -R +w $venv
+ rm -rf $venv
+ fi
+
+ echo "Creating '$python' venv ($venv)"
+
+ case $python in
+ python2*)
+ # For Python2, just create venv and install pip
+ virtualenv -p $python $venv > $pip_log || return 1
+ $venv/bin/pip install --upgrade pip > $pip_log || return 1
+ ;;
+ python3*)
+ local pkg_list="git-review jenkins-job-builder lftools[openstack] "
+ pkg_list+="python-heatclient python-openstackclient "
+ pkg_list+="setuptools testresources tox yq"
+ $python -m venv $venv > $pip_log
+ $venv/bin/pip install --upgrade pip > $pip_log || return 1
+ # Redirect errors for now
+ $venv/bin/pip install --upgrade $pkg_list >> $pip_log 2> /dev/null || return 1
+ # Generate list of packages
+ pkg_list=$($venv/bin/pip freeze | awk -F '=' '{print $1}') || return 1
+ # Update all packages, usuaally need to run twice to get all versions
+ # correct.
+ local upgrade_cmd="$venv/bin/pip install --upgrade $pkg_list"
+ if $upgrade_cmd >> $pip_log 2>&1 > /dev/null ; then
+ echo -n "Running 'pip --upgrade' to validate..."
+ $upgrade_cmd >> $pip_log || return 1
+ echo "..OK"
+ fi
+ ;;
+ *)
+ lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
+ return 1
+ ;;
+ esac
+
+ touch $venv/$_lf_done_file
+ # Once this venv is created, make it read-only
+ chmod -R -w $venv
+ # Archive output of 'pip freeze'
+ mkdir -p $WORKSPACE/archives
+ $venv/bin/pip freeze > $WORKSPACE/archives/freeze-log-$python || return 1
+ rm -rf $pip_log
+
+} # End lf-venv-create()
+
+################################################################################
+#
+# NAME
+# lf-venv-add()
+#
+# SYNOPSIS
+# # shellcheck disable=SC1090
+# source ~/lf-env.sh
+#
+# lf-venv-add python3 pkg
+# or
+# lf-venv-add python2 pkg1 pkg2 pkg3
+#
+# DESCRIPTION
+# This function will add one or more python packages to an existing venv.
+# Attempts to add packages directly (pip) will result in errors because
+# the venv does not have 'write' permission.
+#
+# RETURN VALUES
+# None
+#
+################################################################################
+
+function lf-venv-add()
+{
+ if (( $# < 2 )); then
+ lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Missing Package argument"
+ return 1
+ fi
+ python=$1
+ if ! type $python > /dev/null ; then
+ lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
+ return 1
+ fi
+ shift
+ local venv=~/.venv${python#python}
+ if [[ ! -f $venv/$_lf_done_file ]]; then
+ lf-echo-stderr "${FUNCNAME[0]}(): ERROR: '$venv' is not a valid venv"
+ return 1
+ fi
+ local pkg_list=$*
+ local pip_log=/tmp/pip_log-$$
+
+ echo "Installing '$pkg_list' into $venv"
+ chmod -R +w $venv
+ rm $venv/$_lf_done_file
+ $venv/bin/pip install --upgrade $pkg_list > $pip_log || return 1
+ pkg_list=$($venv/bin/pip freeze | awk -F '=' '{print $1}') || return 1
+ $venv/bin/pip install --upgrade $pkg_list > $pip_log || return 1
+ touch $venv/$_lf_done_file
+ chmod -R -w $venv
+ # Archive output of 'pip freeze'
+ $venv/bin/pip freeze > $WORKSPACE/archives/freeze-log-$python || return 1
+
+} # End lf-venv-add()