diff options
author | Vratko Polak <vrpolak@cisco.com> | 2020-10-30 09:44:41 +0100 |
---|---|---|
committer | Vratko Polak <vrpolak@cisco.com> | 2020-10-30 09:44:41 +0100 |
commit | 1c2097ca62c74abf9380b2c03fdcc2eb0aad16f1 (patch) | |
tree | 152409ff329a553a6355e5e55bdd74d8f5f5226e /resources/libraries/bash/function | |
parent | 1eadcabab5f547f705a69c59262e09c1d1a98182 (diff) |
Tox: Add copyright checkermain
Created by copying out a small subset of CSIT code.
Removed CSIT references, e.g. CSIT_DIR -> ROOT_DIR.
Change-Id: Id4c17fe9c99e74fcf02a48abf4a3cfaa57f890cb
Signed-off-by: Vratko Polak <vrpolak@cisco.com>
Diffstat (limited to 'resources/libraries/bash/function')
-rw-r--r-- | resources/libraries/bash/function/README.txt | 20 | ||||
-rw-r--r-- | resources/libraries/bash/function/common.sh | 110 |
2 files changed, 130 insertions, 0 deletions
diff --git a/resources/libraries/bash/function/README.txt b/resources/libraries/bash/function/README.txt new file mode 100644 index 0000000..9511ddc --- /dev/null +++ b/resources/libraries/bash/function/README.txt @@ -0,0 +1,20 @@ +# Copyright (c) 2020 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +Files in this directory system are to be executed indirectly, +sourced from other scripts. + +In fact, the files should only define functions, +except perhaps some minimal logic needed to import dependencies. +The originating function calls should be executed from elsewhere, +typically from entry scripts. diff --git a/resources/libraries/bash/function/common.sh b/resources/libraries/bash/function/common.sh new file mode 100644 index 0000000..7f8aa0e --- /dev/null +++ b/resources/libraries/bash/function/common.sh @@ -0,0 +1,110 @@ +# Copyright (c) 2020 Cisco and/or its affiliates. +# Copyright (c) 2020 PANTHEON.tech and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -exuo pipefail + +# This library defines functions used by multiple entry scripts. +# Keep functions ordered alphabetically, please. + +function activate_virtualenv () { + + # Update virtualenv pip package, delete and create virtualenv directory, + # activate the virtualenv, install requirements, set PYTHONPATH. + + # Arguments: + # - ${1} - Path to existing directory for creating virtualenv in. + # If missing or empty, ${ROOT_DIR} is used. + # - ${2} - Path to requirements file, ${ROOT_DIR}/requirements.txt if empty. + # Variables read: + # - ROOT_DIR - Path to existing root of local git repository. + # Functions called: + # - die - Print to stderr and exit. + + set -exuo pipefail + + root_path="${1-$ROOT_DIR}" + env_dir="${root_path}/env" + req_path=${2-$ROOT_DIR/requirements.txt} + rm -rf "${env_dir}" || die "Failed to clean previous virtualenv." + pip3 install virtualenv==20.0.20 || { + die "Virtualenv package install failed." + } + virtualenv --no-download --python=$(which python3) "${env_dir}" || { + die "Virtualenv creation for $(which python3) failed." + } + set +u + source "${env_dir}/bin/activate" || die "Virtualenv activation failed." + set -u + pip3 install -r "${req_path}" || { + die "Requirements installation failed." + } +} + + +function common_dirs () { + + # Set global variables, create some directories (without touching content). + + # Variables set: + # - BASH_FUNCTION_DIR - Path to existing directory this file is located in. + # - ROOT_DIR - Path to existing root of local git repository. + # - RESOURCES_DIR - Path to existing CSIT subdirectory "resources". + # Functions called: + # - die - Print to stderr and exit. + + set -exuo pipefail + + this_file=$(readlink -e "${BASH_SOURCE[0]}") || { + die "Some error during locating of this source file." + } + BASH_FUNCTION_DIR=$(dirname "${this_file}") || { + die "Some error during dirname call." + } + # Current working directory could be in a different repo, e.g. VPP. + pushd "${BASH_FUNCTION_DIR}" || die "Pushd failed" + relative_csit_dir=$(git rev-parse --show-toplevel) || { + die "Git rev-parse failed." + } + ROOT_DIR=$(readlink -e "${relative_csit_dir}") || die "Readlink failed." + popd || die "Popd failed." +} + +function die () { + + # Print the message to standard error end exit with error code specified + # by the second argument. + # + # Hardcoded values: + # - The default error message. + # Arguments: + # - ${1} - The whole error message, be sure to quote. Optional + # - ${2} - the code to exit with, default: 1. + + set -x + set +eu + warn "${1:-Unspecified run-time error occurred!}" + exit "${2:-1}" +} + +function warn () { + + # Print the message to standard error. + # + # Arguments: + # - ${@} - The text of the message. + + set -exuo pipefail + + echo "$@" >&2 +} |