|
| 1 | +#!/usr/bin/env bash |
| 2 | +#** ***************************************************************************** |
| 3 | +# * |
| 4 | +# * If not stated otherwise in this file or this component's LICENSE file the |
| 5 | +# * following copyright and licenses apply: |
| 6 | +# * |
| 7 | +# * Copyright 2024 RDK Management |
| 8 | +# * |
| 9 | +# * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# * you may not use this file except in compliance with the License. |
| 11 | +# * You may obtain a copy of the License at |
| 12 | +# * |
| 13 | +# * |
| 14 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +# * |
| 16 | +# * Unless required by applicable law or agreed to in writing, software |
| 17 | +# * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +# * See the License for the specific language governing permissions and |
| 20 | +# * limitations under the License. |
| 21 | +# * |
| 22 | +#* ****************************************************************************** |
| 23 | + |
| 24 | +MY_PATH="$(realpath ${BASH_SOURCE[0]})" |
| 25 | +MY_DIR="$(dirname ${MY_PATH})" |
| 26 | + |
| 27 | +NO_COLOR="\e[0m" |
| 28 | +RED="\e[0;31m" |
| 29 | +CYAN="\e[0;36m" |
| 30 | +YELLOW="\e[1;33m" |
| 31 | +GREEN="\e[0;32m" |
| 32 | +RED_BOLD="\e[1;31m" |
| 33 | +BLUE_BOLD="\e[1;34m" |
| 34 | +YELLOW_BOLD="\e[1;33m" |
| 35 | + |
| 36 | +DEBUG_FLAG=0 |
| 37 | +function ECHO() |
| 38 | +{ |
| 39 | + echo -e "$*" |
| 40 | +} |
| 41 | + |
| 42 | +function DEBUG() |
| 43 | +{ |
| 44 | + # if set -x is in use debug messages are useless as whole stript will be shown |
| 45 | + if [[ "$-" =~ "x" ]]; then |
| 46 | + return |
| 47 | + fi |
| 48 | + if [[ "${DEBUG_FLAG}" == "1" ]];then |
| 49 | + ECHO "${BLUE_BOLD}DEBUG: ${CYAN}$*${NO_COLOR}" > /dev/stderr |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +function INFO() |
| 54 | +{ |
| 55 | + ECHO "${GREEN}$*${NO_COLOR}" |
| 56 | +} |
| 57 | + |
| 58 | +function WARNING() |
| 59 | +{ |
| 60 | + ECHO "${YELLOW_BOLD}Warning: ${YELLOW}$*${NO_COLOR}" > /dev/stderr |
| 61 | +} |
| 62 | + |
| 63 | +function ERROR() |
| 64 | +{ |
| 65 | + ECHO "${RED_BOLD}ERROR: ${RED}$*${NO_COLOR}" |
| 66 | + exit 1 |
| 67 | +} |
| 68 | + |
| 69 | +function version_check() |
| 70 | +{ |
| 71 | +# Check if a version is correct or not |
| 72 | +# Arguments: |
| 73 | +# $1: Version to check |
| 74 | +# $2: Required version |
| 75 | +# +: as the last character can be used to signify any version over the given number. |
| 76 | +# -: as the last character can be used to signify any version below the given number. |
| 77 | +# |
| 78 | + DEBUG "${FUNCNAME} $*" |
| 79 | + local check_version="$1" |
| 80 | + local required_version="$2" |
| 81 | + local check_version_split=(${check_version//\./" "}) |
| 82 | + local req_version_split=(${required_version//\./" "}) |
| 83 | + local stop=$(("${#req_version_split[@]}"-1)) |
| 84 | + for i in $(seq 0 ${stop}) |
| 85 | + do |
| 86 | + local req_version_section="${req_version_split[$i]}" |
| 87 | + DEBUG "Req Version Sect: [${req_version_section}]" |
| 88 | + local check_version_section="${check_version_split[$i]}" |
| 89 | + DEBUG "Check Version Sect: [${check_version_section}]" |
| 90 | + case "${req_version_section}" in |
| 91 | + *"+") |
| 92 | + # Remove the + from the end of the string |
| 93 | + req_version_section="${req_version_section%+}" |
| 94 | + if [[ "$check_version_section" -ge "${req_version_section}" ]];then |
| 95 | + return 1 |
| 96 | + fi |
| 97 | + return 0 |
| 98 | + ;; |
| 99 | + *"-") |
| 100 | + # Remove the - from end of the string |
| 101 | + req_version_section="${req_version_section%-}" |
| 102 | + if [[ "$check_version_section" -le "${req_version_section}" ]];then |
| 103 | + return 1 |
| 104 | + fi |
| 105 | + return 0 |
| 106 | + ;; |
| 107 | + *) |
| 108 | + if [[ "${check_version_section}" != "${req_version_section}" ]];then |
| 109 | + return 0 |
| 110 | + fi |
| 111 | + ;; |
| 112 | + esac |
| 113 | + done |
| 114 | + return 1 |
| 115 | +} |
| 116 | + |
| 117 | +function check_python_env() |
| 118 | +{ |
| 119 | + local installed=() |
| 120 | + local required=() |
| 121 | + readarray -t installed < <(python3 -m pip list | tail -n +3) |
| 122 | + readarray -t required < <(cat ${MY_DIR}/requirements.txt | sed 's/[<>=]/ /g') |
| 123 | + for req_pkg in "${required[@]}" |
| 124 | + do |
| 125 | + local match="" |
| 126 | + local req_pkg_array=($(echo "${req_pkg}" | awk '{print $1" "$NF}')) |
| 127 | + for installed_pkg in "${installed[@]}" |
| 128 | + do |
| 129 | + local installed_name="$(echo ${installed_pkg}| awk '{print $1}')" |
| 130 | + if [[ "${req_pkg_array[0]}" == "${installed_name}" ]];then |
| 131 | + match=${installed_pkg} |
| 132 | + break |
| 133 | + fi |
| 134 | + done |
| 135 | + if [[ -n "${match}" ]]; then |
| 136 | + #Package is installed, check the version |
| 137 | + match_version="$(echo ${match} | awk '{print $NF}')" |
| 138 | + if (version_check "${match_version}" "${req_pkg_array[1]}+");then |
| 139 | + WARNING "Package [${req_pkg_array[0]}] is required to be at verison [${req_pkg_array[1]}+] but is at [${match_version}]" |
| 140 | + return 0 |
| 141 | + fi |
| 142 | + else |
| 143 | + WARNING "Package [${req_pkg_array[0]}] is not installed" |
| 144 | + return 0 |
| 145 | + fi |
| 146 | + done |
| 147 | + return 1 |
| 148 | +} |
| 149 | + |
| 150 | +function create_python_venv() |
| 151 | +{ |
| 152 | + local venv_dir="${MY_DIR}/python_venv" |
| 153 | + local create_env=1 |
| 154 | + if [[ -e "${venv_dir}" ]];then |
| 155 | + if [[ -e "${venv_dir}/bin/activate" ]];then |
| 156 | + create_env=0 |
| 157 | + else |
| 158 | + ERROR "${venv_dir} already exists but is not a python virtual environment.\n \ |
| 159 | + please rename or delete it" |
| 160 | + fi |
| 161 | + fi |
| 162 | + if [[ "${create_env}" ]];then |
| 163 | + mkdir -p "${venv_dir}" |
| 164 | + python3 -m venv "${venv_dir}" |
| 165 | + fi |
| 166 | + source "${venv_dir}/bin/activate" |
| 167 | + pip install -qr "${MY_DIR}/requirements.txt" 2>&1 >/dev/null |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +### MAIN ### |
| 172 | +# Check the python3 version is correct |
| 173 | +python_version="$(python3 --version | awk '{print $NF}')" |
| 174 | +if [[ -n "${python_version}" ]];then |
| 175 | + if (version_check "${python_version}" "3.10+");then |
| 176 | + ERROR "Installed python3 version is [${python_version}], but version 3.10+ is required" |
| 177 | + fi |
| 178 | +else |
| 179 | + ERROR "python3 is not installed. python 3.10+ is required" |
| 180 | +fi |
| 181 | + |
| 182 | +# Check the python environment has the packages required |
| 183 | +if (check_python_env);then |
| 184 | + WARNING "Python environment incorrect, creating virtual environment" |
| 185 | + create_python_venv |
| 186 | +fi |
| 187 | + |
| 188 | +# Build the xts_core binary |
| 189 | +pyinstaller -F "${MY_DIR}/src/xts.py" --distpath "${MY_DIR}/bin/" > "${MY_DIR}"/build.log 2>&1 |
| 190 | + |
| 191 | +INFO "Please run the following commands:\n" |
| 192 | +ECHO "export PATH=\"${MY_DIR}/bin:\$PATH\"" |
| 193 | +INFO "\nTo permanently install this, add the export command as a line in your ~/.bashrc file" |
0 commit comments