Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 1 | # Copyright 2020 The Pigweed Authors |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | # use this file except in compliance with the License. You may obtain a copy of |
| 5 | # the License at |
| 6 | # |
| 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations under |
| 13 | # the License. |
| 14 | |
| 15 | _pw_abspath () { |
| 16 | python -c "import os.path; print(os.path.abspath('$@'))" |
| 17 | } |
| 18 | |
Rob Mohr | f5712ee | 2020-11-09 09:39:52 -0800 | [diff] [blame] | 19 | # Just in case PATH isn't already exported. |
| 20 | export PATH |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 21 | |
| 22 | # Note: Colors are unfortunately duplicated in several places; and removing the |
| 23 | # duplication is not easy. Their locations are: |
| 24 | # |
| 25 | # - bootstrap.sh |
| 26 | # - pw_cli/color.py |
| 27 | # - pw_env_setup/py/pw_env_setup/colors.py |
| 28 | # |
| 29 | # So please keep them matching then modifying them. |
| 30 | pw_none() { |
| 31 | echo -e "$*" |
| 32 | } |
| 33 | |
| 34 | pw_red() { |
| 35 | echo -e "\033[0;31m$*\033[0m" |
| 36 | } |
| 37 | |
| 38 | pw_bold_red() { |
| 39 | echo -e "\033[1;31m$*\033[0m" |
| 40 | } |
| 41 | |
| 42 | pw_yellow() { |
| 43 | echo -e "\033[0;33m$*\033[0m" |
| 44 | } |
| 45 | |
| 46 | pw_bold_yellow() { |
| 47 | echo -e "\033[1;33m$*\033[0m" |
| 48 | } |
| 49 | |
| 50 | pw_green() { |
| 51 | echo -e "\033[0;32m$*\033[0m" |
| 52 | } |
| 53 | |
| 54 | pw_bold_green() { |
| 55 | echo -e "\033[1;32m$*\033[0m" |
| 56 | } |
| 57 | |
| 58 | pw_blue() { |
| 59 | echo -e "\033[1;34m$*\033[0m" |
| 60 | } |
| 61 | |
| 62 | pw_cyan() { |
| 63 | echo -e "\033[1;36m$*\033[0m" |
| 64 | } |
| 65 | |
| 66 | pw_magenta() { |
| 67 | echo -e "\033[0;35m$*\033[0m" |
| 68 | } |
| 69 | |
| 70 | pw_bold_white() { |
| 71 | echo -e "\033[1;37m$*\033[0m" |
| 72 | } |
| 73 | |
| 74 | pw_eval_sourced() { |
| 75 | if [ "$1" -eq 0 ]; then |
| 76 | _PW_NAME=$(basename "$PW_SETUP_SCRIPT_PATH" .sh) |
| 77 | pw_bold_red "Error: Attempting to $_PW_NAME in a subshell" |
| 78 | pw_red " Since $_PW_NAME.sh modifies your shell's environment variables," |
| 79 | pw_red " it must be sourced rather than executed. In particular, " |
| 80 | pw_red " 'bash $_PW_NAME.sh' will not work since the modified " |
| 81 | pw_red " environment will get destroyed at the end of the script. " |
| 82 | pw_red " Instead, source the script's contents in your shell:" |
| 83 | pw_red "" |
| 84 | pw_red " \$ source $_PW_NAME.sh" |
| 85 | exit 1 |
| 86 | fi |
| 87 | } |
| 88 | |
| 89 | pw_check_root() { |
| 90 | _PW_ROOT="$1" |
| 91 | if [[ "$_PW_ROOT" = *" "* ]]; then |
| 92 | pw_bold_red "Error: The Pigweed path contains spaces\n" |
| 93 | pw_red " The path '$_PW_ROOT' contains spaces. " |
| 94 | pw_red " Pigweed's Python environment currently requires Pigweed to be " |
| 95 | pw_red " at a path without spaces. Please checkout Pigweed in a " |
| 96 | pw_red " directory without spaces and retry running bootstrap." |
| 97 | return |
| 98 | fi |
| 99 | } |
| 100 | |
| 101 | pw_get_env_root() { |
| 102 | # PW_ENVIRONMENT_ROOT allows developers to specify where the environment |
| 103 | # should be installed. bootstrap.sh scripts should not use that variable to |
| 104 | # store the result of this function. This separation allows scripts to assume |
| 105 | # PW_ENVIRONMENT_ROOT came from the developer and not from a previous |
| 106 | # bootstrap possibly from another workspace. |
| 107 | if [ -z "$PW_ENVIRONMENT_ROOT" ]; then |
Rob Mohr | 582586b | 2020-11-13 12:33:06 -0800 | [diff] [blame] | 108 | if [ -n "$PW_PROJECT_ROOT" ]; then |
| 109 | echo "$PW_PROJECT_ROOT/.environment" |
| 110 | else |
| 111 | echo "$PW_ROOT/.environment" |
| 112 | fi |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 113 | else |
| 114 | echo "$PW_ENVIRONMENT_ROOT" |
| 115 | fi |
| 116 | } |
| 117 | |
| 118 | # Note: This banner is duplicated in three places; which is a lesser evil than |
| 119 | # the contortions that would be needed to share this snippet across shell, |
| 120 | # batch, and Python. Locations: |
| 121 | # |
| 122 | # - pw_env_setup/util.sh |
| 123 | # - pw_cli/branding.py |
| 124 | # - pw_env_setup/py/pw_env_setup/windows_env_start.py |
| 125 | # |
| 126 | _PW_BANNER=$(cat <<EOF |
| 127 | ▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄ |
| 128 | ▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█ ▒█ ▀ ▒█ ▀ ▒█ ▀█▌ |
| 129 | ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█ ▒███ ▒███ ░█ █▌ |
| 130 | ▒█▀ ░█░ ▓█ █▓ ░█░ █ ▒█ ▒█ ▄ ▒█ ▄ ░█ ▄█▌ |
| 131 | ▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀ |
| 132 | EOF |
| 133 | ) |
| 134 | |
| 135 | _pw_banner() { |
| 136 | if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then |
| 137 | pw_magenta "$_PW_BANNER\n" |
| 138 | fi |
| 139 | } |
| 140 | |
| 141 | _PW_BANNER_FUNC="_pw_banner" |
| 142 | |
| 143 | _pw_hello() { |
| 144 | _PW_TEXT="$1" |
| 145 | if [ -n "$PW_BANNER_FUNC" ]; then |
| 146 | _PW_BANNER_FUNC="$PW_BANNER_FUNC" |
| 147 | fi |
| 148 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 149 | pw_green "\n WELCOME TO...\n" |
| 150 | "$_PW_BANNER_FUNC" |
| 151 | pw_green "$_PW_TEXT" |
| 152 | fi |
| 153 | } |
| 154 | |
Rob Mohr | 428e479 | 2020-10-27 11:46:43 -0700 | [diff] [blame] | 155 | pw_deactivate() { |
Rob Mohr | 926bb60 | 2020-11-05 14:08:04 -0800 | [diff] [blame] | 156 | # Assume PW_ROOT and PW_PROJECT_ROOT has already been set and we need to |
| 157 | # preserve their values. |
Rob Mohr | 428e479 | 2020-10-27 11:46:43 -0700 | [diff] [blame] | 158 | _NEW_PW_ROOT="$PW_ROOT" |
Rob Mohr | 926bb60 | 2020-11-05 14:08:04 -0800 | [diff] [blame] | 159 | _NEW_PW_PROJECT_ROOT="$PW_PROJECT_ROOT" |
Rob Mohr | 428e479 | 2020-10-27 11:46:43 -0700 | [diff] [blame] | 160 | |
| 161 | # Find deactivate script and run it. |
| 162 | _PW_DEACTIVATE_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/deactivate.sh" |
| 163 | if [ -f "$_PW_DEACTIVATE_SH" ]; then |
| 164 | . "$_PW_DEACTIVATE_SH" |
| 165 | fi |
| 166 | |
Rob Mohr | 061f972 | 2020-10-30 16:06:52 -0700 | [diff] [blame] | 167 | # If there's a _pw_deactivate function run it. Redirect output to /dev/null |
| 168 | # in case _pw_deactivate doesn't exist. |
Rob Mohr | c0a8e7b | 2020-11-02 15:22:38 -0800 | [diff] [blame] | 169 | if [ -n "$(command -v _pw_deactivate)" ]; then |
| 170 | _pw_deactivate &> /dev/null |
| 171 | fi |
Rob Mohr | 061f972 | 2020-10-30 16:06:52 -0700 | [diff] [blame] | 172 | |
Rob Mohr | 926bb60 | 2020-11-05 14:08:04 -0800 | [diff] [blame] | 173 | # Restore. |
Rob Mohr | 428e479 | 2020-10-27 11:46:43 -0700 | [diff] [blame] | 174 | PW_ROOT="$_NEW_PW_ROOT" |
| 175 | export PW_ROOT |
Rob Mohr | 926bb60 | 2020-11-05 14:08:04 -0800 | [diff] [blame] | 176 | PW_PROJECT_ROOT="$_NEW_PW_PROJECT_ROOT" |
| 177 | export PW_PROJECT_ROOT |
Rob Mohr | 428e479 | 2020-10-27 11:46:43 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 180 | # The next three functions use the following variables. |
| 181 | # * PW_BANNER_FUNC: function to print banner |
| 182 | # * PW_BOOTSTRAP_PYTHON: specific Python interpreter to use for bootstrap |
| 183 | # * PW_USE_GCS_ENVSETUP: attempt to grab env setup executable from GCS if "true" |
| 184 | # * PW_ROOT: path to Pigweed root |
| 185 | # * PW_ENVSETUP_QUIET: limit output if "true" |
| 186 | # |
| 187 | # All arguments passed in are passed on to env_setup.py in pw_bootstrap, |
| 188 | # pw_activate takes no arguments, and pw_finalize takes the name of the script |
| 189 | # "bootstrap" or "activate" and the path to the setup script written by |
| 190 | # bootstrap.sh. |
| 191 | pw_bootstrap() { |
| 192 | _pw_hello " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n" |
| 193 | |
| 194 | # Allow forcing a specific version of Python for testing pursposes. |
| 195 | if [ -n "$PW_BOOTSTRAP_PYTHON" ]; then |
| 196 | _PW_PYTHON="$PW_BOOTSTRAP_PYTHON" |
| 197 | elif which python &> /dev/null; then |
| 198 | _PW_PYTHON=python |
| 199 | else |
| 200 | pw_bold_red "Error: No system Python present\n" |
| 201 | pw_red " Pigweed's bootstrap process requires a local system Python." |
| 202 | pw_red " Please install Python on your system, add it to your PATH" |
| 203 | pw_red " and re-try running bootstrap." |
| 204 | return |
| 205 | fi |
| 206 | |
| 207 | if [ -n "$PW_USE_GCS_ENVSETUP" ]; then |
| 208 | _PW_ENV_SETUP="$("$PW_ROOT/pw_env_setup/get_pw_env_setup.sh")" |
| 209 | fi |
| 210 | |
| 211 | if [ -n "$_PW_ENV_SETUP" ]; then |
| 212 | "$_PW_ENV_SETUP" "$@" |
Rob Mohr | 4f59add | 2021-03-04 10:22:07 -0800 | [diff] [blame] | 213 | _PW_ENV_SETUP_STATUS="$?" |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 214 | else |
| 215 | "$_PW_PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" "$@" |
Rob Mohr | 4f59add | 2021-03-04 10:22:07 -0800 | [diff] [blame] | 216 | _PW_ENV_SETUP_STATUS="$?" |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 217 | fi |
| 218 | } |
| 219 | |
| 220 | pw_activate() { |
| 221 | _pw_hello " ACTIVATOR! This sets your shell environment variables.\n" |
Rob Mohr | 4f59add | 2021-03-04 10:22:07 -0800 | [diff] [blame] | 222 | _PW_ENV_SETUP_STATUS=0 |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | pw_finalize() { |
| 226 | _PW_NAME="$1" |
| 227 | _PW_SETUP_SH="$2" |
Rob Mohr | 4f59add | 2021-03-04 10:22:07 -0800 | [diff] [blame] | 228 | |
| 229 | if [ "$_PW_ENV_SETUP_STATUS" -ne 0 ]; then |
| 230 | return |
| 231 | fi |
| 232 | |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 233 | if [ -f "$_PW_SETUP_SH" ]; then |
| 234 | . "$_PW_SETUP_SH" |
| 235 | |
| 236 | if [ "$?" -eq 0 ]; then |
| 237 | if [ "$_PW_NAME" = "bootstrap" ] && [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 238 | echo "To activate this environment in the future, run this in your " |
| 239 | echo "terminal:" |
| 240 | echo |
| 241 | pw_green " source ./activate.sh\n" |
| 242 | fi |
| 243 | else |
| 244 | pw_red "Error during $_PW_NAME--see messages above." |
| 245 | fi |
| 246 | else |
| 247 | pw_red "Error during $_PW_NAME--see messages above." |
| 248 | fi |
| 249 | } |
| 250 | |
| 251 | pw_cleanup() { |
| 252 | unset _PW_BANNER |
| 253 | unset _PW_BANNER_FUNC |
| 254 | unset _PW_ENV_SETUP |
| 255 | unset _PW_NAME |
| 256 | unset _PW_PYTHON |
| 257 | unset _PW_SETUP_SH |
Rob Mohr | 428e479 | 2020-10-27 11:46:43 -0700 | [diff] [blame] | 258 | unset _PW_DEACTIVATE_SH |
| 259 | unset _NEW_PW_ROOT |
Rob Mohr | 4f59add | 2021-03-04 10:22:07 -0800 | [diff] [blame] | 260 | unset _PW_ENV_SETUP_STATUS |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 261 | |
| 262 | unset _pw_abspath |
| 263 | unset pw_none |
| 264 | unset pw_red |
| 265 | unset pw_bold_red |
| 266 | unset pw_yellow |
| 267 | unset pw_bold_yellow |
| 268 | unset pw_green |
| 269 | unset pw_bold_green |
| 270 | unset pw_blue |
| 271 | unset pw_cyan |
| 272 | unset pw_magenta |
| 273 | unset pw_bold_white |
| 274 | unset pw_eval_sourced |
| 275 | unset pw_check_root |
| 276 | unset pw_get_env_root |
| 277 | unset _pw_banner |
| 278 | unset pw_bootstrap |
| 279 | unset pw_activate |
| 280 | unset pw_finalize |
| 281 | unset _pw_cleanup |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 282 | } |