3 # SPDX-License-Identifier: EPL-1.0
4 ##############################################################################
5 # Copyright (c) 2019 The Linux Foundation and others.
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Eclipse Public License v1.0
9 # which accompanies this distribution, and is available at
10 # http://www.eclipse.org/legal/epl-v10.html
11 ##############################################################################
13 # A library of functions for LF/Jenkins bash scripts. In the general case, these
14 # functions should only use 'local' variables, and should NOT set
15 # shell/environment variables. If you want to make a
16 # variable available, provide a function that sets the variable: 'function
17 # lf_set_foo() {foo=asdf;}'. Any scripts that need access to the variable can
18 # call the 'set' function. This keeps the name-space pollution to a minimum.
20 # Shell variables that are shared between functions
22 _lf_done_file=".lf-done"
24 ################################################################################
26 # Name: lf-echo-stderr
31 # lf-echo-stderr "this entire" "string will be sent to stderr"
34 # This function will echo all command line aruments to 'stderr'
39 ################################################################################
41 function lf-echo-stderr() { echo "$@" 1>&2; }
43 ################################################################################
49 # # shellcheck disable=SC1090
52 # if lf-boolean $VAR; then
57 # This function will return a BOOLEAN (true or false) based upon the value
58 # of VAR. The value of VAR will be mapped to lower case. If VAR maps to
59 # "true", return true(0). If VAR maps to "false", return false(1). Any
60 # other values will return false(2) and an error message.
63 # true(0), false(1) or false(2)
65 ################################################################################
69 if (( $# != 1 )); then
70 echo "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Missing Required Argument"
74 bool=$(echo "$1" | tr '[:upper:]' '[:lower:]')
79 lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line:{BASH_LINENO[0]} : A boolean cannot be a empty string" >&2
83 lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Invalid value for a boolean: '$bool'"
89 ################################################################################
95 # # shellcheck disable=SC1090
98 # lf-venv-activate python3
101 # This function will validate existance of 'python' venv. If it exists
102 # 'path-to-venv/bin' will be prepended to the PATH.
107 ################################################################################
109 function lf-venv-activate()
111 if (( $# != 1 )); then
112 echo "${FUNCNAME[0]}(): Missing path operand"
117 if [[ $arg =~ ^python ]] && type $arg > /dev/null ; then
118 venv=~/.venv${arg#python}
122 # Validate the path to a VENV
123 if [[ ! -f $venv/$_lf_done_file ]]; then
124 lf-echo-stderr "ERROR: Is '$venv' a Python Environment ?"
127 echo "${FUNCNAME[0]}(): Adding $venv/bin to PATH"
130 } # ENd lf-venv-activate()
132 ################################################################################
135 # lf-venv-create python [package]...
138 # # shellcheck disable=SC1090
141 # lf-venv-create python3 tox tox-pyenv virtualenv
142 # lf-venv-create python3.6
145 # This function will create/update a Python Virtual Environment (venv) based
146 # on the python specified. The 'python' argument must be in the PATH. The venv
147 # will be located in ~/.venv## where ## comes from the 'python' argument.
148 # I.E. python3 -> ~/.venv3. The resulting venv will be left 'read-only' to
149 # discourage the installation of any other packages (except by
150 # lf-venv-create()). By default, only versioned packages will be installed, so
151 # any required packages need to be specified. By default the 'pip install
152 # --upgrade' will be run multiple times. Sometimes pip needs that to get the
153 # versioning correct.
158 ################################################################################
160 function lf-venv-create()
162 if (( $# < 1 )); then
163 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Missing Required Arguments"
168 if ! type $python > /dev/null; then
169 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
173 local venv=~/.venv${python#python}
174 local suffix=$python-$$
175 local pip_log=/tmp/pip_log.$suffix
176 if [[ -f $venv/$_lf_done_file ]]; then
177 echo "Venv Already Exists: '$venv'"
180 # Make sure noting is left over
181 if [[ -d $venv ]]; then
186 echo "Creating '$python' venv ($venv)"
190 # For Python2, just create venv and install pip
191 virtualenv -p $python $venv > $pip_log || return 1
192 $venv/bin/pip install --upgrade pip > $pip_log || return 1
193 $venv/bin/pip install --upgrade $pkg_list > $pip_log || return 1
196 # Include any packages that are tied to a specific version
197 pkg_list+="jenkins-job-builder==2.8.0 "
198 $python -m venv $venv > $pip_log
199 $venv/bin/pip install --upgrade pip > $pip_log || return 1
200 # Redirect errors for now
201 $venv/bin/pip install --upgrade $pkg_list >> $pip_log 2> /dev/null || return 1
202 # Generate list of packages
203 pkg_list=$($venv/bin/pip freeze | awk -F '=' '{print $1}') || return 1
204 # Update all packages, may need to run twice to get all versions
205 # synced up. Ignore exit status on first try
206 $venv/bin/pip install --upgrade $pkg_list >> $pip_log || true
207 echo "Running 'pip --upgrade' to validate..."
208 $venv/bin/pip install --upgrade $pkg_list >> $pip_log || return 1
211 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
216 touch $venv/$_lf_done_file
217 # Once this venv is created, make it read-only
219 # Archive output of 'pip freeze'
220 mkdir -p $WORKSPACE/archives
221 $venv/bin/pip freeze > $WORKSPACE/archives/freeze-log-$python || return 1
224 } # End lf-venv-create()
226 ################################################################################
232 # # shellcheck disable=SC1090
235 # lf-venv-add python3 pkg
237 # lf-venv-add python2 pkg1 pkg2 pkg3
240 # This function will add one or more python packages to an existing venv.
241 # Attempts to add packages directly (pip) will result in errors because
242 # the venv does not have 'write' permission.
247 ################################################################################
249 function lf-venv-add()
251 if (( $# < 2 )); then
252 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Missing Package argument"
256 if ! type $python > /dev/null ; then
257 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
261 local venv=~/.venv${python#python}
262 if [[ ! -f $venv/$_lf_done_file ]]; then
263 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: '$venv' is not a valid venv"
267 local pip_log=/tmp/pip_log-$$
269 echo "Installing '$pkg_list' into $venv"
271 rm $venv/$_lf_done_file
272 $venv/bin/pip install --upgrade $pkg_list > $pip_log || return 1
273 pkg_list=$($venv/bin/pip freeze | awk -F '=' '{print $1}') || return 1
274 $venv/bin/pip install --upgrade $pkg_list > $pip_log || return 1
275 touch $venv/$_lf_done_file
277 # Archive output of 'pip freeze'
278 $venv/bin/pip freeze > $WORKSPACE/archives/freeze-log-$python || return 1
280 } # End lf-venv-add()
282 ################################################################################
283 # Functions that assign Variables
284 ################################################################################
286 # These variables are shell (local) variables and need to be lower-case so
287 # Shellcheck knows they are shell variables and will check for
290 function lf-set-maven-options()
292 # Disable 'unused-variable' check
293 # shellcheck disable=SC2034
294 maven_options="--show-version --batch-mode -Djenkins \
295 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
296 -Dmaven.repo.local=/tmp/r -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"