- python=$1
- shift
- if ! type $python > /dev/null; then
- lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
- return 1
- fi
- local pkg_list="$* "
- 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
- $venv/bin/pip install --upgrade $pkg_list > $pip_log || return 1
- ;;
- python3*)
- # Include any packages that are tied to a specific version
- pkg_list+="jenkins-job-builder==2.8.0 "
- $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, may need to run twice to get all versions
- # synced up. Ignore exit status on first try
- $venv/bin/pip install --upgrade $pkg_list >> $pip_log || true
- echo "Running 'pip --upgrade' to validate..."
- $venv/bin/pip install --upgrade $pkg_list >> $pip_log || return 1
- ;;
- *)
- 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()