-
Notifications
You must be signed in to change notification settings - Fork 12
/
bootstrap.sh
150 lines (128 loc) · 4.82 KB
/
bootstrap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright 2020 The Pigweed Authors
#
# 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
#
# https://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.
# This script must be tested on bash, zsh, and dash.
_pw_abspath () {
python -c "import os.path; print(os.path.abspath('$@'))"
}
_pw_red() {
echo -e "\033[0;31m$*\033[0m"
}
_pw_bold_red() {
echo -e "\033[1;31m$*\033[0m"
}
_pw_green() {
echo -e "\033[0;32m$*\033[0m"
}
_pw_bright_magenta() {
echo -e "\033[0;35m$*\033[0m"
}
_PIGWEED_BANNER=$(cat <<EOF
▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█ ▒█ ▀ ▒█ ▀ ▒█ ▀█▌
▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█ ▒███ ▒███ ░█ █▌
▒█▀ ░█░ ▓█ █▓ ░█░ █ ▒█ ▒█ ▄ ▒█ ▄ ░█ ▄█▌
▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
EOF
)
# Users are not expected to set PW_CHECKOUT_ROOT, it's only used because it
# seems to be impossible to reliably determine the path to a sourced file in
# dash when sourced from a dash script instead of a dash interactive prompt.
# To reinforce that users should not be using PW_CHECKOUT_ROOT, it is cleared
# here after it is used, and other pw tools will complain if they see that
# variable set.
# TODO(mohrr) find out a way to do this without PW_CHECKOUT_ROOT.
if test -n "$PW_CHECKOUT_ROOT"; then
PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$PW_CHECKOUT_ROOT/bootstrap.sh")"
unset PW_CHECKOUT_ROOT
# Shell: bash.
elif test -n "$BASH"; then
PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$BASH_SOURCE")"
# Shell: zsh.
elif test -n "$ZSH_NAME"; then
PW_SETUP_SCRIPT_PATH="$(_pw_abspath "${(%):-%N}")"
# Shell: dash.
elif test ${0##*/} = dash; then
PW_SETUP_SCRIPT_PATH="$(_pw_abspath \
"$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
# If everything else fails, try $0. It could work.
else
PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$0")"
fi
PW_ROOT="$(dirname "$PW_SETUP_SCRIPT_PATH")"
if [[ "$PW_ROOT" = *" "* ]]; then
_pw_bold_red "Error: The Pigweed path contains spaces\n"
_pw_red " The path '$PW_ROOT' contains spaces. "
_pw_red " Pigweed's Python environment currently requires Pigweed to be "
_pw_red " at a path without spaces. Please checkout Pigweed in a directory "
_pw_red " without spaces and retry running bootstrap."
return
fi
export PW_ROOT
SETUP_SH="$PW_ROOT/pw_env_setup/.env_setup.sh"
if [ -z "$PW_ENVSETUP_QUIET" ]; then
_pw_green "\n WELCOME TO...\n"
_pw_bright_magenta "$_PIGWEED_BANNER\n"
fi
# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
[ "$(basename "$PW_SETUP_SCRIPT_PATH")" = "bootstrap.sh" ] || \
[ ! -f "$SETUP_SH" ] || \
[ ! -s "$SETUP_SH" ]
_PW_IS_BOOTSTRAP="$?"
if [ "$_PW_IS_BOOTSTRAP" -eq 0 ]; then
_PW_NAME="bootstrap"
if [ -z "$PW_ENVSETUP_QUIET" ]; then
_pw_green " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n"
fi
# Allow forcing a specific version of Python for testing pursposes.
if [ -n "$PW_BOOTSTRAP_PYTHON" ]; then
PYTHON="$PW_BOOTSTRAP_PYTHON"
elif which python &> /dev/null; then
PYTHON=python
else
_pw_bold_red "Error: No system Python present\n"
_pw_red " Pigweed's bootstrap process requires a local system Python."
_pw_red " Please install Python on your system, add it to your PATH"
_pw_red " and re-try running bootstrap."
return
fi
"$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH"
else
_PW_NAME="activate"
if [ -z "$PW_ENVSETUP_QUIET" ]; then
_pw_green " ACTIVATOR! This sets your shell environment variables.\n"
fi
fi
if [ -f "$SETUP_SH" ]; then
. "$SETUP_SH"
if [ "$?" -eq 0 ]; then
if [ "$_PW_IS_BOOTSTRAP" -eq 0 ] && [ -z "$PW_ENVSETUP_QUIET" ]; then
echo "To activate this environment in the future, run this in your "
echo "terminal:"
echo
_pw_green " . ./activate.sh\n"
fi
else
_pw_red "Error during $_PW_NAME--see messages above."
fi
else
_pw_red "Error during $_PW_NAME--see messages above."
fi
unset _PW_IS_BOOTSTRAP
unset _PW_NAME
unset _PIGWEED_BANNER
unset _pw_abspath
unset _pw_red
unset _pw_bold_red
unset _pw_green
unset _pw_bright_magenta