This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
setup.sh
executable file
·346 lines (308 loc) · 9.35 KB
/
setup.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/bin/bash
#
# Copyright (C) 2016 Wind River Systems, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
if [ -z "${BASH_VERSION}" ]; then
echo "This script must be run with bash." >&2
exit 1
fi
which python3 > /dev/null
if [ $? -ne 0 ]; then
echo >&2 "WRLinux setup requires 'python3'."
echo >&2 "Please install python3."
exit 1
fi
if [ `id -u` = 0 ]; then
echo >&2 "Do not run setup as root."
exit 1
fi
: "${GIT_USERNAME=customer}"
: "${[email protected]}"
# Requires python3
CMD="bin/setup.py"
# Adds arguments to the arg processing
# 1 - argument
# 2 - variable to define
# 3 - keep or discard (if defined, keep)
# there may be arguments you don't want passed to the .py script
setup_add_arg() {
found=0
for parse in ${ARGPARSE[@]}; do
comp=${parse%%:*}
if [ "${comp}" = "$1" ]; then
found=1
fi
done
if [ ${found} -eq 0 ]; then
ARGPARSE[${#ARGPARSE[@]}]="$1:$2:$3"
fi
}
# Functions that add functionality during early processing
setup_add_func() {
ADDFUNCS[${#ADDFUNCS[@]}]="$1"
}
# Functions that export variables (or need to run very late)
setup_export_func() {
EXPORTFUNCS[${#EXPORTFUNCS[@]}]="$1"
}
# Functions that run on shutdown
setup_shutdown_func() {
SHUTDOWNFUNCS[${#SHUTDOWNFUNCS[@]}]="$1"
}
# Takes value_name default_value
# value_name is set to the first value found in the list:
# git config, git config --global, and finally default_value
add_gitconfig() {
VAR=$(git config "$1" || git config --global "$1" || echo "$2")
git config -f .gitconfig "${1}" "${VAR}"
}
shutdown() {
for func in "${SHUTDOWNFUNCS[@]}"; do
# During shutdown, we don't care about return codes
$func
done
}
shutdown_handler() {
echo -e "\nAborted by user, will terminate this setup."
shutdown
exit 1
}
# Input: argument list
# Output: 'help=1' or unset
# PASSARGS set to the arguments to pass on
parse_arguments() {
local found keep comp next val
while [ $# -ge 1 ] ; do
found=0
if [ "$1" = "--help" -o "$1" = "-h" ]; then
# Default into a --help module which is part of setup.py
help=1
PASSARGS[${#PASSARGS[@]}]="$1"
shift
continue
fi
for parse in ${ARGPARSE[@]}; do
comp=${parse%%:*}
next=${parse#${comp}:}
val=${next%%:*}
next=${next#${val}:}
if [ "${next}" != "${val}" ]; then
keep=${next}
else
keep=""
fi
case "$1" in
${comp}=*)
eval ${val}=\${1#*=}
if [ -n "${keep}" ]; then
PASSARGS[${#PASSARGS[@]}]="$1"
fi
shift
found=1
break
;;
${comp})
eval ${val}=\${2}
if [ -n "${keep}" ]; then
PASSARGS[${#PASSARGS[@]}]="$1"
# Only check whether $2 is set or not, set to "" or '--foo'
# should work because:
# - set to "": keep align with argparse since it works in this way.
# - set to "--foo": argparse knows it's not the arg of $1,
# but another option, and can handle it correctly.
if [ -n "${2+x}" ]; then
PASSARGS[${#PASSARGS[@]}]="$2"
fi
fi
if [ -z "${2+x}" ]; then
shift 1
else
shift 2
fi
found=1
break
;;
esac
done
if [ $found -ne 1 ]; then
PASSARGS[${#PASSARGS[@]}]="$1"
shift
fi
done
}
trap shutdown_handler INT
# Setup the minimal defaults first..
# BASEDIR, BASEURL and BASEBRANCH
BASEDIR=$(readlink -f "$(dirname "$0")")
# Argument parsing, define a limited set of args
setup_add_arg --base-url BASEURL keep
setup_add_arg --base-branch BASEBRANCH keep
help=0
parse_arguments "$@"
unset PASSARGS
# setup git url
REMOTEURL=$(cd "$BASEDIR" ; git config remote.origin.url 2>/dev/null)
# BASEURL is one directory above the git checkout
BASEREPO=""
if [ -z "${BASEURL}" ]; then
BASEURL=$(echo "$REMOTEURL" | sed -e 's,/$,,' -e 's,/[^/]*$,,')
BASEREPO=${REMOTEURL##$BASEURL\/}
fi
# First check if this is an absolute path (starts w/ '/')
# If it's not, we then check if it's a valid URL (contains ://)
if [ "${BASEURL:0:1}" != '/' ]; then
if [ "${BASEURL#*://}" == "${BASEURL}" -a "${BASEURL#*:}" == "${BASEURL}" ]; then
echo >&2
echo "ERROR: The BASEURL ($BASEURL) is not in a supported format." >&2
if [ -n "${BASEREPO}" ]; then
echo "The BASEURL was derived from the URL of $BASEREPO ($REMOTEURL)." >&2
echo "Either update the repository URL or use the --base-url argument to override." >&2
fi
echo >&2
echo "BASEURL must use an absolute file path, or a properly formatted remote URL" >&2
echo "such as:" >&2
echo " /home/user/path or file:///home/user/path" >&2
echo " http://hostname/path" >&2
echo " https://hostname/path" >&2
echo " git://hostname/path" >&2
echo " ssh://user@hostname/path" >&2
echo " [user@]hostname:path" >&2
echo >&2
exit 1
fi
fi
git_cmd="git --git-dir=$BASEDIR/.git"
if [ -z "${BASEBRANCH}" ]; then
BASEBRANCH=$($git_cmd rev-parse --abbrev-ref HEAD)
if [ "$BASEBRANCH" = "HEAD" ]; then
# Maybe this is a tag instead?
BASEBRANCH=$($git_cmd describe HEAD 2>/dev/null)
if [ $? -ne 0 ]; then
# No reasonable branch/tag name found...
BASEBRANCH=""
else
echo "$BASEBRANCH" | grep -q "vWRLINUX_CI_"
if [ $? -ne 0 ]; then
echo "ERROR: Tag $BASEBRANCH is not supported by setup.sh, please use a branch." >&2
echo ""
exit 1
fi
latest_tag=$($git_cmd tag --sort=taggerdate |tail -1)
if [ "$latest_tag" != "$BASEBRANCH" ]; then
echo "ERROR: Only latest tag is supported" >&2
echo "ERROR: Current tag: $BASEBRANCH" >&2
echo "ERROR: Latest tag: $latest_tag" >&2
echo ""
exit 1
fi
BASEBRANCH="refs/tags/$BASEBRANCH"
fi
fi
fi
# Load custom setup additions
if [ -d "${BASEDIR}/data/environment.d" ]; then
for envfile in ${BASEDIR}/data/environment.d/*.sh ; do
. $envfile
done
fi
# We need to reparse the arguments as we've now loaded the environment.d
# extensions
help=0
parse_arguments "$@"
if [ $help -ne 1 ]; then
# Before doing anything else, error out if the project directory
# is unsafe.
case "$PWD" in
$BASEDIR | $BASEDIR/*)
echo >&2 "$0: The current working directory is used as your project directory"
echo >&2 " Your project directory must not be in or under"
echo >&2 " '${BASEDIR}'"
echo >&2 ""
echo >&2 " Typically a project is setup by doing:"
echo >&2 " $ mkdir my_project"
echo >&2 " $ cd my_project"
echo >&2 " $ git clone --branch $BASEBRANCH $REMOTEURL"
echo >&2 " $ .$(echo $REMOTEURL | sed "s,$BASEURL,,")/setup.sh $@"
exit 1
;;
esac
if [ -z "$BASEBRANCH" ]; then
echo "May be on a detached HEAD, HEAD must be on a branch or tag. ($BASEDIR)" >&2
echo "You can avoid this by passing the branch using --base-branch=" >&2
exit 1
fi
# Is this a tag? If so, don't allow tags w/ '-'
if [ "$BASEBRANCH" != "${BASEBRANCH##refs/tags/}" ]; then
if [ "$BASEBRANCH" != "${BASEBRANCH//-*}" ]; then
echo "Checkout may be on a detached HEAD, this HEAD does not appear to" >&2
echo "correspond to a specific, tag. (It appears you may be working with" >&2
echo "tag ${BASEBRANCH//-*}. If this is correct, use" >&2
echo "--base-branch=${BASEBRANCH//-*} in the arguments to" >&2
echo "$0."
exit 1
fi
fi
for func in "${ADDFUNCS[@]}"; do
$func
rc=$?
if [ $rc -ne 0 ]; then
echo "Stopping: an error occurred in $func." >&2
shutdown
exit $rc
fi
done
# Configure the current directory so repo works seemlessly
add_gitconfig "user.name" "${GIT_USERNAME}"
add_gitconfig "user.email" "${GIT_USEREMAIL}"
add_gitconfig "color.ui" "false"
add_gitconfig "color.diff" "false"
add_gitconfig "color.status" "false"
fi # if help -ne 1
# We potentially have code that doesn't parse correctly with older versions
# of Python, and rather than fixing that and being eternally vigilant for
# any other new feature use, just check the version here.
py_v35_check=$(python3 -c 'import sys; print(sys.version_info >= (3,5,0))')
if [ "$py_v35_check" != "True" ]; then
echo >&2 "BitBake requires Python 3.5.0 or later as 'python3 (scripts/install-buildtools can be used if needed)'"
return 1
fi
unset py_v35_check
# This can happen if python3/urllib was not built with SSL support.
python3 -c 'import urllib.request ; dir(urllib.request.HTTPSHandler)' >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo >&2 "The setup tool requires Python 3.4.0 or later with support for 'urllib.request.HTTPSHandler'"
exit 1
fi
# Python 3 required utf-8 support to work properly, adjust the LANG to en_US.UTF-8.
export LANG='en_US.UTF-8'
# Pass the computed url and branch to ${cmd}
export OE_BASEURL=${BASEURL}
export OE_BASEBRANCH=${BASEBRANCH}
for func in "${EXPORTFUNCS[@]}"; do
$func
rc=$?
if [ $rc -ne 0 ]; then
echo "Stopping: an error occurred in $func." >&2
shutdown
exit $rc
fi
done
trap - INT
# Switch to the python script
${BASEDIR}/${CMD} "${PASSARGS[@]}"
rc=$?
shutdown
# Preserve the return code from the python script
exit $rc