-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo-static-analysis.install-system-deps.sh
executable file
·334 lines (304 loc) · 9.05 KB
/
do-static-analysis.install-system-deps.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
#!/usr/bin/env bash
# System dependency installation logic for the static analysis program
#
# Copyright 2024 林博仁(Buo-ren Lin) <[email protected]>
# SPDX-License-Identifier: CC-BY-SA-4.0 OR LicenseRef-Apache-2.0-If-Not-Used-In-Template-Projects
set \
-o errexit \
-o nounset
if test -v BASH_SOURCE; then
# Convenience variables
# shellcheck disable=SC2034
{
script="$(
realpath \
--strip \
"${BASH_SOURCE[0]}"
)"
script_dir="${script%/*}"
script_filename="${script##*/}"
script_name="${script_filename%%.*}"
}
fi
trap_exit(){
if test -v temp_dir \
&& test -e "${temp_dir}"; then
rm -rf "${temp_dir}"
fi
}
trap trap_exit EXIT
if test "${EUID}" -ne 0; then
printf \
'Error: This program should be run as the superuser(root) user.\n' \
1>&2
exit 1
fi
project_dir="$(dirname "${script_dir}")"
cache_dir="${project_dir}/.cache"
if ! test -e "${cache_dir}"; then
install_opts=(
--directory
)
if test -v SUDO_USER; then
# Configure same user as the running environment to avoid access
# problems afterwards
install_opts+=(
--owner "${SUDO_USER}"
--group "${SUDO_GID}"
)
fi
if ! install "${install_opts[@]}" "${cache_dir}"; then
printf \
'Error: Unable to create the cache directory.\n' \
1>&2
exit 2
fi
fi
apt_archive_cache_mtime_epoch="$(
stat \
--format=%Y \
/var/cache/apt/archives
)"
current_time_epoch="$(
date +%s
)"
if test "$((current_time_epoch - apt_archive_cache_mtime_epoch))" -ge 86400; then
printf \
'Info: Refreshing the APT local package cache...\n'
if ! apt-get update; then
printf \
'Error: Unable to refresh the APT local package cache.\n' \
1>&2
fi
fi
# Silence warnings regarding unavailable debconf frontends
export DEBIAN_FRONTEND=noninteractive
base_runtime_dependency_pkgs=(
wget
)
if ! dpkg -s "${base_runtime_dependency_pkgs[@]}" &>/dev/null; then
printf \
'Info: Installing base runtime dependency packages...\n'
if ! \
apt-get install \
-y \
"${base_runtime_dependency_pkgs[@]}"; then
printf \
'Error: Unable to install the base runtime dependency packages.\n' \
1>&2
exit 2
fi
fi
if ! test -v CI; then
printf \
'Info: Detecting local region code...\n'
wget_opts=(
# Output to the standard output device
--output-document=-
# Don't output debug messages
--quiet
)
if ip_reverse_lookup_service_response="$(
wget \
"${wget_opts[@]}" \
https://ipinfo.io/json
)"; then
grep_opts=(
--perl-regexp
--only-matching
)
if ! region_code="$(
grep \
"${grep_opts[@]}" \
'(?<="country": ")[[:alpha:]]+' \
<<<"${ip_reverse_lookup_service_response}"
)"; then
printf \
'Warning: Unable to query the local region code, falling back to default.\n' \
1>&2
region_code=
else
printf \
'Info: Local region code determined to be "%s"\n' \
"${region_code}"
fi
else
printf \
'Warning: Unable to detect the local region code(IP address reverse lookup service not available), falling back to default.\n' \
1>&2
region_code=
fi
if test -n "${region_code}"; then
# The returned region code is capitalized, fixing it.
region_code="${region_code,,*}"
printf \
'Info: Checking whether the local Ubuntu archive mirror exists...\n'
if ! \
getent hosts \
"${region_code}.archive.ubuntu.com" \
>/dev/null; then
printf \
"Warning: The local Ubuntu archive mirror doesn't seem to exist, falling back to default...\\n"
region_code=
fi
fi
if test -n "${region_code}" \
&& ! grep -q "${region_code}.archive.u" /etc/apt/sources.list; then
printf \
'Info: Switching to use the local APT software repository mirror...\n'
if ! \
sed \
--in-place \
"s@//archive.u@//${region_code}.archive.u@g" \
/etc/apt/sources.list; then
printf \
'Error: Unable to switch to use the local APT software repository mirror.\n' \
1>&2
exit 2
fi
printf \
'Info: Refreshing the local APT software archive cache...\n'
if ! apt-get update; then
printf \
'Error: Unable to refresh the local APT software archive cache.\n' \
1>&2
exit 2
fi
fi
fi
runtime_dependency_pkgs=(
# For matching the ShellCheck version string
grep
git
python3-minimal
python3-pip
python3-venv
# For extracting prebuilt ShellCheck software archive
tar
xz-utils
)
if ! dpkg -s "${runtime_dependency_pkgs[@]}" &>/dev/null; then
printf \
'Info: Installing the runtime dependency packages...\n'
if ! apt-get install -y \
"${runtime_dependency_pkgs[@]}"; then
printf \
'Error: Unable to install the runtime dependency packages.\n' \
1>&2
exit 2
fi
fi
shellcheck_dir="${cache_dir}/shellcheck-stable"
if ! test -e "${shellcheck_dir}/shellcheck"; then
printf \
"Info: Determining the host machine's hardware architecture...\\n"
if ! arch="$(arch)"; then
printf \
"Error: Unable to determine the host machine's hardware architecture.\\n" \
1>&2
exit 1
fi
printf \
'Info: Checking ShellCheck architecure availability...\n'
case "${arch}" in
x86_64|armv6hf|aarch64)
# Assuming the ShellCheck architecture is the same, which
# is probably incorrect...
shellcheck_arch="${arch}"
;;
*)
printf \
'Error: Unsupported ShellCheck architecture "%s".\n' \
"${arch}" \
1>&2
exit 1
;;
esac
printf \
'Info: Determining the ShellCheck prebuilt archive details...\n'
prebuilt_shellcheck_archive_url="https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.linux.${shellcheck_arch}.tar.xz"
prebuilt_shellcheck_archive_filename="${prebuilt_shellcheck_archive_url##*/}"
printf \
'Info: Creating the temporary directory for storing downloaded files...\n'
mktemp_opts=(
--directory
--tmpdir
)
if ! temp_dir="$(
mktemp \
"${mktemp_opts[@]}" \
"${script_name}.XXXXXX"
)"; then
printf \
'Error: Unable to create the temporary directory for storing downloaded files.\n' \
1>&2
exit 2
fi
printf \
'Info: Downloading the prebuilt ShellCheck software archive...\n'
downloaded_prebuilt_shellcheck_archive="${temp_dir}/${prebuilt_shellcheck_archive_filename}"
wget_opts=(
--output-document "${downloaded_prebuilt_shellcheck_archive}"
)
if ! \
wget \
"${wget_opts[@]}" \
"${prebuilt_shellcheck_archive_url}"; then
printf \
'Error: Unable to download the prebuilt ShellCheck software archive...\n' \
1>&2
exit 2
fi
printf \
'Info: Extracting the prebuilt ShellCheck software archive...\n'
tar_opts=(
--extract
--verbose
--directory="${cache_dir}"
--file="${downloaded_prebuilt_shellcheck_archive}"
)
if test -v SUDO_USER; then
# Configure same user as the running environment to avoid access
# problems afterwards
tar_opts+=(
--owner="${SUDO_USER}"
--group="${SUDO_GID}"
)
fi
if ! tar "${tar_opts[@]}"; then
printf \
'Error: Unable to extract the prebuilt ShellCheck software archive.\n' \
1>&2
exit 2
fi
fi
printf \
'Info: Setting up the command search PATHs so that the locally installed shellcheck command can be located...\n'
PATH="${shellcheck_dir}:${PATH}"
printf \
'Info: Querying the ShellCheck version...\n'
if ! shellcheck_version_raw="$(shellcheck --version)"; then
printf \
'Error: Unable to query the ShellCheck version.\n' \
1>&2
exit 2
fi
grep_opts=(
--perl-regexp
--only-matching
)
if ! shellcheck_version="$(
grep \
"${grep_opts[@]}" \
'(?<=version: ).*' \
<<<"${shellcheck_version_raw}"
)"; then
printf \
'Error: Unable to parse out the ShellCheck version string.\n' \
1>&2
fi
printf \
'Info: ShellCheck version is "%s".\n' \
"${shellcheck_version}"
printf \
'Info: Operation completed without errors.\n'