Rob Mohr | 2cc32ec | 2020-02-05 10:55:12 -0800 | [diff] [blame] | 1 | # Copyright 2020 The Pigweed Authors |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 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 | # This script must be tested on bash, zsh, and dash. |
| 16 | |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 17 | _pw_abspath () { |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 18 | python -c "import os.path; print(os.path.abspath('$@'))" |
Rob Mohr | dc70d1c | 2019-12-04 10:05:28 -0800 | [diff] [blame] | 19 | } |
| 20 | |
Keir Mierle | a60b207 | 2020-06-11 23:28:18 -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 | |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 34 | _pw_red() { |
Alexei Frolov | 0246148 | 2020-03-09 10:19:49 -0700 | [diff] [blame] | 35 | echo -e "\033[0;31m$*\033[0m" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | _pw_bold_red() { |
Alexei Frolov | 0246148 | 2020-03-09 10:19:49 -0700 | [diff] [blame] | 39 | echo -e "\033[1;31m$*\033[0m" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Keir Mierle | a60b207 | 2020-06-11 23:28:18 -0700 | [diff] [blame] | 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 | |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 50 | _pw_green() { |
Alexei Frolov | 0246148 | 2020-03-09 10:19:49 -0700 | [diff] [blame] | 51 | echo -e "\033[0;32m$*\033[0m" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Keir Mierle | a60b207 | 2020-06-11 23:28:18 -0700 | [diff] [blame] | 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() { |
Alexei Frolov | 0246148 | 2020-03-09 10:19:49 -0700 | [diff] [blame] | 67 | echo -e "\033[0;35m$*\033[0m" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Keir Mierle | a60b207 | 2020-06-11 23:28:18 -0700 | [diff] [blame] | 70 | _pw_bold_white() { |
| 71 | echo -e "\033[1;37m$*\033[0m" |
| 72 | } |
| 73 | |
| 74 | # Note: This banner is duplicated in three places; which is a lesser evil than |
| 75 | # the contortions that would be needed to share this snippet acros shell, |
| 76 | # batch, and Python. Locations: |
| 77 | # |
| 78 | # - bootstrap.sh |
| 79 | # - pw_cli/branding.py |
| 80 | # - pw_env_setup/py/pw_env_setup/windows_env_start.py |
| 81 | # |
| 82 | _PW_BANNER=$(cat <<EOF |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 83 | ▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄ |
| 84 | ▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█ ▒█ ▀ ▒█ ▀ ▒█ ▀█▌ |
| 85 | ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█ ▒███ ▒███ ░█ █▌ |
| 86 | ▒█▀ ░█░ ▓█ █▓ ░█░ █ ▒█ ▒█ ▄ ▒█ ▄ ░█ ▄█▌ |
| 87 | ▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀ |
| 88 | EOF |
| 89 | ) |
| 90 | |
Keir Mierle | a60b207 | 2020-06-11 23:28:18 -0700 | [diff] [blame] | 91 | # Support customizing the branding with a different banner and color. |
| 92 | if test -f "$PW_BRANDING_BANNER"; then |
| 93 | _PW_BANNER=$(cat $PW_BRANDING_BANNER) |
| 94 | fi |
| 95 | if test -z "$PW_BRANDING_BANNER_COLOR"; then |
| 96 | PW_BRANDING_BANNER_COLOR=magenta |
| 97 | fi |
| 98 | |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 99 | # Users are not expected to set PW_CHECKOUT_ROOT, it's only used because it |
| 100 | # seems to be impossible to reliably determine the path to a sourced file in |
| 101 | # dash when sourced from a dash script instead of a dash interactive prompt. |
| 102 | # To reinforce that users should not be using PW_CHECKOUT_ROOT, it is cleared |
| 103 | # here after it is used, and other pw tools will complain if they see that |
| 104 | # variable set. |
| 105 | # TODO(mohrr) find out a way to do this without PW_CHECKOUT_ROOT. |
| 106 | if test -n "$PW_CHECKOUT_ROOT"; then |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 107 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$PW_CHECKOUT_ROOT/bootstrap.sh")" |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 108 | unset PW_CHECKOUT_ROOT |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 109 | # Shell: bash. |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 110 | elif test -n "$BASH"; then |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 111 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$BASH_SOURCE")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 112 | # Shell: zsh. |
| 113 | elif test -n "$ZSH_NAME"; then |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 114 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath "${(%):-%N}")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 115 | # Shell: dash. |
| 116 | elif test ${0##*/} = dash; then |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 117 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath \ |
| 118 | "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 119 | # If everything else fails, try $0. It could work. |
| 120 | else |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 121 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$0")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 122 | fi |
| 123 | |
Rob Mohr | 6ac3f7d | 2020-03-27 12:43:28 -0700 | [diff] [blame] | 124 | # Check if this file is being executed or sourced. |
| 125 | _pw_sourced=0 |
| 126 | if [ -n "$SWARMING_BOT_ID" ]; then |
| 127 | # If set we're running on swarming and don't need this check. |
| 128 | _pw_sourced=1 |
| 129 | elif [ -n "$ZSH_EVAL_CONTEXT" ]; then |
| 130 | case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac |
| 131 | elif [ -n "$KSH_VERSION" ]; then |
| 132 | [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \ |
| 133 | "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \ |
| 134 | && _pw_sourced=1 |
| 135 | elif [ -n "$BASH_VERSION" ]; then |
| 136 | (return 0 2>/dev/null) && _pw_sourced=1 |
| 137 | else # All other shells: examine $0 for known shell binary filenames |
| 138 | # Detects `sh` and `dash`; add additional shell filenames as needed. |
| 139 | case ${0##*/} in sh|dash) _pw_sourced=1;; esac |
| 140 | fi |
| 141 | |
| 142 | if [ "$_pw_sourced" -eq 0 ]; then |
| 143 | _PW_NAME=$(basename "$PW_SETUP_SCRIPT_PATH" .sh) |
| 144 | _pw_bold_red "Error: Attempting to $_PW_NAME in a subshell" |
| 145 | _pw_red " Since $_PW_NAME.sh modifies your shell's environment variables, it" |
| 146 | _pw_red " must be sourced rather than executed. In particular, " |
| 147 | _pw_red " 'bash $_PW_NAME.sh' will not work since the modified environment " |
| 148 | _pw_red " will get destroyed at the end of the script. Instead, source the " |
| 149 | _pw_red " script's contents in your shell:" |
| 150 | _pw_red "" |
| 151 | _pw_red " \$ source $_PW_NAME.sh" |
| 152 | exit 1 |
| 153 | fi |
| 154 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 155 | PW_ROOT="$(dirname "$PW_SETUP_SCRIPT_PATH")" |
| 156 | |
| 157 | if [[ "$PW_ROOT" = *" "* ]]; then |
| 158 | _pw_bold_red "Error: The Pigweed path contains spaces\n" |
| 159 | _pw_red " The path '$PW_ROOT' contains spaces. " |
| 160 | _pw_red " Pigweed's Python environment currently requires Pigweed to be " |
| 161 | _pw_red " at a path without spaces. Please checkout Pigweed in a directory " |
| 162 | _pw_red " without spaces and retry running bootstrap." |
| 163 | return |
| 164 | fi |
| 165 | |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 166 | export PW_ROOT |
| 167 | |
Rob Mohr | 3352ccf | 2020-03-11 14:01:18 -0700 | [diff] [blame] | 168 | SETUP_SH="$PW_ROOT/pw_env_setup/.env_setup.sh" |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 169 | |
Wyatt Hepler | 5009a6f | 2020-04-28 18:29:45 -0700 | [diff] [blame] | 170 | if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 171 | _pw_green "\n WELCOME TO...\n" |
Keir Mierle | a60b207 | 2020-06-11 23:28:18 -0700 | [diff] [blame] | 172 | "_pw_$PW_BRANDING_BANNER_COLOR" "$_PW_BANNER\n" |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 173 | fi |
Rob Mohr | de7c77a | 2020-01-16 13:51:44 -0800 | [diff] [blame] | 174 | |
Rob Mohr | 5898a9d | 2020-01-22 13:54:43 -0800 | [diff] [blame] | 175 | # Run full bootstrap when invoked as bootstrap, or env file is missing/empty. |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 176 | [ "$(basename "$PW_SETUP_SCRIPT_PATH")" = "bootstrap.sh" ] || \ |
| 177 | [ ! -f "$SETUP_SH" ] || \ |
| 178 | [ ! -s "$SETUP_SH" ] |
| 179 | _PW_IS_BOOTSTRAP="$?" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 180 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 181 | if [ "$_PW_IS_BOOTSTRAP" -eq 0 ]; then |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 182 | _PW_NAME="bootstrap" |
| 183 | |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 184 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 185 | _pw_green " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n" |
| 186 | fi |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 187 | |
Alexei Frolov | 426b9b8 | 2020-03-09 14:30:22 -0700 | [diff] [blame] | 188 | # Allow forcing a specific version of Python for testing pursposes. |
| 189 | if [ -n "$PW_BOOTSTRAP_PYTHON" ]; then |
| 190 | PYTHON="$PW_BOOTSTRAP_PYTHON" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 191 | elif which python &> /dev/null; then |
| 192 | PYTHON=python |
| 193 | else |
| 194 | _pw_bold_red "Error: No system Python present\n" |
| 195 | _pw_red " Pigweed's bootstrap process requires a local system Python." |
| 196 | _pw_red " Please install Python on your system, add it to your PATH" |
| 197 | _pw_red " and re-try running bootstrap." |
| 198 | return |
| 199 | fi |
| 200 | |
Rob Mohr | d596a53 | 2020-04-03 08:59:12 -0700 | [diff] [blame] | 201 | _PW_OLD_CIPD_PACKAGE_FILES="$PW_CIPD_PACKAGE_FILES" |
| 202 | PW_CIPD_PACKAGE_FILES="$PW_ROOT/pw_env_setup/py/pw_env_setup/cipd_setup/*.json:$PW_CIPD_PACKAGE_FILES" |
| 203 | export PW_CIPD_PACKAGE_FILES |
| 204 | |
| 205 | _PW_OLD_VIRTUALENV_REQUIREMENTS="$PW_VIRTUALENV_REQUIREMENTS" |
| 206 | PW_VIRTUALENV_REQUIREMENTS="$PW_ROOT/pw_env_setup/py/pw_env_setup/virtualenv_setup/requirements.txt:$PW_VIRTUALENV_REQUIREMENTS" |
| 207 | export PW_VIRTUALENV_REQUIREMENTS |
| 208 | |
| 209 | _PW_OLD_VIRTUALENV_SETUP_PY_ROOTS="$PW_VIRTUALENV_SETUP_PY_ROOTS" |
| 210 | PW_VIRTUALENV_SETUP_PY_ROOTS="$PW_ROOT/*:$PW_VIRTUALENV_SETUP_PY_ROOTS" |
| 211 | export PW_VIRTUALENV_SETUP_PY_ROOTS |
| 212 | |
| 213 | _PW_OLD_CARGO_PACKAGE_FILES="$PW_CARGO_PACKAGE_FILES" |
| 214 | PW_CARGO_PACKAGE_FILES="$PW_ROOT/pw_env_setup/py/pw_env_setup/cargo_setup/packages.txt:$PW_CARGO_PACKAGE_FILES" |
| 215 | export PW_CARGO_PACKAGE_FILES |
| 216 | |
Rob Mohr | 206959a | 2020-05-12 07:08:20 -0700 | [diff] [blame] | 217 | if [ -n "$PW_USE_GCS_ENVSETUP" ]; then |
| 218 | _PW_ENV_SETUP="$("$PW_ROOT/pw_env_setup/get_pw_env_setup.sh")" |
| 219 | fi |
| 220 | |
| 221 | if [ -n "$_PW_ENV_SETUP" ]; then |
| 222 | "$_PW_ENV_SETUP" --shell-file "$SETUP_SH" |
| 223 | else |
Rob Mohr | 1908715 | 2020-05-08 10:06:39 -0700 | [diff] [blame] | 224 | "$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH" |
Rob Mohr | 206959a | 2020-05-12 07:08:20 -0700 | [diff] [blame] | 225 | fi |
Rob Mohr | d596a53 | 2020-04-03 08:59:12 -0700 | [diff] [blame] | 226 | |
| 227 | PW_CIPD_PACKAGE_FILES="$_PW_OLD_CIPD_PACKAGE_FILES" |
| 228 | PW_VIRTUALENV_REQUIREMENTS="$_PW_OLD_VIRTUALENV_REQUIREMENTS" |
| 229 | PW_VIRTUALENV_SETUP_PY_ROOTS="$_PW_OLD_VIRTUALENV_SETUP_PY_ROOTS" |
| 230 | PW_CARGO_PACKAGE_FILES="$_PW_OLD_CARGO_PACKAGE_FILES" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 231 | else |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 232 | _PW_NAME="activate" |
| 233 | |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 234 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 235 | _pw_green " ACTIVATOR! This sets your shell environment variables.\n" |
| 236 | fi |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 237 | fi |
Rob Mohr | dc70d1c | 2019-12-04 10:05:28 -0800 | [diff] [blame] | 238 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 239 | if [ -f "$SETUP_SH" ]; then |
| 240 | . "$SETUP_SH" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 241 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 242 | if [ "$?" -eq 0 ]; then |
| 243 | if [ "$_PW_IS_BOOTSTRAP" -eq 0 ] && [ -z "$PW_ENVSETUP_QUIET" ]; then |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 244 | echo "To activate this environment in the future, run this in your " |
| 245 | echo "terminal:" |
| 246 | echo |
Rob Mohr | 6ac3f7d | 2020-03-27 12:43:28 -0700 | [diff] [blame] | 247 | _pw_green " source ./activate.sh\n" |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 248 | fi |
| 249 | else |
| 250 | _pw_red "Error during $_PW_NAME--see messages above." |
Rob Mohr | 47bd923 | 2020-03-13 12:07:26 -0700 | [diff] [blame] | 251 | fi |
| 252 | else |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 253 | _pw_red "Error during $_PW_NAME--see messages above." |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 254 | fi |
| 255 | |
Rob Mohr | 1908715 | 2020-05-08 10:06:39 -0700 | [diff] [blame] | 256 | unset _PW_ENV_SETUP |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 257 | unset _PW_IS_BOOTSTRAP |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 258 | unset _PW_NAME |
Keir Mierle | a60b207 | 2020-06-11 23:28:18 -0700 | [diff] [blame] | 259 | unset _PW_BANNER |
Rob Mohr | d596a53 | 2020-04-03 08:59:12 -0700 | [diff] [blame] | 260 | unset _PW_OLD_CIPD_PACKAGE_FILES |
| 261 | unset _PW_OLD_VIRTUALENV_REQUIREMENTS |
| 262 | unset _PW_OLD_VIRTUALENV_SETUP_PY_ROOTS |
| 263 | unset _PW_OLD_CARGO_PACKAGE_FILES |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 264 | unset _pw_abspath |
| 265 | unset _pw_red |
| 266 | unset _pw_bold_red |
| 267 | unset _pw_green |
Keir Mierle | a60b207 | 2020-06-11 23:28:18 -0700 | [diff] [blame] | 268 | unset _pw_magenta |
Rob Mohr | 6ac3f7d | 2020-03-27 12:43:28 -0700 | [diff] [blame] | 269 | unset _pw_sourced |