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 variable available, provide
16 # a function that sets the variable: 'function lf_set_foo() {foo=asdf;}'. Any
17 # scripts that need access to the variable can call the 'set' function. This
18 # keeps the name-space pollution to a minimum.
20 ################################################################################
22 # Name: lf-echo-stderr
27 # lf-echo-stderr "this entire" "string will be sent to stderr"
30 # This function will echo all command line aruments to 'stderr'
35 ################################################################################
37 function lf-echo-stderr() { echo "$@" 1>&2; }
39 ################################################################################
45 # # shellcheck disable=SC1090
48 # if lf-boolean $VAR; then
53 # This function will return a BOOLEAN (true or false) based upon the value
54 # of VAR. The value of VAR will be mapped to lower case. If VAR maps to
55 # "true", return true(0). If VAR maps to "false", return false(1). Any
56 # other values will return false(2) and an error message.
62 ################################################################################
66 if (( $# != 1 )); then
67 echo "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Missing Required Argument"
71 bool=$(echo "$1" | tr '[:upper:]' '[:lower:]')
76 lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line:{BASH_LINENO[0]} : A boolean cannot be a empty string" >&2
80 lf-echo-stderr "ERROR: ${FUNCNAME[0]}() line: ${BASH_LINENO[0]} : Invalid value for a boolean: '$bool'"
86 ################################################################################
89 # lf-activate-venv [-p|--python python] [package]...
92 # # shellcheck disable=SC1090
95 # lf-activate-venv tox tox-pyenv
97 # lf-activate-venv jenkins-job-builder
99 # lf-activate-venv lftools
101 # lf-activate-venv --python 3.6 git-review
104 # This function will create a new Python Virtual Environment (venv) and
105 # install the specified packages in the new venv. The bin directory from the
106 # venv will be prepended to the PATH.
108 # By default all packages are installed with '--upgrade-strategy eager'.
109 # The venv will always contain pip & virtualenv.
111 # Some packages have a default version. If one of those packages is specified,
112 # the 'version' specifier will be added for the install. If the version is
113 # specified on the command line that version will be used.
114 # The following packages have default versions:
116 # jenkins-job-builder $JJB_VERSION
118 # If the --python option is specified, that python executable will be used to
119 # create the venv. The --python option must be in the PATH. The venv will be
120 # located in '/tmp/venv-####'.
126 ################################################################################
128 function lf-activate-venv()
131 lf_tmp_venv=$(mktemp -d /tmp/venv-XXXX)
134 options=$(getopt -o 'p:' -l 'python:' -n "${FUNCNAME[0]}" -- "$@" )
135 eval set -- "$options"
138 -p|--python) python=$2; shift 2 ;;
140 *) lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown switch '$1'." ; return 1 ;;
143 if ! type $python > /dev/null; then
144 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: Unknown Python: $python"
148 echo "${FUNCNAME[0]}(): INFO: Creating '$python' venv ($lf_tmp_venv)"
153 # For Python2, just create venv and install pip
154 virtualenv -p $python $lf_tmp_venv || return 1
155 $lf_tmp_venv/bin/pip install --upgrade --quiet pip || return 1
156 if [[ -z $pkg_list ]]; then
157 echo "${FUNCNAME[0]}(): WARNING: No packages to install"
160 echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
161 $lf_tmp_venv/bin/pip install --upgrade --quiet $pkg_list || return 1
165 # Add version specifier for some packages
168 jenkins-job-builder) pkg_list+="jenkins-job-builder==${JJB_VERSION:-2.8.0} " ;;
169 *) pkg_list+="$arg " ;;
172 $python -m venv $lf_tmp_venv || return 1
173 $lf_tmp_venv/bin/pip install --upgrade --quiet pip virtualenv || return 1
174 if [[ -z $pkg_list ]]; then
175 echo "${FUNCNAME[0]}(): WARNING: No packages to install"
178 echo "${FUNCNAME[0]}(): INFO: Installing: $pkg_list"
179 $lf_tmp_venv/bin/pip install --upgrade --quiet --upgrade-strategy eager \
180 $pkg_list || return 1
183 lf-echo-stderr "${FUNCNAME[0]}(): ERROR: No support for: $python"
187 echo "${FUNCNAME[0]}(): INFO: Adding $lf_tmp_venv/bin to PATH"
188 PATH=$lf_tmp_venv/bin:$PATH
191 } # End lf-activate-venv()
193 ################################################################################
196 # lf-git-validate-jira-urls
199 # # shellcheck disable=SC1090
202 # lf-git-validate-jira-urls
205 # Check for JIRA URLS in the commit message
211 ################################################################################
213 function lf-git-validate-jira-urls()
215 echo "Checking for JIRA URLs in commit message..."
216 # if JIRA_URL is not defined, nothing to do
217 if [[ -v JIRA_URL ]]; then
218 base_url=$(echo "$JIRA_URL" | awk -F'/' '{print $3}')
219 jira_link=$(git rev-list --format=%B --max-count=1 HEAD | grep -io "http[s]*://$base_url/" || true)
220 if [[ -n $jira_link ]]; then
221 lf-echo-error 'Remove JIRA URLs from commit message'
222 lf-echo-error 'Add jira references as: Issue: <JIRAKEY>-<ISSUE#>, instead of URLs'
229 ################################################################################
232 # lf-jjb-check-ascii()
235 # # shellcheck disable=SC1090
241 # Check for JJB YAML files containing non-printable ascii characters. This
242 # function must be run from the top of the global-jjb repo.
248 ################################################################################
250 function lf-jjb-check-ascii()
252 if [[ ! -d "jjb" ]]; then
253 lf-echo-error "${FUNCNAME[0]}(): ERROR: missing jjb directory"
254 lf-echo-error "This function can only be run from top of global-jjb directory"
257 if LC_ALL=C grep -I -r '[^[:print:][:space:]]' jjb/; then
258 lf-echo-error "${FUNCNAME[0]}(): ERROR: Found YAML files containing non-printable characters."
261 echo "${FUNCNAME[0]}(): INFO: All JJB YAML files contain only printable ASCII characters"
265 ################################################################################
266 # Functions that assign Variables
267 ################################################################################
269 # These variables are shell (local) variables and need to be lower-case so
270 # Shellcheck knows they are shell variables and will check for
273 function lf-set-maven-options()
275 # Disable 'unused-variable' check
276 # shellcheck disable=SC2034
277 maven_options="--show-version --batch-mode -Djenkins \
278 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
279 -Dmaven.repo.local=/tmp/r -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"